27

I'm now currently using Python on Ubuntu 15.10.

But in my OS, I have many different python versions installed:

  • Python (2.7.9)
  • Python3 (3.4.3)
  • Python3.5
  • PyPy

So, it got messy with the versions of the packages in different environments. For example, if I run:

pip3 install django

But in fact, I cannot import django inside python3.5.

Is there any efficient way to call the correct version of pip?

Note:
Don't suggest that I use virtualenv, I know about it and am seeking another solution.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189

4 Answers4

73

Finally I found the solution myself, see the Docs:

https://docs.python.org/3/installing/index.html?highlight=pip#work-with-multiple-versions-of-python-installed-in-parallel

Just call:

pythonXX -m pip install SomePackage

That would work separately for each version of installed python.

Also, according to the docs, if we want to do the same thing in windows, the command is a bit different:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
  • 3
    windows: `py -3 -m pip install SomePackage` install the latest 3 version in 64 `py -3.6-32 -m pip install SomePackage` install the module on the 32 bytes version – JinSnow Feb 17 '17 at 20:10
  • 1
    Actually, because pip is python script, it is interpreted by python, so it also works well without option '-m', like `pythonXX /usr/bin/pip install `, if you don't know the path of `pip`, use `which` to find it, like this ``pythonXX `which pip` install `` – Bruce Aug 24 '18 at 19:52
  • 1
    @Bruce that defeats the whole point of not having to remember different pip versions and automatically matching pip to python by using `-m pip` in a call to the desired python version – Bananach Nov 14 '18 at 10:33
  • @Bananach No it doesn't. Because you also don't need to remember the pip version. What you are using is `/usr/bin/pip` or `which pip`. There is no version within it. – Bruce Nov 20 '18 at 00:10
5

How about using pyenv?

You can switch the version.

$ pyenv install 2.7.X
$ pyenv install 3.5.X
$ pyenv local 2.7.X
$ pyenv global 3.5.X
masudak
  • 161
  • 1
  • 6
3

This solution worked for me:

sudo python2.7 -m pip install [package name]
CrazyCrow
  • 4,125
  • 1
  • 28
  • 39
Franke
  • 1,234
  • 12
  • 14
0

Why not using anaconda?

If you use conda, you can easily create/manage virtual env. For example, if you have root env python 3.4 and py27 env for python 2.7, you can easily switch between them use command source activate [env]

source activate py27
conda install SomePackage
spacegoing
  • 5,056
  • 5
  • 25
  • 42
  • I find that conda and virtual environments work well together. I have multiple versions of python installed in conda environments switch to the one I want then use 'python -m venv ./venv' which installs the python version for the project. Then deactivate the conda environment and run with the new 'venv' for package installations with 'pip' works well so far. – eklektek Feb 08 '23 at 17:46