centos安装python环境

ZhangJian 2022-03-16 n次浏览 Python 编辑

测试环境Centos7.6 Python3.9 系统默认自带Python2不能删除,系统命令要用到 查看默认版本,一般是位于/usr/bin/python目录下

python -V

查看已经安装的版本

ls -l /usr/bin/python*

创建安装位置文件夹

mkdir -p /usr/soft/python3.9

安装所需要的扩展

yum -y groupinstall "Development tools"
yum -y install gcc
yum -y install zlib zlib-devel
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel
yum -y install libffi libffi-devel

下载python安装包 官方下载地址:https://www.python.org/ftp/python/ 镜像站-推荐]:https://registry.npmmirror.com/binary.html?path=python 使用wget命令下载tar.xz格式压缩包

wget https://registry.npmmirror.com/-/binary/python/3.9.10/Python-3.9.10.tar.xz

解压,切到文件目录

tar -xvf Python-3.9.10.tar.xz
cd Python-3.9.10

安装到指定位置/usr/soft/python3.9

./configure --prefix=/usr/soft/python3.9 --with-ssl --enable-shared
make && make install

创建软链接

ln -s /usr/soft/python3.9/bin/python3 /usr/bin/python3.9
ln -s /usr/soft/python3.9/bin/pip3 /usr/bin/pip3.9

测试

python3.9 -V

运行报错:python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory,原因是系统不能到python库中获取文件,只需要想系统声明python库的路径即可

vi /etc/ld.so.conf.d/python3.conf

/usr/soft/python3.9/lib添加进去 i进入编辑模式 按esc后 按: 输入wq保存退出 链接动态库

ldconfig

参考地址:https://blog.csdn.net/weixin_39597399/article/details/110081732 pip源地址修改:https://www.88ksk.cn/blog/article/50.html