centos7 安装 python3

2021/11/4 7:09:42

本文主要是介绍centos7 安装 python3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

起因

为了试试 bpytop 进行了如下折腾 (所有操作均在虚拟机内完成)

先上成果图
PS: 确实 酷!:)

bpytop-view
bpytop-option


安装 python3


1.下载安装包
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
or
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz --no-check-certificate

2.解压

tar -xvJf  Python-3.7.0.tar.xz

3.编译安装

mkdir /usr/local/python3 #创建编译安装目录
cd Python-3.7.0
./configure --prefix=/usr/local/python3  # ./configure --prefix=/usr/local/python3 --with-ssl
make && make install

编译过程遇到的报错&解决办法

zipimport.ZipImportError: can't decompress data; zlib not available

ModuleNotFoundError: No module named '_ctypes'


4.创建软连接

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

5.验证

python3 -V
pip3 -V

参考:
https://www.cnblogs.com/anxminise/p/9650206.html
https://blog.csdn.net/blueheart20/article/details/72827666
https://blog.csdn.net/qq_42353939/article/details/94609591




后续:


pip3 无法正常使用,报错如下:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
[root@controller upload]# pip3 install --upgrade pip
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
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.")': /simple/pip/
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.")': /simple/pip/
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.")': /simple/pip/
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.")': /simple/pip/
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.")': /simple/pip/
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Requirement already up-to-date: pip in /usr/local/python3/lib/python3.7/site-packages (10.0.1)
You are using pip version 10.0.1, however version 21.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
# 下载 openssl 
wget https://files-cdn.cnblogs.com/files/luckjinyan/openssl-1.1.1g.tar.gz

tar -zxvf openssl-1.1.1g.tar.gz

cd openssl-1.1.1g/

# 编译安装
./config --prefix=/usr/local/openssl
make && make install

# 备份&替换旧的 openssl
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/lib64/openssl /usr/lib64/openssl.old
mv /usr/lib64/libssl.so /usr/lib64/libssl.so.old
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so /usr/lib64/libssl.so
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig -v // 建立动态链接

# 验证
openssl version

# 重新编译 python3
cd Python-3.7.0
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make && make install

升级pip3并完成 bpytop 的安装

[root@controller upload]# pip3 install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 538kB/s 
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-21.3.1
[root@controller upload]# pip3 install psutil-5.8.0-cp37-cp37m-manylinux2010_x86_64.whl 
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing ./psutil-5.8.0-cp37-cp37m-manylinux2010_x86_64.whl
Installing collected packages: psutil
Successfully installed psutil-5.8.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@controller upload]# pip3 install bpytop-1.0.67-py3-none-any.whl 
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Processing ./bpytop-1.0.67-py3-none-any.whl
Requirement already satisfied: psutil<6.0.0,>=5.7.0 in /usr/local/python3/lib/python3.7/site-packages (from bpytop==1.0.67) (5.8.0)
Installing collected packages: bpytop
Successfully installed bpytop-1.0.67
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
[root@controller upload]# 

参考:
https://www.jianshu.com/p/8e476eef43f7
https://www.cnblogs.com/luckjinyan/p/13124422.html



这篇关于centos7 安装 python3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程