Search code examples
pythonpip

Install a module using pip for specific python version


On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.

For example:

pip install beautifulsoup4

by default installs BeautifulSoup for Python 2.6

When I do:

import bs4

in Python 2.6 it works, but in Python 2.7 it says:

No module named bs4

Solution

  • Use a version of pip installed against the Python instance you want to install new packages to.

    In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).

    pip's website includes installation instructions, if you can't find anything within your distribution.


    Update: In the modern (2020s) world, python -m pip is the right way to do this, where python can be replaced with whichever interpreter you choose.