227

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
rubo77
  • 19,527
  • 31
  • 134
  • 226
torayeff
  • 9,296
  • 19
  • 69
  • 103

18 Answers18

338

Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:

python2.7 -m pip install foo
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Turion
  • 5,684
  • 4
  • 26
  • 42
  • 2
    As I am installing with pip in a virtual environment (but need to run it against the system python in some instances, with PYTHONPATH set - e.g. when using anything that depends on wxPython), this is the command that worked for me. – Aaron D Feb 17 '15 at 02:12
  • 9
    BEST Solution to all Python2 and Python3 problems! – Harsh Vardhan Ladha Dec 02 '15 at 13:49
  • This is a good answer. Now I only have to add a reference from the "pip" command call to the actual binary you want to run. Am I right? –  Jan 05 '16 at 21:49
  • 9
    I should note that the right way to call a module in python is with `python 2.7 -m pip install foo` – llrs Feb 22 '16 at 16:45
  • 2
    I can't upvote this answer enough. Finally solved this painful problem on DietPi (Raspberry distro) – Pitto Oct 21 '16 at 13:47
  • 1
    I'm quite surprised this answer gets so many upvotes. I actually don't really understand why it works. – Turion Oct 21 '16 at 16:10
  • I have the same problem and followed the example given and get this error: sudo python2.7 /Users/xxx/Library/Caches/pip install openpyxl /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't find '__main__' module in '/Users/xxx/Library/Caches/pip' – user3920295 Dec 28 '16 at 21:26
  • this helped a lot, I was looking for this for ages - thanks – splaisan Aug 09 '18 at 06:55
  • 1
    @Turion it works because the packages you installed are stored in a different spot for each version of python, and if you just do something like `pip install package` it will install that package for whatever version of python happens to be symlinked into the bin directory, which may or may not be the version of python you want to use for your development, e.g.: some versions of Linux still ship with python redirecting to python2.7, but you might be developing a python3 application. If you just did `pip install package` that package would be installed for python2.7 but not anything else. – jrh Jan 29 '19 at 13:21
  • ...I think this is because Python is still an evolving language, in theory maybe Python3.6 is backwards compatible with Python3.5 but this setup is needed because Python3.5 couldn't load a Python3.6 library. – jrh Jan 29 '19 at 13:54
  • 1
    Admin, please mark this as the accepted answer! This solves the OP problem, not the currently selected accepted answer.. – Shailen Feb 13 '19 at 17:50
  • I'm trying to do the same (on Windows) except that apparently pip does not accept my arguments. Im typing `py37 "location-of-pip" -m install numpy` And I get the help page. Any clues? – Sergio Jul 18 '19 at 06:48
  • 1
    @Sergio It looks like the -m is in the wrong place to me, I think the command should be more like `py37 -m "location-of-pip" install numpy` – jrh Jul 31 '19 at 17:31
  • 1
    this worked: `py -2 -m pip install matplotlib`. You solution gave me an error. – amalik2205 Mar 17 '20 at 17:02
  • This solution worked perfectly. Just to mention that I think that we should add -m to work properly. – ppel123 Jan 12 '21 at 21:58
  • This does not work. – NL23codes Oct 08 '21 at 16:50
  • `ModuleNotFoundError: No module named 'distutils.util'` – cactuschibre Oct 02 '22 at 11:36
  • Does not work. 'python3.7' is not recognized as an internal or external command, operable program or batch file. – zdarova_koresh Jan 25 '23 at 14:48
