CentOS6.x机器安装Python2.7.x

准备环境:CentOS6.9机器

1.查看机器默认的Python版本

[root@hlmcent69nma ~]# python -V
Python 2.6.6
[root@hlmcent69nma ~]# whereis python
python: /usr/bin/python /usr/bin/python2.6 /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python /usr/include/python2.6 /usr/share/man/man1/python.1.gz

2.安装gcc

[root@hlmcent69nma ~]# yum install gcc -y

3.下载最新的Python2.7.x安装包,解压并进入指定目录

[root@hlmcent69nma ~]# wget <https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz>
[root@hlmcent69nma ~]# tar zxvf Python-2.7.14.tgz
[root@hlmcent69nma ~]# cd Python-2.7.14

4.配置,编译,编译安装

[root@hlmcent69nma Python-2.7.14]# ./configure --prefix=/usr/local/python/python2.7
[root@hlmcent69nma Python-2.7.14]# make
[root@hlmcent69nma Python-2.7.14]# make install

备注:

执行 ./configure --prefix=/usr/local/python/python3 命令。./configure命令执行完毕之后创建一个文件Makefile,供下面的make命令使用。执行make install之后就会把程序安装到我们指定的目录中去。configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./configure --help输出详细的选项列表。其中--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr/local/share,比较凌乱。如果配置--prefix,如:./configure --prefix=/usr/local/test可以把所有资源文件放在/usr/local/test的路径中,不会杂乱。用了--prefix选项的另一个好处是卸载软件或移植软件。当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统)。当然要卸载程序,也可以在原来的make目录下用一次make uninstall,但前提是make文件指定过uninstall。

5.查看系统的Python版本,还是2.6.x

[root@hlmcent69nma ~]# python -V
Python 2.6.6

6.查看新安装的Python版本,当前系统的Python版本,并将系统指向的Python从2.6.x修改为2.7.x,再次查看当前系统的Python版本,已经变更为2.7.x

[root@hlmcent69nma ~]# /usr/local/python/python2.7/bin/python2.7 -V
Python 2.7.14
[root@hlmcent69nma ~]# /usr/bin/python -V
Python 2.6.6
[root@hlmcent69nma ~]# mv /usr/bin/python /usr/bin/python.bak
[root@hlmcent69nma ~]# ln -s /usr/local/python/python2.7/bin/python2.7 /usr/bin/python
[root@hlmcent69nma ~]# python -V
Python 2.7.14

7.但是发现yum命令无法使用了,由于yum是基于python2.6才能正常工作,需单独将yum指向python2.6版本

[root@hlmcent69nma ~]# yum list
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.14 (default, Nov 30 2017, 11:54:55)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)]
If you cannot solve this problem yourself, please go to
the yum faq at:
<http://yum.baseurl.org/wiki/Faq>

[root@hlmcent69nma ~]# vim /usr/bin/yum

sh2.png

sh2.png

参考链接:

https://www.cnblogs.com/perallina/p/5253329.html

https://www.cnblogs.com/lclq/archive/2016/06/27/5620196.html

如果安装setup.py出现from setuptools import setup, find_packages错误,解决方案如下:

安装后运行到:python setup.py install出现错误,如下:

[root@localhost pip-18.0]*# python setup.py install*
Traceback (most recent call last):
File “setup.py”, line 6, **infrom** setuptools **import** setup, find_packages
ImportError: No module named setuptools

解决方法:

下载:在它的官网可以下载到安装包:https://pypi.python.org/pypi/setuptools,下载路径:

8. 安装setuptools

# wget --no-check-certificate <https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz>
# tar -zxvf setuptools-12.0.3.tar.gz
# cd setuptools-12.0.3
# python setup.py install

9. 安装pip

# wget --no-check-certificate <https://bootstrap.pypa.io/get-pip.py>
# python get-pip.py

安装过程中可能会有一些依赖性问题需要解决,您可以根据具体的提示进行处理。

10. 验证pip安装情况

# pip -V

11. 安装其他需要的模块

使用pip安装其他需要的模块,例如:

# pip install requests
# pip install flask
# pip install numpy

12. 最后验证Python环境是否正常工作

# python
Python 2.7.14 (default, Nov 30 2017, 11:54:55)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

欢迎指出任何有错误或不够清晰的表达,可以在下面评论区评论。

×

喜欢就点赞,疼爱就打赏

//