How to install Python 2.7 and Python 3.3 on CentOS

Source: Internet
Author: User
Tags virtualenv

In this guide I'll show you how to install the Python 2.7 and 3.3 on CentOS 6. The examples below is for Python 2.7.6 and Python 3.3.5, but the procedure was the same for any modern version of Python I Ncluding 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 , would break if the default Pytho N 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-b Y-side with the system version.

This guide should work for all versions of the CentOS 6, but I had only verified it on the CentOS 6.5-bit. It'll 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 must first install the development tools and a few extra libs. The extra libs is not strictly needed to compile python but without them your new Python interpreter would be quite useles S.

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 is a few things you should know and/or consider:

Unicode

Python has a long and complicated the history of it comes to Unicode support. Unless you has 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 have been completely rewritten and strings is automatically stored using the most Effici ENT encoding possible.

You enable UTF-32 in Python 2.7 by adding to the --enable-unicode=ucs4 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 is third-party tools such as Mod_wsgi a nd Blender that won ' t work without it. If you compile Python as a GKFX library you must also tell it what to find the library. You have the options:

    • Compile the path into the executable by adding the end of the Configure command:LDFLAGS="-Wl,-rpath /usr/local/lib"

    • Open the file in /etc/ld.so.conf a text editor and add the path to the /usr/local/lib end of it. After you has added the must run to make the /sbin/ldconfig Dynamic linker aware of the change. This is how the file would 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's when you make altinstall install your custom version of Python. If you use the normal you'll end up with a make install different versions of Python in the filesystem both named python . This can leads to problems that is very hard to diagnose.

Download, compile and install Python

Here is the commands to download, compile and install Python. If you modify as /etc/ld.so.conf 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 would be available as /usr/local/bin/python2.7 or /usr/local/bin/python3.3 . The system version of Python 2.6.6 would continue to be available /usr/bin/python as, /usr/bin/python2 and /usr/bin/python2.6 .

Download and install Setuptools + PIP

Setuptools have replaced distribute as the official Package Manager used for installing packages from the Python package in Dex. Each Python interpreter in 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 is useful when you manage your packages.

The instructions below would 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 I Nstall 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 your can now does 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 your can now does things like this:

PIP2 . 7 install [packagename]

PIP2 . 7 Install -- upgrade [PackageName ]

PIP2 . 7 Uninstall [packagename]

The packages would end up in /usr/local/lib/pythonX.Y/site-packages/ (where is the X.Y Python version).

What ' s next?

If you are using the Python 2.7 I strongly recommend that's install virtualenv and learn how to use it. Virtualenv makes it possible to create isolated Python environments. If you're 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 has its own Python version and packages. This is very useful if 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 my27 Project # Use the built-in pyvenv program in Python 3.3 to create a sandbox called my33project:pyvenv-3.3 My33project # C Heck the system Python interpreter Version:python--version # This'll show Python 2.6.6 # Activate the My27project Sand box and check the version of the default Python interpreter in It:source my27project/bin/activate python--version # Would show Python 2.7.6 Deactivate # Activate the My33project sandbox and check the version of the default Python interpre ter in It:source my33project/bin/activate python--version # This'll 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

21st

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'll 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'll 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'll show Python 3.3.5

Deactivate

When you use Virtualenv to create a sandbox it'll 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, 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 fil E or directory
                                            
That means the system doesn't know which xxx.so to put on.                    
This is the time to add Xxx.so's/etc/ld.so.conf to your account.                     
In general, there are many so files in the/usr/local/lib, so in/etc Adding/usr/local/lib to this line in/ld.so.conf can solve this problem.
will be/etc/ld.so.conf after the file is "/sbin/ldconfig–v" to perform the update before it takes effect /span>

< Span style= "line-height:25px" >/etc/ld.so.conf:


If you have some libraries installed,

Ldconfig is a thing:
It is a program that is usually located under/sbin and is used by the root user. The specific function and usage can be man ldconfig find
To put it simply, it is to cache the library file under the path listed in/etc/ld.so.conf to/etc/ld.so.cache for use
So when you're done installing some library files (for example, just installing glib) or modifying ld.so.conf to add a new library path, you need to run/sbin/ldconfig
So that all library files are cached in the Ld.so.cache, if not done, even if the library file is clearly under the/usr/lib, it will not be used, the result
The compilation process is wrong, missing XXX library.


How to install Python 2.7 and Python 3.3 on CentOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.