74

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.

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 9
    I do not have neither pip-2.6 nor pip-2.7 – torayeff Jun 06 '12 at 18:28
  • 1
    @torayeff Did you look for version-specific pip packages? Packaging is distribution-specific, so you're going to need to either do a little legwork yourself (searching through the available package listings) or ask an Ubuntu-specific support channel. – Charles Duffy Jun 06 '12 at 18:31
  • 2
    I tried this easy_install -d /usr/local/lib/python2.7/ beautifulsoup4, but it does not work, what do you mean by legwork? – torayeff Jun 06 '12 at 18:34
  • 3
    @torayeff the same thing applies to easy_install as to pip -- you should use a version built against the interpreter you're trying to install packages for. "legwork" is a colloquialism referring to effort, typically of a variety that isn't particularly intellectually challenging but may be time consuming (think akin to a job that requires time to be spent walking around)... in this case, reviewing the Ubuntu package listings. – Charles Duffy Jun 06 '12 at 21:27
  • 1
    @torayeff ...and again, if you can't find an Ubuntu package of pip or easy-install specific to Python 2.7, you can install pip by hand by the instructions I linked to in my answer; just be sure to use the `python2.7` executable when going through those instructions. – Charles Duffy Jun 06 '12 at 21:30
  • 1
    For me on CentOS 7 the executable name is pip3.7 rather than pip-3.7 – monotasker Aug 19 '19 at 19:18
  • to install pip in the version you want: python3.VERSIONYOUINSTALLED -m ensurepip and then you can use with python3.VERSIONYOUINSTALLED -m pip install PACKAGEYOUWANT – Allexj Jan 11 '23 at 16:48
61

You can execute pip module for a specific python version using the corresponding python:

Python 2.6:

python2.6 -m pip install beautifulsoup4

Python 2.7

python2.7 -m pip install beautifulsoup4
Tiago Coutinho
  • 1,618
  • 15
  • 17
  • 3
    I scoured the internet many times, searching for this command. I guess most people use a virtual environment to solve this problem, but is a much more viable strategy to me. – Dave Liu Jun 14 '16 at 02:10
  • What if I am inside my python script that is calling pip install beautifulsoup4. How can I make sure that the script was called with the -m flag? – Ogen Jan 26 '17 at 21:13
  • This was the final piece of my puzzle, thank you! `pip` and `pip3` were both locked to python3.X for me so I had to use [this method](https://groups.google.com/d/msg/obey-the-testing-goat-book/RHCR1t7mxgo/uyMUz3D5oysJ) as a roundabout way of installing pip for python2.7 – airdas Sep 28 '17 at 16:17
  • 10
    `py -3.8 -m pip install beautifulsoup4` works for me – Hzzkygcs Oct 04 '21 at 09:14
  • to install pip in the version you want: python3.VERSIONYOUINSTALLED -m ensurepip and then you can use with python3.VERSIONYOUINSTALLED -m pip install PACKAGEYOUWANT – Allexj Jan 11 '23 at 16:48
35

In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )

py -2 -m pip install pyfora
Richard
  • 56,349
  • 34
  • 180
  • 251
mon
  • 2,167
  • 1
  • 12
  • 6
29

You can use this syntax

python_version -m pip install your_package

For example. If you're running python3.5, you named it as "python3", and want to install numpy package

python3 -m pip install numpy
Chau Pham
  • 4,705
  • 1
  • 35
  • 30
20

Have tried this on a Windows machine and it works

If you wanna install opencv for python version 3.7, heres how you do it!

py -3.7 -m pip install opencv-python
Anurag Bhalekar
  • 830
  • 7
  • 9
6

Alternatively, if you want to install specific version of the package with the specific version of python, this is the way

sudo python2.7 -m pip install pyudev=0.16

if the "=" doesnt work, use ==

x@ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16

Invalid requirement: 'pyudev=0.16' = is not a valid operator. Did you mean == ?

x@ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16

works fine

