如何在已安装Python解释器的Linux上更新Python - angener

时间:2024-03-09 10:56:03

如何在已安装Python解释器的Linux上更新Python

1.先删除存在的Python的软链接
 
# CD /usr/bin
# sudo  rm python2

 

 
2.下载新的Python安装包www.python.org
 
3.解压下载好的Python-3.x.x.tgz包(具体包名因你下载的Python具体版本不不同⽽而不不同,如:我下载的是Python3.6.8.那我这里就是Python-3.6.8.tgz)
# tar -zvxf python3.6.8.tgz
 
cd 到python3.6.8目录下
然后依次执行以下代码:
 
make
# make

 

 
 
 
make install
 
make install    或者 make && make install

 

 
 
 
5.建立python3的软链
 
#sudo ln -s /usr/local/python3/bin/python3  /usr/bin/python3

 

 
 
6.并将/usr/local/python3/bin加入PATH
 
 
# vim ~/.bash_profile

 

 
 
# .bash_profile

 

 
 
# Get the aliases and functions
 
 
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
 

 按ESC,输入:wq 或者 输入:x  回车退出。

 
修改完记得执行行下面的命令,让上一步的修改生效:
 
# source ~/.bash_profile

 

检查Python3及pip3是否正常可用:
 
# python3 -V
Python 3.6.8

 

 
 
 
 
# pip3 -V
pip 18.0.1 from

 

 
 
 
/usr/local/python3/lib/python3.6/site-packages (python 3.6)

 

 
 
 
7.不行的话在创建一下pip3的软链接(我也不清楚这一步有什么用)
 
 
# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

 

 

注:

  

提示找不到SSL模块
python安装完毕后,提示找不到ssl模块:
 
[root@localhost ~]# python2.7.5
Python 2.7.5 (default, Jun 3 2013, 11:08:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python27/lib/python2.7/ssl.py", line 60, in <module>
import _ssl # if we can\'t import it, let the error propagate
ImportError: No module named _ssl
>>>

 

5.2 重新编译python(去掉最后4行的注释)
#修改Setup文件
vim /root/Python-3.6.5/Modules/Setup.dist

 

#修改结果如下:
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

 

5.3 再次编译安装
./configure --prefix=/usr/local/python

 

make
make install

 

5.4 测试,已可正常使用。
[root@localhost ~]# python
Python 3.6.4 (default, Jun 3 2013, 14:56:13)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>