文章目录
文章概述
-
Linux系统下进行python开发,若是默认安装的python版本不合适,可以安装新的python版本
-
文章演示的系统:Linux CentOS,除了
yum形式安装依赖包
部分不同,其他部分皆可借鉴到ubuntu -
系统默认的python版本暂时不删除
安装步骤
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
目录下创建了同名软连接,操作如下:
- 查询软连接来源,可以发现这个软连接不是我们安装的python目录
[root@localhost ~]# readlink -f /usr/bin/python3.8
/usr/bin/python3.8
- 删除已有软连接,并替换为我们的:
[root@localhost ~]# rm -f /usr/bin/python3.8
[root@localhost ~]# ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
- pip3.8软连接的检查,同上
镜像源替换
安装成功后,执行命令可能会报错:
[root@localhost /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@localhost ~]# cd ~/.pip
-bash: cd: /root/.pip: No such file or directory
[root@localhost .pip]# mkdir ~/.pip
- 打开
pip.conf
配置文件,并写入内容
[root@localhost .pip]# cd ~/.pip
[root@localhost .pip]# vim pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
- 更新pip,成功
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
文件末尾填入如下内容
- 第一行命令:创建的虚拟环境如果不特意指定版本,默认就是3.8版本。这个路径,就是上述创建软连接时候的路径(也可以不一样,只要是一个真实存在的就可以)
- 第二行命令:还不知道啥用,先配置上
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8
source /usr/local/python38/bin/virtualenvwrapper.sh
更新配置文件,使配置立刻生效
source ~/.bash_profile
测试创建虚拟环境
- 创建虚拟环境
[root@localhost .pip]# mkvirtualenv test
created virtual environment CPython3.8.6.final.0-64 in 214ms
creator CPython3Posix(dest=/root/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==23.3.1, setuptools==69.0.2, wheel==0.42.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/get_env_details
- 进入虚拟环境
(test) [root@localhost .pip]# workon test
(test) [root@localhost .pip]# python
Python 3.8.6 (default, Jan 18 2024, 15:52:38)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(test) [root@localhost .pip]# pip list
Package Version
---------- -------
pip 23.3.1
setuptools 69.0.2
wheel 0.42.0
WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
- 退出虚拟环境
(test) [root@localhost .pip]# deactivate
- 查看虚拟环境列表
[root@localhost .pip]# lsvirtualenv
test
====
- 删除虚拟环境
[root@localhost .pip]# rmvirtualenv test
Removing test...
个人理解
virtualenv只能基于系统上已有的python版本,来创建虚拟环境(可以创建任意多份),但是不能在系统上没有对应python版本的情况下,创建python虚拟环境
感觉还是conda更牛一点。
目前够用就行,用到了conda再说