这里是普通文章模块栏目内容页
centos7.9中安装python3.9.9

centos7.9中安装python3.9.9

1.安装相应的编译工具

在root用户下(不要用普通用户,麻烦),全部复制粘贴过去,一次性安装即可.

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install -y libffi-devel zlib1g-dev
yum install zlib* -y

2.下载安装包

wget https://www.python.org/ftp/python/3.9.9/Python-3.9.9.tar.xz

3.解压

tar -xvJf  Python-3.9.9.tar.xz

4.创建编译安装目录

mkdir /usr/local/python3 

5.安装

cd Python-3.9.9
./configure --prefix=/usr/local/python3 --with-ssl-default-suites=openssl
#./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl-default-suites=openssl
#第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便.
#第二个可以提高python10%-20%代码运行速度,但是gcc小于8.1.0会报错是,见错误4
#第三个是为了安装pip需要用到ssl,后面报错会有提到,原选项 --with-ssl,此版本用 --with-ssl-default-suites=openssl。
make && make install

what-does-enable-optimizations-do-while-compiling-python

6.创建软链接

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

7.验证是否成功

python3 -V
pip3 -V

8.报错处理

错误1.

zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1099: recipe for target 'install' failed make: *** [install] Error 1

需要安装依赖

yum -y install zlib1g-dev

错误2.

ModuleNotFoundError: No module named '_ctypes'

需要安装依赖

yum -y install libffi-devel 

这两个错误需要的依赖已经添加到一开始的依赖安装上去了
参考文章

错误3

使用如下编译会报警告
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl

警告如下
configure: WARNING: unrecognized options: --with-ssl

错误4

Traceback (most recent call last):
  File "/root/Python-3.9.9/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/root/Python-3.9.9/Lib/importlib/util.py", line 2, in <module>
    from . import abc
  File "/root/Python-3.9.9/Lib/importlib/abc.py", line 17, in <module>
    from typing import Protocol, runtime_checkable
  File "/root/Python-3.9.9/Lib/typing.py", line 21, in <module>
    import collections
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/root/Python-3.9.9'
make: *** [profile-opt] Error 2

导致原因:
在低版本的gcc中带有–enable-optimizations参数
解决方法(不推荐使用方法1):
1、升级gcc至高版本,gcc 8.1.0已修复此问题
2、./configure参数中去掉–enable-optimizations

9.安装pipenv

pip install pipenv

在centos中使用python3.7或以上版本,进行pip install 命令容易报错

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https:*******: There was a problem confirming the ssl certificate: 
Can't connect to HTTPS URL because the SSL module is not available. - skipping

在./configure过程中,如果没有加上–with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,刚好pip3过程需要ssl模块,而由于没有指定,所以该功能不可用。解决办法是重新对python3.9进行编译安装,用一下过程来实现编译安装:

cd Python-3.9.9
./configure --with-ssl-default-suites=openssl
make && make install

即可正常使用pip安装.
这个也在安装python的时候指定了.
参考文章

10.修改pip安装源

修改系统pip安装源
在家目录下新建.pip文件夹,进入文件夹新建文件pip.conf之后写入相应镜像网站地址

cd ~
mkdir .pip
cd .pip
vim pip.conf

#进入后添加以下内容,保存退出.
[global]
index-url = https://mirrors.aliyun.com/pypi/simple

修改pipenv安装源
在自己的虚拟环境中找到Pipfile文件,将其中的url = "https://pypi.org/simple"修改为你需要的国内镜像,如https://mirrors.aliyun.com/pypi/simple/

[root@localhost myproject]# vim Pipfile 


[[source]]
name = "pypi"
url = "https://pypi.org/simple" # 改为url = "https://mirrors.aliyun.com/pypi/simple/"
verify_ssl = true

[dev-packages] #这里是开发环境专属包,使用pipenv install --dev package来安装专属开发环境的包

[packages] # 全部环境的通用包,安装在这里.

[requires]
python_version = "3.7"