python源码编译安装和常见问题解决

python编译安装

1、下载源码包
 wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
 tar -zxf Python-3.9.10.tgz 
 
 cd python39/
 
2、编译安装

 ./configure --prefix=/usr/local/python39 --enable-shared --enable-optimizations
 make && make install 

--enable-shared 

启用共享,方便其他依赖python的一些内置库(比如 mysqlclient) 的资源的正常安装; 不启用可能报以下错误:

command 'gcc' failed with exit status 1

--enable-optimizations 是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化,但是编译可能有如下报错
Could not import runpy module SystemError: returned NULL without setting an error
如果报错,就取消该参数

开启enable-shared后会报 找不到so的错误 解决如下:
[root@web python39]# /usr/local/python39/bin/python3.9 -V
/usr/local/python39/bin/python3.9: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory


[root@web python39]# /usr/local/python39/bin/pip3 list
/usr/local/python39/bin/python3.9: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory

echo "/usr/local/python39/lib/" > /etc/ld.so.conf.d/python.conf
[root@web python39]# ldconfig 
[root@web python39]# /usr/local/python39/bin/pip3 list
Package    Version
---------- -------
pip        21.2.4
setuptools 58.1.0
WARNING: The repository located at mirrors.cloud.aliyuncs.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS ins
tead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.cloud.aliyuncs.com'.

3、Python软链
[root@web python39]# ln -s /usr/local/python39/bin/pip3.9 /usr/local/bin/pip3.9
[root@web python39]# ln -s /usr/local/python39/bin/python3.9 /usr/local/bin/python3.9
[root@web python39]# python3.9 -V
Python 3.9.10

4、导出项目依赖包

你可能感兴趣的:(python,开发语言,linux)