linux上同时安装python2.7和python3.6

今天装了CentOS服务器,为其装python3.6的环境,具体步骤如下:
自已已经提前安装python2.7,
注:centos 安装基础
转移安装包:
生成requirements.txt文件

pip freeze > requirements.txt

安装requirements.txt依赖

pip install -r requirements.txt
1. 安装可能需要的依赖包:
  • yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel wget gcc make –y
2. 下载安装包
解压
  • tar zxvf Python-3.6.5.tgz
创建python3.6文件夹
  • mkdir /usr/local/python3.6
进入解压后的文件夹
3. 指定安装路径,编译安装
  • ./configure --prefix=/usr/local/python3.6
  • make
  • make install
4. 添加环境变量

第四步,添加也不可以,失败,直接跳到二标题。

  • vim /etc/profile
    添加以下

export PATH=$PATH:/usr/local/python3.6/bin

5. 重载环境变量

上面的第四步可以使用一下命令替换:

export PATH=/usr/local/python3.6/bin:$PATH

$ source /etc/profile
好了,通过以上的步骤就可以实现python3.6的安装了,现在在界面输入python3 就可以打开python3.6.0 了,直接输入python会是系统自带的 python2.7。

6. 修改pip3

python3 -m pip install --upgrade pip --force-reinstall,显示重新安装成功。

二、重启连接服务器使用python3失败

1. 建立python3的软链

ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3

2. 并将/usr/local/python3.6/bin加入PATH

vim ~/.bash_profile

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3.6/bin
export PATH

按ESC,输入:wq回车退出。

修改完记得执行行下面的命令,让上一步的修改生效:

  • source ~/.bash_profile

检查Python3及pip3是否正常可用:

# python3 -V
Python 3.6.1
# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
3. 不行的话尝试创建一下pip3的软链接
  • ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3
  1. 图像识别环境部署
  2. centos python3 的 卸载 删除

三、更换pip 安装源

pip国内的一些镜像
  阿里云 https://mirrors.aliyun.com/pypi/simple/
  中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  豆瓣(douban) http://pypi.douban.com/simple/
  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

修改源方法:
临时使用:
可以在使用pip的时候在后面加上-i参数,指定pip源

pip install opencv-python -i  https://mirrors.aliyun.com/pypi/simple

Linux:
永久使用可以这样
修改 ~/.pip/pip.conf 文件,如下

# 阿里云
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

# 中国科学技术大学
[global]
index-url=http://pypi.mirrors.ustc.edu.cn/simple/

[install]
trusted-host=pypi.mirrors.ustc.edu.cn

# 清华大学
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/

[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

出现如下报错:
E212: Can't open file for writing
问题解决:
因为不存在.pip这个文件夹所以会存在这个问题,新建文件夹~/.pip就可以解决了.

 mkdir ~/.pip
 vim ~/.pip/pip.conf

Windows:
接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini,内容如下:

[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/

centos7 安装python3.8

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install libffi-devel -y
# 1.下载安装包
wget https://www.python.org/ftp/python/3.8.13/Python-3.8.13.tgz
# 解压
tar zxvf Python-3.8.13.tgz
# 2.创建python3.8文件夹
mkdir /usr/local/python3.8
# 3.指定安装路径,编译安装
cd Python-3.8.13
./configure --prefix=/usr/local/python3.8
make
make install
# 4.环境配置
vim /etc/profile
export PATH=$PATH:/usr/local/python3.8/bin
source /etc/profile
# 5.建立python3的软链
ln -s /usr/local/python3.8/bin/python3 /usr/bin/python3
python3 -m pip install --upgrade pip --force-reinstall
cd /usr/bin
mv pip pip.bak
ln -s /usr/local/python3.8/bin/pip3 /usr/bin/pip
# 6.环境配置
vim ~/.bash_profile
export PATH=$PATH:$HOME/bin:/usr/local/python3.8/bin
source ~/.bash_profile
# 7.查看
[root@cdh03 ~]# cd /usr/bin/
[root@cdh03 bin]# ls -alh|grep python
lrwxrwxrwx   1 root root     29 Apr 29 15:38 pip3 -> /usr/local/python3.8/bin/pip3
lrwxrwxrwx   1 root root      7 Apr 25 08:52 python -> python2
lrwxrwxrwx   1 root root      9 Apr 25 08:52 python2 -> python2.7
-rwxr-xr-x   1 root root   7.0K Nov 17  2020 python2.7
lrwxrwxrwx   1 root root     32 Apr 29 15:35 python3 -> /usr/local/python3.8/bin/python3

问题:****报错-ModuleNotFoundError: No module named '_lzma'

最好保留系统原转的Python,否则出很多问题。

解决方法:

第一步:

# centos系统执行
yum install xz-devel -y
yum install python-backports-lzma -y
pip install backports.lzma

# ubuntu系统执行
apt-get install liblzma-dev -y
pip install backports.lzma

第二步:

vim /usr/local/python3.8/lib/python3.8/lzma.py

第三步:

修改之前废弃的代码

try:
    from _lzma import *
    from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
    from backports.lzma import *
    from backports.lzma import _encode_filter_properties, _decode_filter_properties

重启即可

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容