[root@python Python-3.6.5]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
安装依赖软件包
[root@python ~]# yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel curl epel-release gcc make pcre pcre-devel
下载
[root@python ~]# curl -o python3.6.tgz https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
解压安装
[root@python ~]# ls
Python-3.6.5.tgz
[root@python ~]# tar -xf Python-3.6.5.tgz -C /usr/local/src/
[root@python ~]# cd /usr/local/src/Python-3.6.5
进入目录后,把执行下面的命令
[root@python Python-3.6.5]# sed -ri 's/^#readline/readline/' Modules/Setup.dist
[root@python Python-3.6.5]# sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
[root@python Python-3.6.5]# sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist
[root@python Python-3.6.5]# sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist
[root@python Python-3.6.5]# sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist
开始编译安装
[root@python Python-3.6.5]# ./configure --enable-shared
......
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
如果您想要一个具有所有稳定优化活动的发布版本(PGO等),请运行/配置-启用优化
# --enable-shared 指定安装共享库,共享库在使用其他需调用用python的软件时会用到,比如使用mod_wgsi连接Apache与python时需要
[root@python Python-3.6.5]# make -j 2 && make install
# -j cpu核心数
......等待
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.3 setuptools-39.0.1
配置共享库文件
1. 为所有用户设置共享库目录
用 vi 编辑器打开配置文件 /etc/profile
[root@python Python-3.6.5]# vim /etc/profile
在文件末尾写上如下内容:
# python3.6 共享库目录
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
[root@python Python-3.6.5]# python3
python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory
假如 pycharm 在连接到这台机器的 python 解释器时仍然报找不到
libpython3.6m.so.1.0 文件.
编辑文件 /etc/ld.so.conf.d/python3.conf 内容如下:
[root@python Python-3.6.5]# vim /etc/ld.so.conf.d/python3.conf
/usr/local/lib
执行如下命令,是环境变量生效
source /etc/profile
# 此条命令只能让当前的 shell 环境下的环境变量生效,最好重启机器
测试python3
[root@python Python-3.6.5]# python3
Python 3.6.5 (default, Sep 16 2018, 18:23:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
>>> exit()
[root@python Python-3.6.5]#
输入 exit() 即可退出 python3
测试 pip3
[root@python Python-3.6.5]# pip3 -V
pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6)
# 假如上面显示的含有 python3.6 就没问题了,说名 pip3 安装的模块会安装到 上面显示的目录下
常⻅问题
假如 Python Shell 中敲击方向键显示「^[[C^[[D」
可以安装以下包
pip3 install gnureadline
[root@python Python-3.6.5]# pip3 install gnureadline
Collecting gnureadline
Downloading https://files.pythonhosted.org/packages/f5/c7/03754b54c8d0c5c5303ae2232ed36734faa91e819f0738b0d5d0a581f68c/gnureadline-6.3.8-cp36-cp36m-manylinux1_x86_64.whl (474kB)
100% |████████████████████████████████| 481kB 336kB/s
Installing collected packages: gnureadline
Successfully installed gnureadline-6.3.8
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.