1.1 下载地址
https://www.python.org/downloads/
https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
1.2 安装依赖包
[root@localhost ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
1.3 下载并解压 Python-3.7.9.tgz
[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
[root@localhost ~]# tar -zxvf Python-3.7.9.tgz
1.4 cd 到 Python-3.7.9
[root@localhost ~]# cd Python-3.7.9/
1.5 编译安装
[root@localhost Python-3.7.9]# ./configure --enable-optimizations --with-lto --enable-shared --prefix=/usr/local/python37 LDFLAGS=-Wl,-rpath=/usr/local/python37/lib
[root@localhost Python-3.7.9]# make && make install
1.6 配置系统环境变量 或 创建软连接
vim /etc/profile
export PYTHON_HOME=/usr/local/python37
export PATH=$PYTHON_HOME/bin:$PATH
source /etc/profile
[root@localhost ~]# echo $PATH
/usr/local/python37/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
或
ln -s /usr/local/python37/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python37/bin/pip3.7 /usr/bin/pip3
ln -s /usr/local/python37/bin/pyinstaller /usr/bin/pyinstaller
1.7 验证是否成功
[root@localhost Python-3.7.9]# python3
[root@localhost Python-3.7.9]# pip3
或者:
[root@localhost Python-3.7.9]# python3 -m pip list
1.8 删除安装文件
[root@localhost ~]# rm -rf Python-3.7.9.tgz
[root@localhost ~]# rm -rf Python-3.7.9
1.9 卸载
[root@localhost ~]# rm -rf /usr/local/python37
说明:
/usr/local/python37 为 1.5 编译安装 的安装目录。
1.10 删除软链接
[root@localhost ~]# rm /usr/bin/python3
[root@localhost ~]# rm /usr/bin/pip3
[root@localhost ~]# rm /usr/bin/pyinstaller
'''
配置选项:
./configure --enable-optimizations --with-lto --enable-shared --prefix=/usr/local/python37 LDFLAGS=-Wl,-rpath=/usr/local/python37/lib
--enable-optimizations
用 PROFILE_TASK 启用以配置文件主导的优化(PGO)(默认为禁用)。
--with-lto=[full|thin|no|yes]
在编译过程中启用链接时间优化(LTO)(默认为禁用)。
--enable-shared
启用共享 Python 库 libpython 的编译(默认为禁用)。
--prefix=PREFIX
在 PREFIX 中安装架构无关的文件。 在 Unix 上,它默认为 /usr/local。
/usr/local/python37 为安装路径。
LDFLAGS
链接器标志,例如,如果库位于非标准的目录 lib_dir 中,请使用 -Llib_dir 。
LDFLAGS=-Wl,-rpath=/opt/python/lib是一个编译器选项,用于指定编译器在链接时需要搜索的动态库路径。
其中,-Wl选项告诉编译器将-rpath参数传递给链接器,-rpath参数指定了需要搜索的动态库路径。
这个选项通常用于解决程序在运行时找不到动态库的问题,可以将动态库路径指定为程序运行时默认搜索的路径之一。
3.12.6 Documentation » Python安装和使用 » 3. 配置 Python
https://docs.python.org/zh-cn/3/using/configure.html#
'''
'''
安装命令:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar -zxvf Python-3.7.9.tgz
cd Python-3.7.9/
./configure --enable-optimizations --with-lto --enable-shared --prefix=/usr/local/python37 LDFLAGS=-Wl,-rpath=/usr/local/python37/lib
make && make install
'''
'''
参考:
Python 编译 Python 时的 –enable-optimizations 参数是什么作用
https://geek-docs.com/python/python-ask-answer/918_python_what_does_enableoptimizations_do_while_compiling_python.html
CentOS 7.6 配置pytorch的艰辛历程
https://blog.csdn.net/weixin_44220689/article/details/123900250
CentOS7 下安装python3.7和pip3 并保留系统自带python2.7
https://www.cnblogs.com/xiaoerjun/p/9837029.html
linux安装python的尝试(禁止照敲,只是尝试)
https://blog.csdn.net/cp1002327672/article/details/129521340
各种安装教程---(自用)
https://blog.csdn.net/wujing1_1/article/details/121152525
如何在linux中安装多个版本的python
https://blog.csdn.net/weixin_44789022/article/details/135974961
linux安装和卸载python3
https://www.cnblogs.com/leslie12956/p/13668097.html
altinstall with --enable-shared showing incorrect behaviour
https://bugs.python.org/issue27685
Build Python 3 on Linux
https://junhao.ca/zh/2021/08/25/build-python-3-on-linux/
聊一聊 Python 安装中的 --enable-shared
https://www.cnblogs.com/shenweiyan/p/15648259.html
error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such
https://zhuanlan.zhihu.com/p/68578487
已解决:动态库加载失败:cannot open shared object file: No such file or directory
https://blog.csdn.net/mahoon411/article/details/113576586
'''
'''
# 指定安装路径
./configure --enable-optimizations --with-lto --enable-shared --prefix=/usr/local/python37 LDFLAGS=-Wl,-rpath=/usr/local/python37/lib
[root@localhost bin]# pwd
/usr/local/python37/bin
[root@localhost bin]# ls
2to3 pip pydoc3.7 pyinstaller python3.7m
2to3-3.7 pip3 pyi-archive_viewer pyi-set_version python3.7m-config
easy_install-3.7 pip3.10 pyi-bindepend python3 python3-config
idle3 pip3.7 pyi-grab_version python3.7 pyvenv
idle3.7 pydoc3 pyi-makespec python3.7-config pyvenv-3.7
[root@localhost bin]#
'''
/usr/local/python37/bin/pyinstaller -F hello.py
'''
# 问题1:使用 pyinstaller 打包,error while loading shared libraries 。
[root@localhost bin]# /usr/local/python38/bin/pyinstaller -F /root/PycharmProjects/hello/hello.py
/usr/local/python38/bin/python3.8: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory
[root@localhost bin]# find / -name libpython3.8.so.1.0
/root/Python-3.8.10/libpython3.8.so.1.0
/usr/local/python38/lib/libpython3.8.so.1.0
# 方法1 将动态库绝对路径添加至环境变量 LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python37/lib/
或
export LD_LIBRARY_PATH=/usr/local/python37/lib/:$LD_LIBRARY_PATH
# 方法2 将目录 /usr/local/python37/lib/ 添加到 /etc/ld.so.conf.d/ 目录下的 python37.conf 文件中
# echo "/usr/local/python37/lib/" > /etc/ld.so.conf.d/python37.conf
# ldconfig
# 方法3 运行命令 # ldconfig
[root@localhost ld.so.conf.d]# vim /etc/ld.so.conf.d/python37.conf
/usr/local/python37/lib/
[root@localhost ld.so.conf.d]# ldconfig
'''
'''
问题2:卸载自带的 Python3 。
1.1 如果CentOS自带有python3需要更新指定版本的话,先把自带的删除,输入
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps
卸载掉python3,然后删除所有剩余文件
whereis python3 |xargs rm -frv
1.2 查看信息,如果没有提示版本信息则清理完成。
python3 --version
参考:
CentOS7下安装Python3,超详细完整教程
https://blog.csdn.net/sasafa/article/details/125577770
'''