2020-01-23 linux环境安装python 3
0.yum安装依赖
yum install zlib zlib-devel
yum install zlib zlib-devel
yum install openssl openssl-devel -y
1.下载python3
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
2. 解压文件
TIPS:
创建tar.xz文件:先 tar cvf xxx.tar xxx/ 创建xxx.tar文件,然后 xz -z xxx.tar 将 xxx.tar压缩成 xxx.tar.xz
解压tar.xz文件:先 xz -d xxx.tar.xz 将 xxx.tar.xz解压成 xxx.tar, 然后再用 tar xvf xxx.tar来解包。
创建程序目录
mkdir -p /usr/local/python3
将下载的python安装包移至此目录,并解压
xz -d Python-3.8.1.tar.xz
tar xvf Python-3.8.1.tar
cd Python-3.8.1
3. 编译安装
./configure --prefix=/usr/local/python3
make
make install
4. 建立软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -sf /usr/local/python3/bin/python3 /usr/bin/python
5. 添加至环境变量
vim ~/.bash_profile
添加以下内容
PYTHON=/usr/local/python3/bin
export PATH=$PATH:$HOME/bin:$PYTHON
6. 测试
python3 -V
7. 安装PIP
Python包管理工具,拥有庞大的第三方支持库。
下载安装setuptools
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6
python3 setup.py build
python3 setup.py install
下载安装pip
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
cd pip-8.0.2
解压tar -zxvf pip-8.0.2.tar.gz
编译python3 setup.py build
安装python3 setup.py install
建立软连接ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
8. 安装过程遇到的问题
8.1 安装setuptools报如下错误
ModuleNotFoundError: No module named '_ctypes'
解决方法yum install libffi-devel
,然后从config开始重新安装。
(同时把zlib也安装上yum install zlib zlib-devel
yum install openssl openssl-devel -y
)
8.2 yum报错
[root@localhost bin]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
解决方法:
vi /usr/bin/yum
将文件头
#!/usr/bin/python
import sys
try:
import yum
修改为
#!/usr/bin/python2
import sys
try:
import yum
vi /usr/libexec/urlgrabber-ext-down
环境配置由python 变为 python2,#!/usr/bin/python2
8.3 pip安装第三方包的时候报错
How to fix “module 'platform' has no attribute 'linux_distribution'” when installing new packages with Python3.8?
见:https://stackoverflow.com/questions/58758447/how-to-fix-module-platform-has-no-attribute-linux-distribution-when-instal
只能回退到py3.7了,回退后执行以下命令无报错。(setuptools pip无需回退!)
pip3 install numpy
pip3 install matplotlib
pip3 install scipy
pip3 install sklearn
参考https://blog.csdn.net/zhangdongren/article/details/82685932
https://pip.pypa.io/en/stable/installing/