在CentOS6上安装Python2.7的解决方法

yipeiwu_com5年前Python基础

在CentOS6上yum安装工具是基于Python2.6.6的,所以在CentOS6上默认安装的是Python2.6.6,因为要在服务器系统为CentOS6上部署生产环境,但是代码都是基于Python2.7写的,所有遇到了问题。

探索

发现系统不能卸载Python2.6后,查了系统的版本号

cat /etc/*-release

发现系统版本为CentOS6,于是开始Google搜索怎么解决。

解决办法

重新手动装一个Python2.7

准备阶段

# Start by making sure your system is up-to-date:
yum update
# Compilers and related tools:
yum groupinstall -y "development tools"
# Libraries needed during compilation to enable all features of Python:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
# If you are on a clean "minimal" install of CentOS you also need the wget tool:
yum install -y wget

安装Python

下载python2.7并安装

# Get Python 2.7.14:
wget http://python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

安装Pip

# First get the script:
wget https://bootstrap.pypa.io/get-pip.py
# Then execute it using Python 2.7
python2.7 get-pip.py
# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]

创建虚拟环境

最后可以利用venv创建一个虚拟环境(毕竟Python2.6你不能卸载)

# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv
virtualenv my27project
试一下?
# Check the system Python interpreter version:
python --version
# This will show Python 2.6.6
# Activate the my27project sandbox:
source my27project/bin/activate
# This will show Python 2.7.4
python --version

总结

以上所述是小编给大家介绍的在CentOS6上安装Python2.7的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

深入解析Python中的lambda表达式的用法

普通的数学运算用这个纯抽象的符号演算来定义,计算结果只能在脑子里存在。所以写了点代码,来验证文章中介绍的演算规则。 我们来验证文章里介绍的自然数及自然数运算规则。说到自然数,今天还百度了...

Python字符串格式化的方法(两种)

本文介绍了Python字符串格式化,主要有两种方法,分享给大家,具体如下 用于字符串的拼接,性能更优。 字符串格式化有两种方式:百分号方式、format方式。 百分号方式比较老,而for...

Python实现一个简单的验证码程序

  老师讲完random函数,自己写的,虽然和老师示例的不那么美观,智能,但是也自己想出来的,所以记录一下,代码就需要自己不断的自己练习,实战,才能提高啊!不然就像我们这些大部分靠自学的...

Python实现随机取一个矩阵数组的某几行

废话不多说了,直接上代码吧! import numpy as np array = np.array([0, 0]) for i in range(10): array =...

Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

去空格及特殊符号 s.strip().lstrip().rstrip(',') Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 复制字符串 #s...