Dealing with multiple Python versions and PIP?
The current recommendation is to use python -m pip
, where python
is the version of Python you would like to use. This is the recommendation because it works across all versions of Python, and in all forms of virtualenv. For example:
# The system default python:
$ python -m pip install fish
# A virtualenv's python:
$ .env/bin/python -m pip install fish
# A specific version of python:
$ python-3.6 -m pip install fish
Previous answer, left for posterity:
Since version 0.8, Pip supports pip-{version}
. You can use it the same as easy_install-{version}
:
$ pip-2.5 install myfoopackage
$ pip-2.6 install otherpackage
$ pip-2.7 install mybarpackage
EDIT: pip changed its schema to use pipVERSION
instead of pip-VERSION
in version 1.5. You should use the following if you have pip >= 1.5
:
$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage
Check https://github.com/pypa/pip/pull/1053 for more details
References:
- https://github.com/pypa/pip/issues/200
http://www.pip-installer.org/docs/pip/en/0.8.3/news.html#id4
https://pip.pypa.io/en/stable/news/#v0-8 or
https://web.archive.org/web/20140310013920/http://www.pip-installer.org:80/docs/pip/en/0.8.3/news.html#id4
How do I deal with pip in different python versions?
If you have your python2
binary located, you can just call it directly:
/usr/bin/python2 -m pip install googlemaps
And if you're not sure where your python binary is, you can use
import sys
print(sys.executable)
to locate it.
And if you don't have pip, you should install it by downloading this file:
https://bootstrap.pypa.io/get-pip.py
then running:
/usr/bin/python2 get-pip.py
Installing scikit-learn with pip: are there multiple python versions installed on my computer?
First, use python -v
to check default python installation. If it is the version you are using, continue with python
instead of python3
.
Now run python3 -m pip install scikit-learn
If you are on a Mac, DO NOT DELETE PYTHON 2.7. It is needed for your system to run properly.
How to ensure I use the correct python version when I have multiple versions of Python installed?
Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself.
They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?):
~$ python3.7 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
~$ python3.8 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
~$ python3.9 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']
The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet.
With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it.
tmp$ python3.7 -m venv app1
tmp$ source ./app1/bin/activate
(app1) tmp$ python -V
Python 3.7.12
(app1) tmp$
(app1) tmp$ deactivate
tmp$ python3.8 -m venv app2
tmp$ source ./app2/bin/activate
(app2) tmp$ python -V
Python 3.8.12
(app2) tmp$
(app2) tmp$ deactivate
tmp$ python3.9 -m venv app3
tmp$ source ./app3/bin/activate
(app3) tmp$ python -V
Python 3.9.9
(app3) tmp$
(app3) tmp$ deactivate
In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python
refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?.
As for installing packages, again, once you have setup the correct virtual environments, doing pip install
would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package.
If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with
$ </path/or/alias/to/specific/python/installation> -m pip install <packages>
$ python3.7 -m pip install "flake8<=3.6"
$ python3.7 -m pip list | grep flake8
flake8 3.6.0
$ ls -H /usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8
flake8
flake8-3.6.0.dist-info
flake8_quotes
flake8_quotes-3.2.0.dist-info
$ python3.9 -m pip install flake8
$ python3.9 -m pip list | grep flake8
flake8 4.0.1
$ ls -H /usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8
flake8
flake8-4.0.1.dist-info
The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder.
But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0.
This means the system default for python
or python3
on your machine points to the 3.10 installation. Mine is set to point to Python 3.9:
tmp$ python3 -V
Python 3.9.9
tmp$ which python3
/usr/local/bin/python3
tmp$ /usr/local/bin/python3 -V
Python 3.9.9
You can:
- Manually change the default version
- Setup aliases to the different versions:
tmp$ type python3.7
python3.7 is aliased to `/usr/local/opt/python@3.7/bin/python3'
tmp$ type python3.8
python3.8 is aliased to `/usr/local/opt/python@3.8/bin/python3'
tmp$ type python3.9
python3.9 is aliased to `/usr/local/opt/python@3.9/bin/python3' - Use a version management tool for switching versions, such as pyenv
How to run pip from different versions of python using the python command?
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
Install a module using pip for specific python version
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.
Related Topics
Change Default Python Version from 2.4 to 2.6
System-Wide Mutex in Python on Linux
Are Global Variables Thread-Safe in Flask? How to Share Data Between Requests
Selenium "Selenium.Common.Exceptions.Nosuchelementexception" When Using Chrome
Listing Available Devices in Python-Opencv
How to Create a Density Plot in Matplotlib
Converting Epoch Time With Milliseconds to Datetime
Hex/Binary String Conversion in Swift
List of Lists Changes Reflected Across Sublists Unexpectedly
How to Clone a List So That It Doesn't Change Unexpectedly After Assignment
What Are Metaclasses in Python
How to Read a File Line-By-Line into a List
Lazy Method For Reading Big File in Python
How to Install Pygame on Python Via Pip (Windows 10)
Faster Version of 'Pygame.Event.Get()'. Why Are Events Being Missed and Why Are the Events Delayed