26

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.

7
  • Why are you not using virtualenv? It was made to solve this exact problem. Commented Jan 15, 2016 at 1:45
  • Run pip3 -V and see where it points to. You should also have a pip3.5 for Py3.5. Commented Jan 15, 2016 at 1:46
  • @OdraEncoded: Thank you, I've got the answer myself. Commented Jan 15, 2016 at 1:49
  • 1
    Didn't, you might want to fix the link in your answer though. Commented Jan 15, 2016 at 1:51
  • Thank you, I've change the link to a properer section. Commented Jan 15, 2016 at 1:54

4 Answers 4

72

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
Sign up to request clarification or add additional context in comments.

4 Comments

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
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 <somepackage>, if you don't know the path of pip, use which to find it, like this pythonXX `which pip` install <somepackage>
@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 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.
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

Comments

3

This solution worked for me:

sudo python2.7 -m pip install [package name]

2 Comments

It's not good practice to sudo pip install as this clobbers your distribution's Python dependencies on Linux.
Clobbers? Otherwise agree use of sudo should be limited.
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.