文章目录
- Linux
- 1、下载
- 2.0、安装前的埋坑
- 坑1
- 坑2
- 坑3
- 2、安装
- 3、配置
- 3.1、配置/etc/profile
- 3.2、创建全局软链接
- 4、验证
- Windows
- 1、下载
- 2、安装
- 3、配置
- 4、验证
Linux
应用环境 | 安装版本 | 安装时间 |
Deepin | 15.11 | 2020-05-08 |
Python3 | 3.8.2 | 2020-05-08 |
1、下载
2.0、安装前的埋坑
- 如果跳过2.0埋坑步骤直接进行安装,恭喜你,你十有八九会掉坑里[滑稽笑容]
- 以下埋坑步骤是我反复查资料、删除、编译、安装试了不下5遍,说多了都是泪[内牛满面]
坑1
- 此报错信息是在运行Python环境导包阶段出现的,缺少依赖包,安装
libffi-dev
即可,请删除之前的Python环境重新编译安装 - Python3中有个内置模块叫ctypes,它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library)
# 报错信息
ModuleNotFoundError: No module named ‘_ctypes’
# 安装依赖包libffi-dev
# nangy @ nangy-deepin in ~ [8:09:50]
$ sudo apt install libffi-dev
坑2
- 此报错信息是在python进行make阶段出现的,可能是自己之前安装的
openssl
版本过高,出现版本不兼容 - 去官网下载一个不是那么高的版本,我下载的1.0.2版本,适中
- openssl具体安装步骤,会在下面
坑3
描述,请往下看
# 报错信息
./python: /lib/x86_64-linux-gnu/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by ./python)
# nangy @ nangy-deepin in ~/Downloads/software [8:18:16]
$ wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
坑3
# 报错信息
ModuleNotFoundError: No module named '_ssl'
- 此时要做:
- 删除
Python、openssl
旧环境(只删除自己安装的环境,可别删系统自带,删除系统自带容易造成系统不稳定) - 编译安装
坑2
步骤下载的openssl - 编译安装Python前,修改
Python-3.8.2/Modules/Setup
,其他文章说还有Setup.dist
,但是我在3.8.2中没有找到
- 删除之前报错的环境:安装时最后自定义安装路径,就是为了这个时候的删除动作
# nangy @ nangy-deepin in ~/Downloads/software [8:19:41]
$ sudo rm -rf /usr/local/python/python38/
# nangy @ nangy-deepin in ~/Downloads/software [8:19:41]
$ sudo rm -rf /usr/local/openssl/
- 编译安装openssl
# nangy @ nangy-deepin in ~/Downloads/software [8:19:41]
$ tar -zxvf openssl-1.0.2u.tar.gz
# nangy @ nangy-deepin in ~/Downloads/software [8:19:41]
$ cd openssl-1.0.2u
# 最好自定义路径,避免影响系统自带,也方便后期删除重编译
# nangy @ nangy-deepin in ~/Downloads/software/openssl-1.0.2u [8:19:41]
$ sudo ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
# nangy @ nangy-deepin in ~/Downloads/software/openssl-1.0.2u [8:19:41]
$ sudo make
# nangy @ nangy-deepin in ~/Downloads/software/openssl-1.0.2u [8:19:41]
$ sudo make test
# nangy @ nangy-deepin in ~/Downloads/software/openssl-1.0.2u [8:19:41]
$ sudo make install
- 修改Python-3.8.2/Modules/Setup文件
# nangy @ nangy-deepin in ~/Downloads/software [8:41:14]
$ tar -zxvf Python-3.8.2.tgz
# nangy @ nangy-deepin in ~/Downloads/software [8:41:14]
$ cd Python-3.8.2
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [8:41:38]
$ vim Modules/Setup
- 大概在209行左右,取消下面4行的注释
- SSL项要改成刚刚自定义安装openssl的路径
# Socket module helper for socket(2)
_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/openssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
- 好了,如果以上埋坑动作准备好了,下面就可以开始真正的编译安装Python了
2、安装
- 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境,
- 比如xxx!!!!!
- 不要动现有的python2环境!
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [20:43:25]
$ sudo ./configure --prefix=/usr/local/python/python38
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [20:50:52]
$ sudo make
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [20:59:58]
$ sudo make install
- 注释1:如果不配置
--prefix
,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较分散。 - 注释2:配置
--prefix
,所有资源文件放在/usr/local/python/python38
的路径中,方便管理,以后不用了,直接删除此目录即可。
3、配置
3.1、配置/etc/profile
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [21:02:37]
$ sudo vim /etc/profile
- 将以下配置到内容末尾
# python3
export PATH=/usr/local/python/python38/bin:$PATH
-
wq
保存退出,并source生效,为了保险,最好把系统重启一下 - 因为我装的
zsh
,直接source会报错,so,切换到bash
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [21:03:52]
$ su -
密码:
root@nangy-deepin:~# source /etc/profile
3.2、创建全局软链接
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [21:03:52]
$ sudo ln -s /usr/local/python/python38/bin/python3.8 /usr/bin/python3.8
# nangy @ nangy-deepin in ~/Downloads/software/Python-3.8.2 [21:03:52]
$ sudo ln -s /usr/local/python/python38/bin/pip3.8 /usr/bin/pip3.8
4、验证
# nangy @ nangy-deepin in ~ [17:06:44]
$ python3.8 -V
Python 3.8.2
# nangy @ nangy-deepin in ~ [17:07:44]
$ pip3.8 -V
pip 19.2.3 from /usr/local/python/python38/lib/python3.8/site-packages/pip (python 3.8)
Windows
1、下载
2、安装
3、配置
- 如果安装第一步选择了
Add Python3.8 to PATH
,则不需要手动配置
4、验证