ravi.zombie
  • 1,482
  • 1
  • 20
  • 23
  • 1
    To any future visitors, please see [this warning from the makers of pip](https://github.com/pypa/pip/issues/5599) on why you should **never** use `sudo` with pip. If you think you need to use sudo, you're probably about modify system-owned files which are only intended to be modified by your system's package manager. Prefer installing packages to your user directories by passing `--user`, or even better use a virtual environment. – Brian61354270 May 14 '22 at 22:14
  • to install pip in the version you want: python3.VERSIONYOUINSTALLED -m ensurepip and then you can use with python3.VERSIONYOUINSTALLED -m pip install PACKAGEYOUWANT – Allexj Jan 11 '23 at 16:49
4

If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".

Bhaskar Bhuyan
  • 501
  • 6
  • 16
3

Python 2

sudo pip2 install johnbonjovi  

Python 3

sudo pip3 install johnbonjovi
  • 4
    This doesn't work when you have multiple installations of `python3`. I have `3.4` and `3.5`, and I cannot `pip install` anything for `3.4`. – byxor Apr 20 '17 at 19:11
  • To any future visitors, please see [this warning from the makers of pip](https://github.com/pypa/pip/issues/5599) on why you should **never** use `sudo` with pip. If you think you need to use sudo, you're probably about modify system-owned files which are only intended to be modified by your system's package manager. Prefer installing packages to your user directories by passing `--user`, or even better use a virtual environment. – Brian61354270 May 14 '22 at 22:15
3

To install the module, first locate the folder in which that version of Python is located (Open a cmd window and type where python to list all paths for python installations), copy the path to the preferred version, then, from a command line interface, navigate to the same folder and run the pip install command. For example:

cd "c:\users\name_of_python_folder\python.exe" -m pip install beautifulsoup

This should work properly.

Tpeazy1
  • 43
  • 4
2

For Python 3

sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4

For Python 2

sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4

On Debian/Ubuntu, pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
  • 1
    this answer falls when you both have python3.6 and python3.7 for example. Then pip3 will install packages only for default python3. – muyustan Mar 22 '20 at 16:16
  • @muyustan you can simply do `python3.x -m pip install beautifulsoup4` and replace the `x` to the wanted version example: `python3.7 -m pip install beautifulsoup4` – Zeperox Apr 06 '21 at 06:40
  • To any future visitors, please see [this warning from the makers of pip](https://github.com/pypa/pip/issues/5599) on why you should **never** use `sudo` with pip. If you think you need to use sudo, you're probably about modify system-owned files which are only intended to be modified by your system's package manager. Prefer installing packages to your user directories by passing `--user`, or even better use a virtual environment. – Brian61354270 May 14 '22 at 22:16
2

for python2 use:

py -2 -m pip install beautifulsoup4
amalik2205
  • 3,962
  • 1
  • 15
  • 21
2

I'm using Ubuntu 22.04, which comes with python 3.10.4.

Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Homer6
  • 15,034
  • 11
  • 61
  • 81
  • for me this dont work - I get ```Requirement already satisfied``` in ```/usr/lib/python3/dist-packages``` - which is the version from python 3.10 :( – Thomas Oct 01 '22 at 14:05
  • You may have to remove 3.10 first. Those packages may be changing too, given how recent Ubuntu 22 is and how they may adapt. – Homer6 Oct 04 '22 at 02:59
2
  • Folder location: /usr/local/lib/python3.8
  • Package: python3.8 -m pip install <package_name>
End user
  • 77
  • 3
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – rachwa Aug 15 '22 at 10:54
1

I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).

Making a simple change worked for me.

When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.

If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only

Raji
  • 115
  • 7
1

As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.

# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
RKelley
  • 576
  • 6
  • 16
0

I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.

Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).

CodeManX
  • 11,159
  • 5
  • 49
  • 70
0

I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.

Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.

  1. sudo apt install build-essential
  2. download the source from https://www.python.org/downloads/source/
  3. unpack the file downloaded: tar xf <filename>
  4. cd <directory> - change into the directory created.
  5. ./configure
  6. make
  7. sudo make install

Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.

Again, do not mess with system python.

Kevin Buchs
  • 2,520
  • 4
  • 36
  • 55