笔者环境为centos 6.8 x64位系统,笔者后续在centos 7.6 x64位系统中也按如下方式安装成功。
注意:不要修改系统自带的python,因为yum会用到系统自带python,且大多数只支持python2.x。
1: 安装必要的开发工具库
yum update
yum -y groupinstall "Development tools"
yum -y install zlib* zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel pcre pcre-devel gcc gcc-c++
2:下载对应的python源码
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
3:安装python(将下载的源代码移到/usr/local/python3目录)
mkdir /usr/local/python3
mv Python-3.6.3.tar.xz /usr/local/python3/
cd /usr/local/python3/
tar -xvJf Python-3.6.3.tar.xz
cd Python-3.6.3
# 设置编译路径
./configure --prefix=/usr/local/python3
# 编辑Setup以及Setup.dist文件
vim Modules/Setup
vim Modules/Setup.dist
# 找到以下内容,并改成与下面一样,再保存
# 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/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
# 重新设置编译路径
./configure --prefix=/usr/local/python3
# 编译与安装
make && make install
坑:缺少SSL,由于python3在编译过程中没有带上SSL,但是在使用pip3
的时候,需要ssl
的支持,解决方案是参考上述编辑Setup以及Setup.dlist文件
如果后期运行也提示"no module _ssl"可参考此博主文章来解决https://blog.csdn.net/qq_26870933/article/details/84336109,需要重新编译python,重新编译前,在源码目录下执行下make clear清除上次编译信息
Traceback (most recent call last):
File "/www/ny20/venv/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
File "/www/ny20/venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
from gunicorn.app.base import Application
File "/www/ny20/venv/lib/python3.6/site-packages/gunicorn/app/base.py", line 14, in <module>
from gunicorn.config import Config, get_default_config_file
File "/www/ny20/venv/lib/python3.6/site-packages/gunicorn/config.py", line 18, in <module>
import ssl
File "/usr/local/python3/lib/python3.6/ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
4:建立软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
5:将python加入环境变量
# 打开环境变量修改文件末尾的PATH变量
vim ~/.bash_profile
# 将/usr/local/python3/bin增加到PATH中,保存并退出
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
# 使环境变量生效
source ~/.bash_profile
# 检查环境变量是否配置成功,查看输出值是否包含/usr/local/python3/bin
echo $PATH
文章是笔者在网上搜索资料并实际在centos 6.8 x64位系统上实践过的,参考了以下两个博主的经验: