Linux下安装或升级Python 2.7(含pip安装)

1.准备编译环境gcc,没有gcc编译环境会出现报错,如下:

[root@localhost ~]# cd Python-2.7.11
[root@localhost Python-2.7.11]# ./configure –prefix=/usr/local/python27
checking build system type… x86_64-unknown-linux-gnu
checking host system type… x86_64-unknown-linux-gnu
checking for –enable-universalsdk… no
checking for –with-universal-archs… 32-bit
checking MACHDEP… linux2
checking EXTRAPLATDIR…
checking for –without-gcc… no
checking for gcc… no
checking for cc… no
checking for cl.exe… no

configure: error: in `/root/Python-2.7.11′:
configure: error: no acceptable C compiler found in $PATH
See `config.log’ for more details
(原因是服务器缺少gcc编辑器)

2.去官网下载要安装的对应版本的python的源代码

下载地址:https://www.python.org/downloads/source/

你可以选择你要下载的版本,用wget指令来下载相应的源代码

3.解压下载的代码包

1 2 tar  -zxvf Python-x.x.x.tgz cd  Python-x.x.x

4.配置

1)查找configure文件

1 2 find  . -name configure cd  搜索结果(一般就在Python文件根目录下)

2)进行配置

1 . /configure

5.编译

1 2 make make  install

(如果没有其他特殊需求,安装就到此结束了,输入pythonx.x即可以进入你刚刚安装的python开发环境)

6.替换以前的python默认版本(创建新的软连接)

1 2 3 cd  /usr/bin/ rm  -rf python ln  -s  /usr/local/Python-x .x.x /bin/python  . /python 注意:/usr/local/Python-x.x.x/bin/python 路径也有可能是 /usr/local/bin/python2.x.x

你直接输入python就是你最新安装的python新版本,要想用以前的版本,可以输入pythonx.x来启动!

7.升级完python之后,yum可能无法正常使用,提示如下:

There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
   No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It’s possible that the above module doesn’t match the
current version of Python, which is:
2.7.3 (default, Jul 15 2014, 16:52:36) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)]
If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://wiki.linux.duke.edu/YumFaq

应该是yum和Python是依赖关系,yum是python的模块,所以需要修改yum来解决:

1、修改yum
# vim /usr/bin/yum 

将 #!/usr/bin/python 改成  #!/usr/bin/python2.4
【保存并退出】

2. 再测试yum安装,成功;

==========================================================

pip安装:

1、脚本安装

$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py

$ python get-pip.py

2、yum安装

$ sudo yum install python-pip

$ sudo apt-get install python-pip

脚本安装过程中,可能会遇到两个问题:

(1)zlib、zlib-devel没有安装,报错如下:

[root@localhost home]# python get-pip.py 
Traceback (most recent call last):
  File “get-pip.py”, line 20061, in <module>
    main()
  File “get-pip.py”, line 194, in main
    bootstrap(tmpdir=tmpdir)
  File “get-pip.py”, line 82, in bootstrap
    import pip
zipimport.ZipImportError: can’t decompress data; zlib not available

解决方法:
1、安装依赖zlib、zlib-devel
2、重新编译安装Python

  1. ./configure 
  2. 编辑Modules/Setup文件 
  3. 找到下面这句,去掉注释 
  4. #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz 
  5. 重新编译安装:make & make install


(2)SSL没有安装,报错如下:

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pip
  Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: Can’t connect to HTTPS URL because the SSL module is not available. – skipping
  Could not find a version that satisfies the requirement pip (from versions: )
No matching distribution found for pip

解决方案如下:

[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
>>>

2. 查看openssl安装包,发现缺少openssl-devel包
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-20.el5
openssl-0.9.8e-20.el5
[root@localhost ~]#

3. yum安装openssl-devel
[root@localhost ~]# yum install openssl-devel -y
#查看安装结果
[root@localhost ~]# rpm -aq|grep openssl
openssl-0.9.8e-26.el5_9.1
openssl-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1
openssl-devel-0.9.8e-26.el5_9.1

4. 重新编译python
#修改Setup文件
vi /usr/software/Python-2.7.5/Modules/Setup
#修改结果如下:
# 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. 重新编译
make
make install

6. 测试,已可正常使用。
[root@localhost ~]# python2.7.5
Python 2.7.5 (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
>>>


pip安装完成后,通过一下指令查看版本:

 pip -V  #查看pip版本

    原文作者:lovemysea
    原文地址: https://blog.csdn.net/lovemysea/article/details/78158435
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