打开WEB浏览器访问 https://www.python.org/downloads/source/
选择适用于 Unix/Linux 的源码压缩包。
下载及解压压缩包 Python-3.x.x.tgz,3.x.x 为你下载的对应版本号。
如果你需要自定义一些选项修改 Modules/Setup
以 Python3.6.8 版本为例:
tar -zxvf Python-3.6.8.tgz
cd Python-3.6.8
./configure
make && make install
检查 Python3 是否正常可用:
python3 -V
Python 3.6.8
若想自定义python安装路径,需借助 --prefix=/< path >/…
tar -zxvf Python-3.6.8.tgz
cd Python-3.6.8
# 配置python安装的位置。若该目录不存在,安装过程中会自动建立
./configure --prefix=/home/dongxw/usr
make && make install
# 查看安装后的目录
ls /home/dongxw/usr
bin include lib share
# 建立python命令的软链接
ln -s /home/dongxw/usr/bin/python3 /home/dongxw/usr/bin/python
若系统中本来已安装python,例如在/usr/bin目录下,并且希望优先使用自定义路径安装的python时,应在环境变量 $PATH 中进行更改,将自定义路径的python放到 $PATH 中 /usr/bin 之前:
vim ~/.bashrc
PATH=$HOME/usr/bin:$PATH:$HOME/.local/bin
export PATH
查看python版本,看是否为自定义安装的版本:
python -V
Python 3.6.8
以安装 cheroot 为例。
pip install cheroot
如果想要升级可以输入
python -m pip install --upgrade cheroot
默认是从 https://pypi.org/simple/ 源进行安装,由于该网站在国内有可能被墙了,在安装程序时,无法从python官网下载资料,会报错如下:
Searching for cheroot
Reading https://pypi.org/simple/cheroot/
Download error on https://pypi.org/simple/cheroot/: unknown url type: https -- Some packages may not be found!
Couldn't find index page for 'cheroot' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.org/simple/
Download error on https://pypi.org/simple/: unknown url type: https -- Some packages may not be found!
No local packages or working download links found for cheroot
error: Could not find suitable distribution for Requirement.parse('cheroot')
可以更改安装源:
pip3 install cheroot -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
【常用国内源】:
若是频繁使用该源,可以进行如下常态化配置:
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
配置环境变量:%HOMEPATH%/.pip/pip.conf添加到 $PATH 中。[easy_install]
index_url = http://mirrors.aliyun.com/pypi/simple/
将 cheroot-6.3.0-py2.py3-none-any.whl 下载到 /home/dongxw/usr/lib/python3.6/site-packages/ 文件夹下,
cd /home/dongxw/usr/lib/python3.6/site-packages/
pip install cheroot-6.3.0-py2.py3-none-any.whl