How to install Python 2.7 and Python 3.3 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


1

2


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.conf in a text editor and add the path /usr/local/lib to the end of it. After you have added the line you must run /sbin/ldconfig to 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.conf

    include ld.so.conf.d/*.conf /usr/local/lib


    1

    2


    include 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.

# 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


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.

# 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]


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

# 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


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.

-----------------

error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or directory
                                            
那就表示系統不知道xxx.so 放在哪個目錄下。                   
這個時候就要在/etc/ld.so.conf中加入xxx.so所在的目錄。                    
一般而言,有很多so檔會在/usr/local/lib這個目錄下,所以在/etc/ld.so.conf中加入/usr/local/lib這一行,可以解決此問題。
將 /etc/ld.so.conf存檔後,還要執行「/sbin/ldconfig –v」來更新一下才會生效

/etc/ld.so.conf:
这个文件记录了编译时使用的动态链接库的路径。
默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件
如果你安装了某些库,没有指定 --prefix=/usr 这样lib库就装到了/usr/local下,而又没有在/etc/ld.so.conf中添加/usr/local/lib,就会报错了

ldconfig是个什么东东吧 :
它是一个程序,通常它位于/sbin下,是root用户使用的东东。具体作用及用法可以man ldconfig查到
简单的说,它的作用就是将/etc/ld.so.conf列出的路径下的库文件 缓存到/etc/ld.so.cache 以供使用
因此当安装完一些库文件,(例如刚安装好glib),或者修改ld.so.conf增加新的库路径后,需要运行一下/sbin/ldconfig
使所有的库文件都被缓存到ld.so.cache中,如果没做,即使库文件明明就在/usr/lib下的,也是不会被使用的,结果
编译过程中抱错,缺少xxx库。

时间: 2024-10-17 05:06:34

How to install Python 2.7 and Python 3.3 on CentOS的相关文章

Install OpenCV in Windows for Python

Here I will tell you how to install OpenCV 2.4x in Windows  for Python 2.7. Pre-requisites ( need to be downloaded ) : Python : Download latest version of Python 2.7 from Python site.Numpy : Download Numpy for Python 2.7 from here.OpenCV 2.4 : Downlo

Learn Python From 'Head First Python' [0] : Install

1. download software form the python‘s webset 2. install the python and set the class path 3. verify if the install is correct PS: for Step 2. if you use the default install. then undre the disk C:, there will be a dir like Python34. go to the env se

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

python基础教程:python的环境搭建

Python编程语言可应用于多平台包括 Linux 和 Mac OS X.一般的 Linux 发行版本都自带 Python,Mac OS X 最新版也自带了 Python,也就是已经安装好了,不需要再配置. Windows 下直接下载最新版的 Python 2.7.9,安装的时候注意选择 你可以通过终端窗口输入 "python" 命令来查看本地是否已经安装Python以及Python的安装版本. Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, Su

python扩展实现方法--python与c混和编程

Reference: http://www.cnblogs.com/btchenguang/archive/2012/09/04/2670849.html python 头文件在的位置:/usr/include/python2.7                                  /usr/local/include/python2.7 前言(更新:更方便易用的方式在http://www.swig.org/tutorial.html) 大部分的Python的扩展都是用C语言写的,

初窥Python(三)——python版本升级及ipython的安装使用

在使用 CentOS6.6 X64 系统时,由于系统自带的 python 版本为 2.6.6,而 2.x 版本中当前普遍使用的为2.7 版本,所以我们要对系统的 python 版本做一个升级.ipython 是一个增强版的shell,支持TAB补全,自动缩进等供能,比默认的 python shell 要好用很多.下面主要介绍如何升级 python 版本,安装 pip 并使用 pip install 安装 ipython,搭建一个简易的 python 环境. 1.官网下载安装包: [[email 

Python学习之使用Python操作Redis数据库

最近在写一个检查一台服务器上所有游戏区服配置文件中redis某个key值大小的脚本,本打算使用shell+awk+sed的方式去解决这个问题,但是由于redis的配置信息是php数组形式.shell脚本一时没有写出来,就请教他人帮忙写了个python脚本,但是自己python不是很精通,于是按照脚本中涉及到的python知识现学现用,然后根据自己的需求更改脚本.这里分享一下如何使用python操作redis数据库. Redis的Python驱动源码下载地址是https://github.com/

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时会带来诸多不便.所以在此记录下ubuntu下 python 2.7与python 3.X的转换的方法: 先下载python3 sudo add-apt-repository ppa:fkrull/deadsnakes #添加一个源 sudo apt-get update #更新源列表,以获取最新的版本 sudo a

【python】prepare for python env

////// ////// install pip ////// [[email protected] tools]# python get-pip.py /tmp/tmp4BEPxg/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from