CentOS7 安装 Python3.6
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 安装 Python3.6相关的知识,希望对你有一定的参考价值。
先看编译安装,编译安装后的文件目录基本和Windows平台上目录差不多。指定安装路径的话,所有的文件都是在一起的。
后面有yum的安装的方法。
编译安装
编译安装,会装好setuptools和pip这两个工具,都在site-packages这个文件夹里。之后再用pip安装各种模块,也都在pip所在的site-packages文件夹里。总之,所有的东西都在python3的安装目录下。
下载安装包
[[email protected] ~]# cd ~
[[email protected] ~]# mkdir download
[[email protected] ~]# cd download/
[[email protected] 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])
[[email protected] download]#
下载后解压:
[[email protected] download]# tar -zxvf Python-3.6.6.tgz
安装依赖包
我一开始并没有装下面的依赖包,而是直接编译安装,等报错了再找解决办法:
[[email protected] ~]# yum install gcc zlib-devel
.install 的时候需要gcc
make install 的时候需要zlib-devel
编译安装
如果缺少依赖包,编辑的时候就会有问题。到时候就查看错误信息,到网上搜一下,看看是缺少了哪个依赖包:
[[email protected] download]# cd Python-3.6.6
[[email protected] Python-3.6.6]# ./configure prefix=/usr/local/python3
指定安装目录是:/usr/local/python3。
如果不指定安装目录的话,安装后可执行文件默认放在/usr/local/bin(默认放这里的可执行文件后面就不用另外再创建软链接了),库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,比较凌乱。似乎也不是太乱的样子,不过网上的文章都推荐在默认位置下再建一级目录,全部放一起,就是这里的做法。
我们指定安装目录,以后卸载的话直接删除目录就可以干净卸载了。
然后make:
[[email protected] 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,@[email protected],/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
[[email protected] Python-3.6.6]#
直接根据提示运行上面给的命令,然后再make:
[[email protected] Python-3.6.6]# sed -e "s,@[email protected],/usr/local/python3/bin/python3.6m," < ./Misc/python-config.in >python-config.py
[[email protected] 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
[[email protected] Python-3.6.6]#
make 成功后就是 make install :
[[email protected] Python-3.6.6]# make install
验证
直接先运行python3,再确认一下版本信息:
[[email protected] ~]# cd /usr/local/python3/
[roo[email protected] python3]# ./bin/python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[[email protected] python3]#
退出用 exit()
创建软链接
直接执行python3命令是找不到的,在python3的启动命令加一条软链接到系统目录:
[[email protected] ~]# python3
-bash: python3: 未找到命令
[[email protected] ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[[email protected] ~]# python3
Python 3.6.6 (default, Sep 29 2018, 16:19:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
pip工具是安装各种python包的,也很常用,也加个软链接:
[[email protected] ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[[email protected] ~]# pip3 --version
pip 10.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
用python命令进入python3
还可以把默认的python的软链接也指向python3,就是把系统默认的用python命令启动python2给替换成启动python3。这样做的问题是,系统的yum使用要python2,因此替换为python3后,yum就无法正常工作了。
解决办法就是修改yum的配置文件 vi /usr/bin/yum
,然后找到开头的 #!/usr/bin/python
修改为 #!/usr/bin/python2.7
保存退出就好了。
yum安装
用yum的话,步骤简单多了,就是安装路径会分散在不同的目录里。
安装epel源
用下面的命令就可以安装好epel源。不过国外的源可能会比较慢,可以都换成国内的yum源。
[[email protected] ~]# yum install epel-release
上面的步骤就不要做了。
先确认有没有wget命令:
[[email protected] ~]# wget --version
如果没有就先装一下,之后下载yum源的文件要用:
[[email protected] ~]# yum install wget
备份现在的yum源
备份是个好习惯,直接删也可以。然后发现wget没装,现在一个yum源也没有了,只能找其他办法了。
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# mkdir bak
[[email protected] yum.repos.d]# mv *.repo bak
[[email protected] yum.repos.d]# ls
bak
现在文件夹已经空了,去下载yum源的repo文件,我用的是阿里的源:
[[email protected] yum.repos.d]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] yum.repos.d]# wget https://mirrors.aliyun.com/repo/epel-7.repo
然后可以makecache建立本地缓存:
[[email protected] yum.repos.d]# yum makecache
安装python
这里装了python,另外还装了一个setuptools,之后的pip工具要通过setuptools来安装。
[[email protected] yum.repos.d]# yum install python36 python36-setuptools
安装完之后要用python36命令来启动,这里可以添加一下python3命令的软连接:
[[email protected] ~]# ln -s python36 /usr/bin/python3
安装pip
通过setuptools的easyinstall命令来安装pip:
[[email protected] ~]# easy_install-3.6 pip
安装目录
python 和 setuptools 都在这个目录里:/usr/lib/python3.6/site-packages/
而 pip 是在下面这个目录里,就是默认编译安装的目录:/usr/local/lib/python3.6/site-packages
之后用pip安装的模块也都会在pip的这个目录里。
以上是关于CentOS7 安装 Python3.6的主要内容,如果未能解决你的问题,请参考以下文章