install Python 2.7 and Python 3.3 on CentOS 6
来自:http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
In this guide I will show you how to install Python 2.7 and 3.3 on CentOS 6. The examples below are for Python 2.7.6 and Python 3.3.5, but the procedure is the same for any modern version of Python including the upcoming Python 3.4.0.
I make regular updates to this guide to track new versions. Please see the end of the document for a changelog.
CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example yum, will break if the default Python interpreter is upgraded. The trick is to install new versions of Python in /usr/local (or some other non-standard location) so that they can live side-by-side with the system version.
This guide should work for all versions of CentOS 6, but I have only verified it on CentOS 6.5 64 bit. It will probably work for some versions of CentOS 5 also.
Execute all the commands below as root either by logging in as root or by using sudo.
Preparations – install prerequisites
In order to compile Python you must first install the development tools and a few extra libs. The extra libs are not strictly needed to compile Python but without them your new Python interpreter will be quite useless.
yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
Things to consider
Before you compile and install Python there are a few things you should know and/or consider:
Unicode
Python has a long and complicated history when it comes to Unicode support. Unless you have very specific reasons you should configure Python 3.2 and earlier to enable UTF-32 support. This increases memory usage but improves compatibility. In Python 3.3 the Unicode support has been completely rewritten and strings are automatically stored using the most efficient encoding possible.
You enable UTF-32 in Python 2.7 by adding --enable-unicode=ucs4 to the configure command. In Python 3.2 the flag is called --with-wide-unicode.
Shared library
You should probably compile Python as a shared library. All modern Linux distros ship with Python compiled as a shared library, and there are third-party tools such as mod_wsgi and Blender that won’t work without it. If you compile Python as a shared library you must also tell it how to find the library. You have two options:
- Compile the path into the executable by adding this to the end of the configure command:
LDFLAGS="-Wl,-rpath /usr/local/lib" - Open the file
/etc/ld.so.confin a text editor and add the path/usr/local/libto the end of it. After you have added the line you must run/sbin/ldconfigto make the dynamic linker aware of the change. This is how the file will look after adding the line on a clean install of CentOS 6.5:/etc/ld.so.conf12include ld.so.conf.d/*.conf/usr/local/lib
Use “make altinstall” to prevent problems
It is critical that you use make altinstall when you install your custom version of Python. If you use the normal make install you will end up with two different versions of Python in the filesystem both named python. This can lead to problems that are very hard to diagnose.
Download, compile and install Python
Here are the commands to download, compile and install Python. If you modify /etc/ld.so.conf as discussed above you can remove the LDFLAGS parameter below.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# Python 2.7.6:
wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
tar xf Python-2.7.6.tar.xz
cd Python-2.7.6
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
# Python 3.3.5:
wget http://python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xf Python-3.3.5.tar.xz
cd Python-3.3.5
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
|
After running the commands above your newly installed Python interpreter will be available as/usr/local/bin/python2.7 or /usr/local/bin/python3.3. The system version of Python 2.6.6 will continue to be available as /usr/bin/python, /usr/bin/python2 and /usr/bin/python2.6.
Download and install Setuptools + pip
Setuptools has replaced Distribute as the official package manager used for installing packages from the Python Package Index. Each Python interpreter on your system needs its own install of Setuptools. I also suggest you install pip. It builds on top of Setuptools and provides a few extra functions that are useful when you manage your packages.
The instructions below will install the latest version of Setuptools and pip for you.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
# Then install it for Python 2.7 and/or Python 3.3:
python2.7 ez_setup.py
python3.3 ez_setup.py
# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
easy_install-3.3 pip
# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]
|
The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (where X.Y is the Python version).
What’s next?
If you are using Python 2.7 I strongly recommend that you install virtualenv and learn how to use it. Virtualenv makes it possible to create isolated Python environments. If you are using Python 3.3 then you don’t need virtualenv because that functionality is already built in.
Each isolated Python environment (also called sandbox) can have its own Python version and packages. This is very useful when you work on multiple projects or on different versions of the same project.
Create your first isolated Python environment
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv
virtualenv-2.7 my27project
# Use the built-in pyvenv program in Python 3.3 to create a sandbox called my33project:
pyvenv-3.3 my33project
# Check the system Python interpreter version:
python --version
# This will show Python 2.6.6
# Activate the my27project sandbox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python --version
# This will show Python 2.7.6
deactivate
# Activate the my33project sandbox and check the version of the default Python interpreter in it:
source my33project/bin/activate
python --version
# This will show Python 3.3.5
deactivate
|
When you use virtualenv to create a sandbox it will automatically install setuptools and pip for you inside the sandbox. If you use pyvenv then you must do it yourself. You can reuse the ez_setup.py file you downloaded earlier and just run it after you activate your new sandbox.
install Python 2.7 and Python 3.3 on CentOS 6的更多相关文章
- Install OpenCV 3.0 and Python 2.7+ on OSX
http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...
- Install OpenCV 3.0 and Python 2.7+ on Ubuntu
为了防止原文消失或者被墙,转载留个底,最好还是去看原贴,因为随着版本变化,原贴是有人维护升级的 http://www.pyimagesearch.com/2015/06/22/install-Open ...
- SimpleCV install and "You need the python image library to save by filehandle"
2015年5月3日 22:15:43 在win7下安装了python.simplecv,试着运行simplecv官网第一个hello world程序结果报错,提示说%python%/lib/site- ...
- 在执行 pip install 时遇到错误:python setup.py egg_info ...
最近重新安装win10 64位专业版, 正好遇到python3.8发布,试了一下.结果jupyter都安装不了...心碎. ERROR: Command errored out with exit s ...
- ubuntu下python 2.7与python 3.X的转换
ubuntu下python 2.7与python 3.X的转换 由于ubuntu本身自带python 2.7,而python 3.X与2.7有很多不同,所以在使用python 3.X时会带来诸多不便. ...
- python扩展实现方法--python与c混和编程 转自:http://www.cnblogs.com/btchenguang/archive/2012/09/04/2670849.html
前言 需要扩展Python语言的理由: 创建Python扩展的步骤 1. 创建应用程序代码 2. 利用样板来包装代码 a. 包含python的头文件 b. 为每个模块的每一个函数增加一个型如PyObj ...
- 【Python】我的Python学习笔记【1】【using Python 2】
1.模块格式 #!/usr/bin/env python # -*- coding: utf-8 -*- ... ...def main(): ...... ... if __name__=='__m ...
- python扩展实现方法--python与c混和编程
前言 需要扩展Python语言的理由: 创建Python扩展的步骤 1. 创建应用程序代码 2. 利用样板来包装代码 a. 包含python的头文件 b. 为每个模块的每一个函数增加一个型如PyObj ...
- [Python]linux自己定义Python脚本命令
在window下写好的程序配置到Linux上,要实现随意文件夹下的命令调用. 因为初学Linux,这里从文件传输等最主要的方法入手,记录配置的过程中遇到的各种问题. 连接远端server 这里使用pu ...
随机推荐
- bzoj5138 [Usaco2017 Dec]Push a Box
题目描述: bz luogu 题解: 暴力可以记录$AB$位置转移,这个时候状态是$n^4$的,无法接受. 考虑只记录$A$在$B$旁边时的状态,这个时候状态时$n^2$的. 所以说转移有两种,一种是 ...
- Idea 搭建Maven--web项目(MVC)
小编最近正在学习使用MVC框架,在搭建Maven的项目过程中,遇到了很多问题,上网搜了很多材料才找到答案,为了小编以后查起来方便,也为了向广大小伙伴分享,写了这部片博文,敬我昨天一天的学习结果! 步骤 ...
- CSS3的渐变-gradient
CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变). CSS3的线性渐变 一.线性渐变在Mozilla下的应用 语法: -moz-li ...
- python-数字类型内置方法
数字类型内置方法 为什么要有数据类型? 数据是用来表示状态的,不同的状态就应该用不同的数据类型去表示 整型(int) 用途:年龄.号码.银行卡号等 定义:可以使用int()方法将纯数字的字符串转换为十 ...
- Linux文件管理类命令及命令别名
文件查看类命令: cat: tac: 从文件尾部开始显示 分屏显示: more [option] 文件名: 查看至文件尾部会退出 空格为翻页 less [option] 文件名: 查看至文件尾部不退出 ...
- 关于Linux下的环境变量
一.交互式shell和非交互式shell 要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是login shell 和non-login shel ...
- 在web中绘制图像 -- canvas篇
汗,不小心点击发布了.其实正在编辑中...... HTML Canvas是现代浏览器中非常棒的绘图技术,Canvas可以绘制图形,操作图片,制作游戏,创建动画等:Canvas是很容易使用的,下面我们来 ...
- IOS 自动布局-UIStackPanel和UIGridPanel(一)
我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...
- 大数据学习——本地安装redis
下载安装包 https://github.com/MicrosoftArchive/redis 下载后解压 运行cmd 然后到redis路径 运行命令: redis-server redis.wind ...
- Leetcode 322.零钱兑换
零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...