222

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
2

17 Answers 17

331

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
18
  • 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, 2015 at 2:12
  • 9
    BEST Solution to all Python2 and Python3 problems! Dec 2, 2015 at 13: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, 2016 at 16:45
  • 2
    I can't upvote this answer enough. Finally solved this painful problem on DietPi (Raspberry distro)
    – Pitto
    Oct 21, 2016 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, 2016 at 16:10
73

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.

7
  • 9
    I do not have neither pip-2.6 nor pip-2.7
    – torayeff
    Jun 6, 2012 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. Jun 6, 2012 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 6, 2012 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. Jun 6, 2012 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. Jun 6, 2012 at 21:30
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
5
  • 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, 2016 at 2: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, 2017 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 as a roundabout way of installing pip for python2.7
    – airdas
    Sep 28, 2017 at 16:17
  • 8
    py -3.8 -m pip install beautifulsoup4 works for me
    – Hzzkygcs
    Oct 4, 2021 at 9: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 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
3
  • 1
    Thansk. It woked in my case. I have 3.4 and 3.7 version. I had to run py -3.7 -m pip install -r requirements.txt to use the pip associated with python 3.7
    – rodrigorf
    May 9, 2019 at 19:12
  • 2
    This should be the accepted answer. The others above this do not work.
    – NL23codes
    Oct 8, 2021 at 16:52
  • I'm now wondering why this worked and not the others. Was it because it was python3? Feb 1 at 23:16
28

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
3
  • This is like my answer, but better, since it doesn't need to fiddle with system paths.
    – Turion
    Aug 11, 2017 at 9:07
  • It seems this would now be the best answer. Worked perfect with me as well. Mar 1, 2019 at 3:11
  • 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 at 16:49
19

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
2
  • Can i do this without administrator privilege? Jan 7, 2021 at 9:42
  • @NaveenKumar yes, you can. The packages usually gets installed on C:\Users\{user.account}\AppData\Roaming\Python\Python37\site-packages Mar 1, 2021 at 21:38
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

2
  • To any future visitors, please see this warning from the makers of pip 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. May 14, 2022 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 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 ".

3

Python 2

sudo pip2 install johnbonjovi  

Python 3

sudo pip3 install johnbonjovi
2
  • 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, 2017 at 19:11
  • To any future visitors, please see this warning from the makers of pip 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. May 14, 2022 at 22:15
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.

3
  • 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, 2020 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 6, 2021 at 6:40
  • To any future visitors, please see this warning from the makers of pip 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. May 14, 2022 at 22:16
2

for python2 use:

py -2 -m pip install beautifulsoup4
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

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"
1
  • 1
    What are $(which pip) and $1"
    – gyuunyuu
    Nov 10, 2020 at 23:29
1

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
2
  • 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 1, 2022 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 4, 2022 at 2:59
1
  • Folder location: /usr/local/lib/python3.8
  • Package: python3.8 -m pip install <package_name>
1
  • Your answer could be improved by adding more information on what the code does and how it helps the OP.
    – rachwa
    Aug 15, 2022 at 10:54
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).

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.