CentOS7 安装 Python3.6
先看编译安装,编译安装后的文件目录基本和Windows平台上目录差不多。指定安装路径的话,所有的文件都是在一起的。
后面有yum的安装的方法。
编译安装
编译安装,会装好setuptools和pip这两个工具,都在site-packages这个文件夹里。之后再用pip安装各种模块,也都在pip所在的site-packages文件夹里。总之,所有的东西都在python3的安装目录下。
下载安装包
[root@Python36 ~]# cd ~
[root@Python36 ~]# mkdir download
[root@Python36 ~]# cd download/
[root@Python36 download]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
--2018-09-29 15:49:52-- https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
正在解析主机 www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:36::223
正在连接 www.python.org (www.python.org)|151.101.108.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22930752 (22M) [application/octet-stream]
正在保存至: “Python-3.6.6.tgz”
100%[==================================================================>] 22,930,752 59.5KB/s 用时 7m 48s
2018-09-29 15:57:40 (47.9 KB/s) - 已保存 “Python-3.6.6.tgz” [22930752/22930752])
[root@Python36 download]#
下载后解压:
[root@Python36 download]# tar -zxvf Python-3.6.6.tgz
安装依赖包
我一开始并没有装下面的依赖包,而是直接编译安装,等报错了再找解决办法:
[root@Python36 ~]# yum install gcc zlib-devel
.install 的时候需要gcc
make install 的时候需要zlib-devel
编译安装
如果缺少依赖包,编辑的时候就会有问题。到时候就查看错误信息,到网上搜一下,看看是缺少了哪个依赖包:
[root@Python36 download]# cd Python-3.6.6
[root@Python36 Python-3.6.6]# ./configure prefix=/usr/local/python3
注意,这里的编译命令有点问题,还需要加一个参数--with-ssl
。否则python可以顺利安装并且正常使用,但是运行pip的时候会有问题。具体在这节编译安装的最后加了一个小节pip3报错进行说明。
这里可以就按上面的命令来,然后参考最后一小节的方法来解决。不过既然已经知道有问题了,就把参数加上。把上面的命令替换为这个,多加一个参数:
yum install openssl-devel
./configure prefix=/usr/local/python3 --with-ssl
在编译安装python3.6.8的时候,需要在编译之前先把 openssl-devel 这个包也安装好。否则加了参数也是没用的
指定安装目录是:/usr/local/python3。
如果不指定安装目录的话,安装后可执行文件默认放在/usr/local/bin(默认放这里的可执行文件后面就不用另外再创建软链接了),库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,比较凌乱。似乎也不是太乱的样子,不过网上的文章都推荐在默认位置下再建一级目录,全部放一起,就是这里的做法。
我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。
然后make:
[root@Python36 Python-3.6.6]# make
这里遇到报错了:
gcc -pthread -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.6m.a -lpthread -ldl -lutil -lm
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones; ->
LC_ALL=C sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
# On Darwin, always use the python version of the script, the shell
# version doesn't use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then \
cp python-config.py python-config; \
fi
[root@Python36 Python-3.6.6]#
直接根据提示运行上面给的命令,然后再make:
[root@Python36 Python-3.6.6]# sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
[root@Python36 Python-3.6.6]# make
CC='gcc -pthread' LDSHARED='gcc -pthread -shared ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall' _TCLTK_INCLUDES='' _TCLTK_LIBS='' ./python -E ./setup.py build
running build
running build_ext
INFO: Can't locate Tcl/Tk libs and/or headers
warning: building with the bundled copy of libffi is deprecated on this platform. It will not be distributed with Python 3.7
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_dbm _gdbm _lzma
_sqlite3 _ssl _tkinter
readline zlib
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
atexit pwd time
running build_scripts
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pydoc3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/idle3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/2to3 -> build/scripts-3.6
copying and adjusting /root/download/Python-3.6.6/Tools/scripts/pyvenv -> build/scripts-3.6
changing mode of build/scripts-3.6/pydoc3 from 644 to 755
changing mode of build/scripts-3.6/idle3 from 644 to 755
changing mode of build/scripts-3.6/2to3 from 644 to 755
changing mode of build/scripts-3.6/pyvenv from 644 to 755
renaming build/scripts-3.6/pydoc3 to build/scripts-3.6/pydoc3.6
renaming build/scripts-3.6/idle3 to build/scripts-3.6/idle3.6
renaming build/scripts-3.6/2to3 to build/scripts-3.6/2to3-3.6
renaming build/scripts-3.6/pyvenv to build/scripts-3.6/pyvenv-3.6
[root@Python36 Python-3.6.6]#
make 成功后就是 make install :
[root@Python36 Python-3.6.6]# make install
验证
直接先运行python3,再确认一下版本信息:
[root@Python36 ~]# cd /usr/local/python3/
[root@Python36 python3]# ./bin/python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00)
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@Python36 python3]#
退出用 exit()
创建软链接
创建软链接的方法不是很好,还可以通过修改环境变量PATH来解决。不过还是先看下如何通过创建软链接解决问题。
直接执行python3命令是找不到的,在python3的启动命令加一条软链接到系统目录:
[root@Python36 ~]# python3
-bash: python3: 未找到命令
[root@Python36 ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@Python36 ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00)
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
pip工具是安装各种python包的,也很常用,也加个软链接:
[root@Python36 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@Python36 ~]# pip3 --version
pip 10.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
修改环境变量PATH
上面软链接,一次只解决了一个命令的问题。其实/usr/local/python3/bin/目录下所有的文件都是可执行文件,都需要设置为可以执行。当然也可以批量创建软链接,把 /usr/local/python3/bin/目录下的所有文件都创建一个软链接。但是这么做一方面很烦,而且解决不了将来的问题。因为将来使用pip安装时生成的可执行文件都会放到 /usr/local/python3/bin/目录中,到时候还是需要再创建软链接。
产生问题的原因
会出现这个问题是因为安装的时候我们手动指定了安装目录,原本这些文件都是在/usr/local/bin/目录下的,而这个目录里的文件都是可以直接执行的。
系统的环境变量PATH,指定了哪些目录用来存放可执行文件,这些目录都的文件都是在全局可以直接调用执行的。查看变量值的方法如下:
[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Python36 ~]#
设置环境变量
这里在profile中设置PATH,修改全局的环境变量。
编辑/etc/profile文件,找到export行,在下面新增加一行:
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCOntrOL
export PATH=$PATH:/usr/local/python3/bin
第一行是文件中原本的内容,第二行是我们增加的内容。
修改不会立刻生效,如果需要立即生效的话,可以执行source命令:
[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@Python36 ~]# source /etc/profile
[root@Python36 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin
[root@Python36 ~]#
用python命令进入python3
还可以把默认的python的软链接也指向python3,就是把系统默认的用python命令启动python2给替换成启动python3。这样做的问题是,系统的yum使用要python2,因此替换为python3后,yum就无法正常工作了。
解决办法就是修改yum的配置文件 vi /usr/bin/yum
,然后找到开头的 #!/usr/bin/python
修改为 #!/usr/bin/python2.7
保存退出就好了。
pip3报错
按照这里上面介绍的方法安装完成后,在使用pip的时候会出现报错:
[root@Python36 ~]# pip3 install -i https://mirrors.163.com/pypi/simple ansible
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://mirrors.163.com/pypi/simple
Collecting ansible
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.",)': /pypi/simple/ansible/
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.",)': /pypi/simple/ansible/
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.",)': /pypi/simple/ansible/
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.",)': /pypi/simple/ansible/
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.",)': /pypi/simple/ansible/
Could not fetch URL https://mirrors.163.com/pypi/simple/ansible/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.163.com', port=443): Max retries exceeded with url: /pypi/simple/ansible/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
Could not find a version that satisfies the requirement ansible (from versions: )
No matching distribution found for ansible
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://mirrors.163.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.163.com', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
[root@Python36 ~]#
原因
这里是ssl模块的问题,系统上现在有两个版本的python。使用我们编译安装的python3无法导入ssl模块:
[root@Python36 ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00)
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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'
>>> exit()
[root@Python36 ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()
[root@Python36 ~]#
在./configure过程中,如果没有加上–with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,刚好pip3过程需要ssl模块,而由于没有指定,所以该功能不可用。
解决办法
就是多加一个参数--with-ssl
,重新再做一遍编译安装。我这里也是全部安装完成之后才发现有问题的,不过没有关系,把之前编译安装的步骤再重新做一遍:
yum install openssl-devel
cd Python-3.6.6
./configure prefix=/usr/local/python3 --with-ssl
sed -e "s,@EXENAME@,/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
make
make install
在第一次编译安装3.6.6的时候没有印象安装个 openssl-devel 这个包,有可能是之前已经安装过了。不过最近在一台新的系统上编译安装3.6.8的时候发现一定要在编译之前把 openssl-devel 这个包安装好才行。
然后验证一下:
[root@Python36 ~]# python3
Python 3.6.6 (default, Aug 21 2019, 11:43:46)
[GCC ④.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()
[root@Python36 ~]#
编译python3.7遇到的问题
编译python3.7.4的时候,在make的过程中又出现了新的报错:
Failed to build these modules:
_ctypes
因为python3.7版本需要一个新的包libffi-devel,再yum安装一下即可:
$ yum install libffi-devel
装完之后,再运行一下make。
yum安装
用yum的话,步骤简单多了,就是安装路径会分散在不同的目录里。
安装epel源
用下面的命令就可以安装好epel源。不过国外的源可能会比较慢,可以都换成国内的yum源。
[root@Python3 ~]# yum install epel-release
上面的步骤就不要做了。
先确认有没有wget命令:
[root@Python3 ~]# wget --version
如果没有就先装一下,之后下载yum源的文件要用:
[root@Python3 ~]# yum install wget
备份现在的yum源
备份是个好习惯,直接删也可以。然后发现wget没装,现在一个yum源也没有了,只能找其他办法了。
[root@Python3 ~]# cd /etc/yum.repos.d/
[root@Python3 yum.repos.d]# mkdir bak
[root@Python3 yum.repos.d]# mv *.repo bak
[root@Python3 yum.repos.d]# ls
bak
现在文件夹已经空了,去下载yum源的repo文件,我用的是阿里的源:
[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[root@Python3 yum.repos.d]# wget https://mirrors.aliyun.com/repo/epel-7.repo
然后可以makecache建立本地缓存:
[root@Python3 yum.repos.d]# yum makecache
安装python
这里装了python,另外还装了一个setuptools,之后的pip工具要通过setuptools来安装。
[root@Python3 yum.repos.d]# yum install python36 python36-setuptools
安装完之后要用python36命令来启动,这里可以添加一下python3命令的软连接:
[root@Python3 ~]# ln -s python36 /usr/bin/python3
安装pip
通过setuptools的easyinstall命令来安装pip:
[root@Python3 ~]# easy_install-3.6 pip
安装目录
python 和 setuptools 都在这个目录里:/usr/lib/python3.6/site-packages/
而 pip 是在下面这个目录里,就是默认编译安装的目录:/usr/local/lib/python3.6/site-packages
之后用pip安装的模块也都会在pip的这个目录里。