• How to install Python 2.7 and Python 3.3 on CentOS 6


    refer to:http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/
    How to install Python 2.7 and Python 3.3 on CentOS 6

    This guide shows you how to install Python 2.7 and/or Python 3.3 on any version of CentOS 6. It also includes instructions for setuptools, pip, virtualenv and pyvenv.

    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 exampleyum, 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 usingsudo.
    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-d

       yumgroupinstall"Development tools"
       yuminstallzlib-develbzip2-developenssl-develncurses-develsqlite-develreadline-develtk-develgdbm-develdb4-devellibpcap-develxz-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
    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 normalmake install you will end up with two different versions of Python in the filesystem both namedpython. 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

    
    # 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&&makealtinstall

     
    # 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&&makealtinstall

    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 replacedDistribute as the official package manager used for installing packages from thePython Package Index. Each Python interpreter on your system needs its own install of Setuptools. I also suggest you installpip. 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]
    
    	
    # First get the setup script for Setuptools:
    wgethttps://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
     
    # Then install it for Python 2.7 and/or Python 3.3:
    python2.7ez_setup.py
    python3.3ez_setup.py
     
    # Now install pip using the newly installed setuptools:
    easy_install-2.7pip
    easy_install-3.3pip
     
    # With pip installed you can now do things like this:
    pip2.7install[packagename]
    pip2.7install--upgrade[packagename]
    pip2.7uninstall[packagename]
    
    The packages will end up in /usr/local/lib/pythonX.Y/site-packages/ (whereX.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
    
    	
    # Install virtualenv for Python 2.7 and create a sandbox called my27project:
    pip2.7installvirtualenv
    virtualenv-2.7my27project
     
    # Use the built-in pyvenv program in Python 3.3 to create a sandbox called my33project:
    pyvenv-3.3my33project
     
    # 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:
    sourcemy27project/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:
    sourcemy33project/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.
    Yesterday is history.
    Tomorrow is a mystery.
    But today is a gift.
    That is why it's called the present.
    The old game: give a wolf a taste, then keep him hungry.
  • 相关阅读:
    Requests库入门——应用实例-网络图片的爬取与保存(好看的小姐姐≧▽≦)
    Python Requests库简单入门
    Python BeautifulSoup库基础及一般元素提取方法
    Coloring a Tree(耐心翻译+思维)
    Python jieba库的使用说明
    竖式除法模拟
    Pythagorean Triples毕达哥斯拉三角(数学思维+构造)
    C++ STL中的Binary search(二分查找)
    Polycarp and Letters(set首战!)
    Java多线程干货系列—(一)Java多线程基础
  • 原文地址:https://www.cnblogs.com/ZhangRuoXu/p/6369272.html
Copyright © 2020-2023  润新知