Python easy_install in a virtualenv gives setuptools error

Graeme Perrow picture Graeme Perrow · Feb 12, 2014 · Viewed 8.4k times · Source

There are a number of other StackOverflow questions similar to this one, but in each case, the platform was different or the error message was different or the solution had no effect or was outdated. I am trying to set up a Python 2.7.6 virtualenv and install modules into it but easy_install gives me errors indicating that setuptools is not available. But AFAIK easy_install is part of setuptools, so this makes no sense.

The problem only happens in a virtualenv. Here's what I've done:

  • Created a brand new Red Hat 5 virtual machine
  • Did a yum -y update to get the latest stuff, rebooted
  • Downloaded Python-2.7.6.tar.gz, unzipped, ./configure; make; sudo make install
  • Confirmed that python -V gives me 2.7.6 and sudo python -V also gives me 2.7.6
  • wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
  • Modified ez_setup.py to add the --no-check-certificate flag to wget to get around proxy server problems in our network
  • sudo python ez_setup.py
  • sudo easy_install pip
  • sudo pip install virtualenv
  • virtualenv virtpy
  • . virtpy/bin/activate
  • easy_install elementtree

All of these steps succeed except for the last one, which fails with:

Traceback (most recent call last):
  File "/home/gperrow/virtpy/bin/easy_install", line 7, in <module>
    from setuptools.command.easy_install import main
  File "/home/gperrow/virtpy/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 44, in <module>
    from setuptools.package_index import PackageIndex
  File "/home/gperrow/virtpy/lib/python2.7/site-packages/setuptools/package_index.py", line 203, in <module>
    sys.version[:3], require('setuptools')[0].version
  File "/usr/local/bin/scripts/pkg_resources.py", line 584, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/bin/scripts/pkg_resources.py", line 482, in resolve
    raise DistributionNotFound(req)  # XXX put more info here
pkg_resources.DistributionNotFound: setuptools

I'm starting with a clean VM and I've done nothing really unusual but I'm finding "easy_install" anything but. Am I doing something wrong or am I missing one or more steps?

Answer

Dr. Jan-Philip Gehrcke picture Dr. Jan-Philip Gehrcke · Feb 17, 2014

I can't tell why exactly you get errors, but I am confident that there is a systematic approach that lets you cleanly install your custom Python including a working pip and virtualenv. In the following, I describe the procedure that I would use.

First of all, leave your system's Python untouched, for a number of reasons. One of them is that parts of your Linux distribution might depend on the specifics of its default Python. You don't want to break these parts. Another reason is that the vanilla Python installed to default locations might become confused by residuals of the original Python (distributions may have a specific Python/dist-packages/site-packages directory layout that differs from the vanilla one). This might or might not be a real problem in practice -- you can conceptually prevent these issues by not overwriting the system's Python. Another argument is that there is no need to install Python 2.7.6 as root. Install it as unprivileged user (called 'joe' from here on) and put it into /opt or something. This would be a clean start.

After having set up your custom Python, create yourself a little shell script, e.g. setup.sh that sets up the environment for using your custom Python version. Make sure to adjust and clean up the environment. Obviously, this especially affects PATH and PYTHONPATH. I would make sure that PYTHONPATH is unset and that PATH properly points to the custom install. Look at env and try to identify if there is anything left that might configure python in unexpected ways. After all, make sure that

$ command -v python
$ python -v

, executed as joe, look right.

Still being joe and under the proper environment, install pip for the custom Python. According to http://pip.readthedocs.org/en/latest/installing.html, download https://raw.github.com/pypa/pip/master/contrib/get-pip.py and execute it: python get-pip.py. Validate that it installed properly and that your environment is still right:

$ command -v pip
/CUSTOM/PYTHON/bin/pip

$ pip --version
pip 1.x.x from /CUSTOM/PYTHON/lib/python2.7/site-packages

At this point you should make sure that your environment does not contain any VIRTUALENV_* variables (which might have been set by your distribution or whatever component (unlikely, but worth checking)). If any VIRTUALENV_* variable is set, it most likely configures virtualenv in an unexpected way. Get rid of this (unset, or change). Then go ahead and install virtualenv into your new Python with the new pip, via pip install virtualenv. It might also be worth a try to install the latest development version of virtualenv via pip install https://github.com/pypa/virtualenv/tarball/develop.

Create and activate a new virtual environment. Using command -v pip, verify that pip comes from the virtual environment. Then install your custom package(s).

Note: I would definitely use pip to install things to the new virtual environment, and not easy_install, if possible. pip will quite soon be the official installer tool (it will be included with Python 3.4). If for some reason you really depend on easy_install, this should be possible (the easy_install command is provided by the virtual environment), but just to be sure you should also verify this via command -v easy_install.