Install Python 2.7 and Python 3.3 on CentOS 6

Source: Internet
Author: User
Tags virtualenv

From: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

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

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

12345678910111213 # 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.

123456789101112131415 # First get the setup script for Setuptools:wget HTTPS://bitbucket. org/pypa/setuptools/Raw/ Bootstrap/ez_setup. py # Then install the 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

12345678910111213141516171819202122 # 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.6Deactivate# 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.5Deactivate

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.

Install Python 2.7 and Python 3.3 on CentOS 6

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.