Search code examples
pythondebianeasy-install

Migrating default python installation from 2.6 to 2.7: problems with easy_install


I'm using debian with a default python installation of 2.6 I wanted to migrate to python 2.7, including installing easy_install. I followed someone else's instruction to delete my /usr/bin/python, then link

ln -s /usr/bin/python2.7 /usr/bin/python

I downloaded the most recent version of setuptools

and cd'ed to the file. The install help asked me to run it as a shell program, I did, with the following error:

sh setuptools-0.6c11-py2.7.egg
Traceback (most recent call last):
    File "<string>", line 1 in <module>
ImportError: No Module named setuptools.command.easy_install

I have a feeling that my installation of easy_install is related to the version of python I'm running, but I can't quite figure it out. I tried also downloading the .tar.gz file, cd-ing into the directory, and running

python setup.py build; setup.py install

After I run that, I can use easy_install, with the following error:

Traceback (most recent call last):
  File "/usr/local/bin/easy_install", line 5, in <module>
    from pkg_resources import load_entry_point
ImportError: No Module named pkg_resources

Can anyone suggest to me a solution? Thanks for the help.


Solution

  • Replacing the system Python isn't a great idea. Moving up by one point release probably won't break your system, but who wants to take the risk? Installing libraries to your system Python using easy_install can also lead to problems if they conflict with each other.

    An alternative is to build Python from source, install it to your home directory, and use virtualenv to create isolated environments into which you can install whatever libraries you need for a given project using pip (which is the more modern equivalent to easy_install).

    For Python 2.7, if you want some of the 'optional' parts of the standard library, that means building a couple of other things, too. Here's a script (largely cobbled together from blog posts scattered across the interweb) that works for me on Debian "Squeeze" (stable, at the time of writing):

    #!/bin/bash -e
    
    # Setup
    sudo aptitude install build-essential
    mkdir -p ${HOME}/.local
    mkdir build-python
    cd build-python
    
    # Get sources
    
    ### Tcl/Tk <http://www.tcl.tk/software/tcltk/download.html>
    wget "http://downloads.sourceforge.net/project/tcl/Tcl/8.5.13/tcl8.5.13-src.tar.gz"
    wget "http://downloads.sourceforge.net/project/tcl/Tcl/8.5.13/tk8.5.13-src.tar.gz"
    
    ### Berkeley DB <http://www.oracle.com/technetwork/products/berkeleydb/downloads/index-082944.html>
    wget "http://download.oracle.com/berkeley-db/db-4.8.30.tar.gz"
    
    ### Python <http://www.python.org/download/>
    wget "http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz"
    
    # Build Tcl
    tar xzf tcl8.5.13-src.tar.gz
    cd tcl8.5.13/unix
    ./configure --prefix=${HOME}/.local
    make
    make install
    cd ../..
    
    # Build Tk
    tar xzf tk8.5.13-src.tar.gz
    cd tk8.5.13/unix
    ./configure --prefix=${HOME}/.local
    make
    make install
    cd ../..
    
    # Build Berkeley DB 4.8
    tar xzf db-4.8.30.tar.gz
    cd db-4.8.30/build_unix
    ../dist/configure --prefix=${HOME}/.local/opt/BerkeleyDB.4.8 --enable-tcl --with-tcl=${HOME}/.local/lib
    make
    make install
    cd ../..
    
    # Set compile flags
    export LDFLAGS="-L${HOME}/.local/lib -L${HOME}/.local/opt/BerkeleyDB.4.8/lib"
    export CPPFLAGS="-I${HOME}/.local/include -I${HOME}/.local/opt/BerkeleyDB.4.8/include"
    export CXXFLAGS=${CPPFLAGS}
    export CFLAGS=${CPPFLAGS}
    export LD_LIBRARY_PATH=${HOME}/.local/lib:${HOME}/.local/opt/BerkeleyDB.4.8/lib
    export LD_RUN_PATH=${LD_LIBRARY_PATH}
    
    # Build Python 2.7
    tar xzf Python-2.7.3.tgz
    cd Python-2.7.3
    ./configure --prefix=${HOME}/.local
    make
    make altinstall
    cd ..
    
    # Install virtualenv, pip and virtualenvwrapper
    curl http://python-distribute.org/distribute_setup.py | ${HOME}/.local/bin/python2.7
    curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | ${HOME}/.local/bin/python2.7
    ${HOME}/.local/bin/pip install virtualenvwrapper
    
    # Update ~/.bashrc
    echo 'export PATH="${HOME}/.local/bin:${PATH}"' >> ${HOME}/.bashrc
    echo 'export WORKON_HOME="${HOME}/.local/virtualenv"' >> ${HOME}/.bashrc
    echo 'export VIRTUALENVWRAPPER_PYTHON="${HOME}/.local/bin/python2.7"' >> ${HOME}/.bashrc
    echo 'export VIRTUALENVWRAPPER_VIRTUALENV="${HOME}/.local/bin/virtualenv"' >> ${HOME}/.bashrc
    echo 'export VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--python=python2.7"' >> ${HOME}/.bashrc
    echo 'source ${HOME}/.local/bin/virtualenvwrapper.sh' >> ${HOME}/.bashrc
    
    # Finish ...
    cd ..
    echo -e "\n\n ... Done!"
    

    This script will ask for your password in order to install GCC etc. if it needs to, then take a while to build everything - about 25 minutes on my (ancient) machine and (terrible) internet connection. If you're paying attention, at some point you'll see the following message scroll past:

    Python build finished, but the necessary bits to build these modules were not found:
    bsddb185           dl                 imageop         
    sunaudiodev                                           
    To find the necessary bits, look in setup.py in detect_modules() for the module's name.
    

    Those four modules are archaic and/or deprecated, so you don't need to worry about them. If the message mentions any other modules, that means some necessary library isn't installed on your system. You can still run Python if that's the case, but won't be able to import those modules. Shout in the comments if you're affected by this, and I'll update the script accordingly.

    Once that's finished successfully, you need to source your .bashrc:

    $ source ~/.bashrc
    

    ... and you'll then be able to run your newly compiled Python ...

    $ python2.7
    Python 2.7.3 (default, Nov 17 2012, 02:00:26) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    ... create a virtualenv to work in ...

    $ mkvirtualenv my_env
    $ python
    Python 2.7.3 (default, Nov 17 2012, 02:00:26) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    ... install libraries into it ...

    $ pip install beautifulsoup4
    

    ... and so on.

    To exit a virtualenv:

    $ deactivate
    

    To re-enter it at a later date:

    $ workon my_env
    

    For more, check out the documentation for pip and virtualenvwrapper.