前言
linux安装python3本来不是什么难事,但是会遇到各种奇葩问题。姑且还是写下这篇文章记录一下。
一、下载
linux下执行
wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
或者,登录Python Source Releases | Python.org,找到对应版本(我们以Python 3.6.5为例)如图:
二、文件上传
将文件上传到Linux系统的某个目录下,根据自己情况上传,本例上传到了/root/tools
目录下,如图:
三、解压
执行tar -zxvf Python-3.6.5.tgz
命令,将文件解压到当前目录,如图:
四、准备编译环境
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
解释说明一下(标记核心的包务必安装。例如不安装libffi-devel,则会导致pandas导入时报错):
yum -y install zlib-devel bzip2-devel openssl-devel(核心) ncurses-devel sqlite-devel readline-devel tk-devel gcc make gdbm-devel db4-devel libpcap-devel xz-devel(核心) libffi-devel(核心)
如果python是3.7版本,还需要安装libffi-devel。整个编译过程1分钟左右。
五、编译安装
执行cd Python-3.6.5
进入解压后的Python-3.6.5目录下,依次执行如下三个命令(其中--prefix
是Python的安装目录):
./configure --prefix=/root/training/Python-3.6.5
make
make install
执行第一步时提示(./configure --enable-optimizations),不要执行,忽略即可(若不小心执行了,则删除解压文件Python-3.6.5.tgz重新解压即可)。执行下图命令会在make && make install
时导致 Could not import runpy module
错误(参考:linux中安装python 3.8.0 编译报错 Could not import runpy module 解决方案_HD243608836的博客-CSDN博客):
安装成功后,如图:
我们看到最后一行,同时安装了
setuptools
和pip
工具。到这里就安装完成了。
细心的朋友可能会发现,我上图是有一个warning的,这个是python路径的问题,我们到下面第七步会解决这个问题。
六、创建软链接
一般Linux系统已经默认安装了python2.7.5,这里我们不能将它删除,如果删除,系统可能会出现问题。我们只需要为Python3.6.5创建一个软链接即可,我们把软链接放到/usr/local/bin
目录下,如图:
七、配置环境变量
配置环境变量主要是能快速使用pip3安装命令。
执行 vi ~/.bash_profile
,打开配置文件,添加如下配置:
# 配置python
export PYTHON_HOME=/root/training/Python-3.6.5
export PATH=$PYTHON_HOME/bin:$PATH
保存退出(:wq
),执行source ~/.bash_profile
命令使配置生效。执行echo命令,查看是否配置成功,如图:
八、问题总结
问题1
Loaded plugins: fastestmirror 00:00:00 Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error" One of the configured repositories failed (Unknown), and yum doesn't have enough cached data to continue. At this point the only safe thing yum can do is fail. There are a few ways to work "fix" this: 1. Contact the upstream for the repository and get them to fix the problem. 2. Reconfigure the baseurl/etc. for the repository, to point to a working upstream. This is most often useful if you are using a newer distribution release than is supported by the repository (and the packages for the previous distribution release still work).
这个情况一般是不能连接外网,执行如下命令
vi /etc/sysconfig/network-scripts/ifcfg-ens33
每个人的Linux中ifcfg-ens33名称不一定完全一样。我的配置如下:
TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=no#BOOTPROTO=noneDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33UUID=296fb7a9-961a-46ea-bc1b-678cca49d40aDEVICE=ens33ONBOOT=yesIPADDR=192.168.189.111GATEWAY=192.168.189.2NETMASK=255.255.255.0DNS1=8.8.8.8PREFIX=24IPV6_PRIVACY=no
配置好保存,执行service network restart
重启网络服务。然后再重新执行上面的yum安装命令即可。
问题2:安装时报错ModuleNotFoundError: No module named '_ctypes'
执行如下命令:
yum install libffi-devel
从"./configure ...",也就是上面第五步开始重新安装。
问题3:pip3 install时报错“pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.”
先安装openssl-dev
,然后重新编译安装,只是在编译的过程中加入 --enable-optimizations
ubuntu:
sudo apt-get install libffi-dev
centos7
yum install libffi-devel -y
问题4:python安装后,bin目录下没有pip
这个问题,有可能是之前已经安装过python,系统还存在以前的pip,再重新安装python时出现。
我们会看到安装信息最后显示,pip已经在另一个目录。
我们根据这个目录,删掉历史目录的pip后,重新安装pip即可。重新安装pip的代码可以用这个
python -m pip install --upgrade pip