Linux系统下进行python开发,若是默认安装的python版本不合适,可以安装新的python版本
文章演示的系统:Linux CentOS,除了yum形式安装依赖包
部分不同,其他部分皆可借鉴到ubuntu
系统默认的python版本暂时不删除
安装版本:Python 3.8
[root@localhost ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel -y
根目录下,下载安装包
[root@localhost ~]# cd ~
[root@localhost ~]# wget https://registry.npmmirror.com/-/binary/python/3.8.6/Python-3.8.6.tgz
解压缩,并进入解压后的目录
[root@localhost ~]# tar -xf Python-3.8.6.tgz
[root@localhost ~]# cd Python-3.8.6
配置编译和安装的路径,并进行编译和安装
[root@localhost ~]# ./configure --prefix=/usr/local/python38
[root@localhost ~]# make && make install
理解环境变量
[root@localhost ~]# echo $PATH
~/anaconda3/bin:/usr/local/git/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
以冒号分割,每个目录都是一个系统变量的存放路径。
若运行一个命令python3.8 test.py
:
python3.8
,Linux会优先到上述路径中找:按照顺序,依次查找通过建立软连接,来配置python3.8版本的全局访问
[root@localhost ~]# ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
[root@localhost ~]# ln -s /usr/local/python38/bin/pip3 /usr/bin/pip3.8
上述建立软连接的方法:将python3这个变量,更改为python3.8,所以后续运行的命令就以python3.8写。pip同理
!!!若建立软连接的时候显示软连接已经存在,有可能是系统默认存在python版本,且已经在/usr/bin
目录下创建了同名软连接,操作如下:
root@ubuntu:~# readlink -f /usr/bin/python3.8
/usr/bin/python3.8
root@ubuntu:~# rm -f /usr/bin/python3.8
[root@localhost ~]# ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
安装成功后,执行命令可能会报错:
root@ubuntu:/usr/bin# python3.8 -m pip install --upgrade pip
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Requirement already up-to-date: pip in /usr/local/python38/lib/python3.8/site-packages (20.2.1)
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
通过配置镜像源,可以加速下载,避免出错:
root@ubuntu:~# cd ~/.pip
-bash: cd: /root/.pip: No such file or directory
root@ubuntu:~# mkdir ~/.pip
pip.conf
配置文件,并写入内容root@ubuntu:~# cd ~/.pip
root@ubuntu:~/.pip# vim pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
python3.8 -m pip install --upgrade pip
virtualenv + virtualenvwrapper
更新pip、setuptools,安装pbr
[root@localhost ~]# python3.8 -m pip install --upgrade pip
[root@localhost ~]# python3.8 -m pip install --upgrade setuptools
[root@localhost ~]# pip3.8 install pbr
安装虚拟环境(第三方库)
[root@localhost ~]# pip3.8 install virtualenv
[root@localhost ~]# pip3.8 install virtualenvwrapper
配置系统变量
[root@localhost ~]# ln -s /usr/local/python38/bin/virtualenv /usr/bin/virtualenv
可以在任何目录,通过virtualenv xxx
来管理虚拟环境了
配置虚拟环境
进入文件
[root@localhost ~]# vim ~/.bash_profile
文件末尾填入如下内容
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8
source /usr/local/python38/bin/virtualenvwrapper.sh
更新配置文件,使配置立刻生效
source ~/.bash_profile
文章链接:https://zhuanlan.zhihu.com/p/405174594