Is there any way to make pip play well with multiple versions of Python? For example, I want to use pip to explicitly install things to either my site 2.5 installation or my site 2.6 installation.
For example, with easy_install, I use easy_install-2.{5,6}.
And, yes — I know about virtualenv, and no — it's not a solution to this particular problem.
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
On Windows, you can execute the pip module using a given Python version through the Python launcher, py.exe, if you chose to install it during Python 3 setup.
py -3 -m pip install packagename
py -2 -m pip install packagename
You can be even more specific and request an exact sub-version of Python:
py -3.6 -m pip install packagename
To get a list of all installed Python versions available through the launcher, run:
py --list
Alternatively, you can launch the desired Python executable directly:
C:/path/to/specific/python.exe -m pip install packagename
/path/to/python2.{5,6} /path/to/pip install PackageName doesn't work?
For this to work on any python version that doesn't have pip already installed you need to download pip and do python*version* setup.py install. For example python3.3 setup.py install. This resolves the import error in the comments. (As suggested by #hbdgaf)
I had python 2.6 installed by default (Amazon EC2 AMI), but needed python2.7 plus some external packages for my application. Assuming you already installed python2.7 alongside with default python (2.6 in my case). Here is how to install pip and packages for non-default python2.7
Install pip for your python version:
curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py
Use specific pip version to install packages:
pip2.7 install mysql-connector-python --allow-external mysql-connector-python
It worked for me in windows this way:
I changed the name of python files python.py and pythonw.exe to python3.py pythonw3.py
Then I just ran this command in the prompt:
python3 -m pip install package
Other answers show how to use pip with both 2.X and 3.X Python, but does not show how to handle the case of multiple Python distributions (eg. original Python and Anaconda Python).
I have a total of 3 Python versions: original Python 2.7 and Python 3.5 and Anaconda Python 3.5.
Here is how I install a package into:
Original Python 3.5:
/usr/bin/python3 -m pip install python-daemon
Original Python 2.7:
/usr/bin/python -m pip install python-daemon
Anaconda Python 3.5:
python3 -m pip install python-daemon
or
pip3 install python-daemon
Simpler, as Anaconda overrides original Python binaries in user environment.
Of course, installing in anaconda should be done with conda command, this is just an example.
Also, make sure that pip is installed for that specific python.You might need to manually install pip. This works in Ubuntu 16.04:
sudo apt-get install python-pip
or
sudo apt-get install python3-pip
From here: https://docs.python.org/3/installing/
Here is how to install packages for various versions that are installed at the same time linux, mac, posix:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
python3.5 -m pip install SomePackage # specifically Python 3.5
python3.6 -m pip install SomePackage # specifically Python 3.6
On Windows, use the py Python launcher in combination with the -m switch:
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
I ran into this issue myself recently and found that I wasn't getting the right pip for Python 3, on my Linux system that also has Python 2.
First you must ensure that you have installed pip for your python version:
For Python 2:
sudo apt-get install python-pip
For Python 3:
sudo apt-get install python3-pip
Then to install packages for one version of Python or the other, simply use the following for Python 2:
pip install <package>
or for Python 3:
pip3 install <package>
pip is also a python package. So the easiest way to install modules to a specific python version would be below
python2.7 /usr/bin/pip install foo
or
python2.7 -m pip install foo
So apparently there are multiple versions of easy_install and pip. It seems to be a big mess. Anyway, this is what I did to install Django for Python 2.7 on Ubuntu 12.10:
$ sudo easy_install-2.7 pip
Searching for pip
Best match: pip 1.1
Adding pip 1.1 to easy-install.pth file
Installing pip-2.7 script to /usr/local/bin
Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip
$ sudo pip-2.7 install django
Downloading/unpacking django
Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
Running setup.py egg_info for package django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
Running setup.py install for django
changing mode of build/scripts-2.7/django-admin.py from 644 to 755
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django
Cleaning up...
$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>
On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2.7 -m pip install SomePackage
python3.4 -m pip install SomePackage
(appropriately versioned pip commands may also be available)
On Windows, use the py Python launcher in combination with the -m switch:
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3.4 -m pip install SomePackage # specifically Python 3.4
if you get an error for py -3.4 then try:
pip install SomePackage
Installation of multiple versions of Python and respective Packages.
Python version on the same windows machine : 2.7 , 3.4 and 3.6
Installation of all 3 versions of Python :
Installed the Python 2.7 , 3.4 and 3.6 with the below paths
PATH for all 3 versions of Python :
Made sure the PATH variable ( in System Variables ) has below paths included - C:\Python27\;C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;C:\Python36\Scripts\;
Renaming the executables for versions :
Changed the python executable name in C:\Python36 and C:\Python34 to python36 and python34 respectively.
Checked for the command prompt with all versions :
Installing the packages separately for each version
If you have multiple versions as well as multiple architectures (32 bit, 64 bit) you will need to add a -32 or -64 at the end of your version.
For windows, go to cmd and type py --list and it will produce the versions you have installed. The list will look like the following:
Installed Pythons found by py Launcher for Windows
-3.7-64 *
-3.7-32
-3.6-32
The full command as an example will be:
py -3.6-32 -m pip install (package)
If you want to get more indepth, to install a specific version of a package on a specific version of python, use ==(version) after the package. As an example,
py -3.6-32 -m pip install opencv-python==4.1.0.25
Here is my take on the problem. Works for Python3. The main features are:
Each Python version is compiled from source
All versions are installed locally
Does not mangle your system's default Python installation in any way
Each Python version is isolated with virtualenv
Prerequisites: If you are using some bare-bones thin client with no extra turf installed, you should run this first (in ubuntu 18.04 at least, extra packages added for convenience):
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository universe
sudo apt-get update
sudo apt-get install -y build-essential cmake
sudo apt-get install -y zlib1g zlib1g-dev libsqlite3-dev \
openssl libssl-dev libffi-dev unzip pciutils net-tools \
libblas-dev gfortran libblas3
The steps are as follows:
If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though.
Download source for different python versions under the following directory structure:
$HOME/
python_versions/ : download Python-*.tgz packages here and "tar xvf" them. You'll get directories like this:
Python-3.4.8/
Python-3.6.5/
Python-3.x.y/
...
At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!):
mkdir root
./configure --prefix=$PWD/root
make -j 2
make install
virtualenv --no-site-packages -p root/bin/python3.x env
At "python_versions/" create files like this:
env_python3x.bash:
#!/bin/bash
echo "type deactivate to exit"
source $HOME/python_versions/Python-3.x.y/env/bin/activate
Now, anytime you wish to opt for python3.x, do
source $HOME/python_versions/env_python3x.bash
to enter the virtualenv
While in the virtualenv, install your favorite python packages with
pip install --upgrade package_name
To exit the virtualenv and python version just type "deactivate"
UPDATE
It seems that --no-site-packages is deprecated. There's an easy fix for this: Once you have activated the virtualenv, just point the HOME env variable to somewhere else than your actual home directory, i.e.:
export HOME=some/where/else
A nice way to do this in general is:
Create virtualenv
Activate virtualenv
If you want to "recycle" existing libraries to your virtualenv, softlink them from your existing install, i.e.
ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/
Do export PYTHONPATH=, export HOME=/some/other/dir
Now you should have custom-isolated virtualenv.
UPDATE 2 / SUDO
Wan't to force sudo to use your virtualenv?
Defaults secure_path="/home/USENAME/Python-3.x.y/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults env_keep += "VIRTUAL_ENV"
Defaults env_keep += "PYTHONPATH"
Now try "sudo python3 --version" and magic should happen
UPDATE 3 / DOCKER
Enable virtualenv inside your docker (of course, you have built it in your docker image):
ENV VIRTUAL_ENV=/home/USER/Python-3.x.y/env
ENV PYTHONPATH=
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
You can use one of the following commands:
pip2 install SomePackage
pip3 install SomePackage
python2 -m pip install SomePackage
python3 -m pip install SomePackage
And of course, make sure that you have the correct version of pip installed
sudo apt-get install python-pip
sudo apt-get install python3-pip
I haven't used these commands myself but, some answers above suggests using them to specify exactly the version of python you want to use
pip-2.7 install SomePackage
python-3.6 -m pip install SomePackage
For python 3 and Windows OS, I always use this syntax to install packages on different version:
First I always use Git Bash Command Prompt.
Here an example installing urllib package.
Default Python version:(The normal pip command)
pip install urllib3
For the other versions
py -3.8 -m pip install urllib3
py => for python
-3.8 => for the version (I'm using the 3.8.7 version) but if you're using the 3.7.7 version it will be "-3.7"
-m : just because or for modify
pip install urllib3 : the normal pip command
Most of the answers here address the issue but I want to add something what was continually confusing me with regard to creating an alternate installation of python in the /usr/local on CentOS 7. When I installed there, it appeared like pip was working since I could use pip2.7 install and it would install modules. However, what I couldn't figure out was why my newly installed version of python wasn't seeing what I was installing.
It turns out in CentOS 7 that there is already a python2.7 and a pip2.7 in the /usr/bin folder. To install pip for your new python distribution, you need to specifically tell sudo to go to /usr/local/bin
sudo /usr/local/bin/python2.7 -m ensurepip
This should get pip2.7 installed in your /usr/local/bin folder along with your version of python. The trick is that when you want to install modules, you either need to modify the sudo $PATH variable to include /usr/local/bin or you need to execute
sudo /usr/local/bin/pip2.7 install <module>
if you want to install a new module. It took me forever to remember that sudo wasn't immediately seeing /usr/local/bin.
Simple and recent
On windows
1- Supposed that you have a different version of Python installed in your system. To check use the following command to check:
> py --list
-3.10-64 *
-3.7-64
2- Set your preferred default version:
by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.7).
by settting the py.ini file usually located on C:\Users\<your user name>\AppData\Local if not create one. For example, setting PY_PYTHON=3 and PY_PYTHON3=3.7 environment variables are equivalent to the INI file containing:
[defaults]
python=3
python3=3.7
3- check by typing again py --list:
> py --list
-3.10-64
-3.7-64 *
4- If you would like to run virtual environment with spesific version of python and pip see this post.
On Linux (Ubuntu)
An easy approach for managing different python versions on Linux is update-alternatives command. This command giving us the ability to switch between many versions of the same software easily.
command format:update-alternatives --install link name path priority, the name is the generic name for the master link, the link is the name of its symlink, the path is the alternative being introduced for the master link, and priority is the priority of the alternatives group.
Usage: Suppose you installed two versions of python (python3.10 , python3.7). Now by running this command you will link the command name (python3) to different versions of python and assign a priority number. A higher priority number means a higher priority.
$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
List installed versions of python with this command:
$ update-alternatives --list python3
/usr/bin/python3.7
/usr/bin/python3.10
Switching between versions: Just manually select the priority number of the desired python version after running following command.
$ update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.10 2 auto mode
* 1 /usr/bin/python3.7 1 manual mode
2 /usr/bin/python3.10 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
Context: Archlinux
Action:
Install python2-pip:
sudo pacman -S python2-pip
You now have pip2.7:
sudo pip2.7 install boto
Test (in my case I needed 'boto'):
Run the following commands:
python2
import boto
Success: No error.
Exit: Ctrl+D
for example, if you set other versions (e.g. 3.5) as default and want to install pip for python 2.7:
download pip at https://pypi.python.org/pypi/pip (tar)
unzip tar file
cd to the file’s directory
sudo python2.7 setup.py install
If you have both python3.6 and python3.7 installed and want to use pip with python3.7 by default, here's what you should do:
First make sure you have pip installed for python3.7
python3.7 -m pip install -U pip
Now pip3.7 must be available, so we edit .bashrc
nano ~/.bashrc
adding the following line to it
alias pip=pip3.7
In order for the changes to take effect type in the shell:
source ~/.bashrc
Now if you type:
pip --version
you should get:
pip 20.1.1 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)
which means, if you use, for example:
pip install <package>
it would install the <package> for python3.7
Another possible way could be using conda and pip. Some time you probably want to use just one of those, but if you really need to set up a particular version of python I combine both.
I create a starting conda enviroment with the python I want. As in here https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html. Alternatively you could set up the whole enviroment just using conda.
conda create -n myenv python=3.6.4
Then activate your enviroment with the python you like. This command could change depending on the OS.
source activae myenv
Now you have your python active then you could continue using conda but if you need/want to use pip:
python -m pip -r requirements.txt
Here you have a possible way.
You can go to for example C:\Python2.7\Scripts and then run cmd from that path. After that you can run pip2.7 install yourpackage...
That will install package for that version of Python.
This is probably the completely wrong thing to do (I'm a python noob), but I just went in and edited the pip file
#!/usr/bin/env python3 <-- I changed this line.
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
To use multiple versions of pip, just type
pip{version} and run command
Example: for python 3.10
pip3.10
pip3.10 list
pip3.10
for Python 3.7
pip3.7
pip3.7 list
For windows specifically:
\path\to\python.exe -m pip install PackageName works.
for Blender:
/usr/bin $ python3.7 -m pip install irc
Some useful information for debugging this is the pip debug command. It shows the location of the python interpreter that it is attached to in the 1st line (after the warning).
$ pip debug
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 21.2.4 from /data/akshay/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
sys.version: 3.9.12 (main, Apr 5 2022, 06:56:58)
sys.executable: /data/akshay/anaconda3/bin/python
Related
Is there any way to make pip play well with multiple versions of Python? For example, I want to use pip to explicitly install things to either my site 2.5 installation or my site 2.6 installation.
For example, with easy_install, I use easy_install-2.{5,6}.
And, yes — I know about virtualenv, and no — it's not a solution to this particular problem.
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
On Windows, you can execute the pip module using a given Python version through the Python launcher, py.exe, if you chose to install it during Python 3 setup.
py -3 -m pip install packagename
py -2 -m pip install packagename
You can be even more specific and request an exact sub-version of Python:
py -3.6 -m pip install packagename
To get a list of all installed Python versions available through the launcher, run:
py --list
Alternatively, you can launch the desired Python executable directly:
C:/path/to/specific/python.exe -m pip install packagename
/path/to/python2.{5,6} /path/to/pip install PackageName doesn't work?
For this to work on any python version that doesn't have pip already installed you need to download pip and do python*version* setup.py install. For example python3.3 setup.py install. This resolves the import error in the comments. (As suggested by #hbdgaf)
I had python 2.6 installed by default (Amazon EC2 AMI), but needed python2.7 plus some external packages for my application. Assuming you already installed python2.7 alongside with default python (2.6 in my case). Here is how to install pip and packages for non-default python2.7
Install pip for your python version:
curl -O https://bootstrap.pypa.io/get-pip.py
python27 get-pip.py
Use specific pip version to install packages:
pip2.7 install mysql-connector-python --allow-external mysql-connector-python
It worked for me in windows this way:
I changed the name of python files python.py and pythonw.exe to python3.py pythonw3.py
Then I just ran this command in the prompt:
python3 -m pip install package
Other answers show how to use pip with both 2.X and 3.X Python, but does not show how to handle the case of multiple Python distributions (eg. original Python and Anaconda Python).
I have a total of 3 Python versions: original Python 2.7 and Python 3.5 and Anaconda Python 3.5.
Here is how I install a package into:
Original Python 3.5:
/usr/bin/python3 -m pip install python-daemon
Original Python 2.7:
/usr/bin/python -m pip install python-daemon
Anaconda Python 3.5:
python3 -m pip install python-daemon
or
pip3 install python-daemon
Simpler, as Anaconda overrides original Python binaries in user environment.
Of course, installing in anaconda should be done with conda command, this is just an example.
Also, make sure that pip is installed for that specific python.You might need to manually install pip. This works in Ubuntu 16.04:
sudo apt-get install python-pip
or
sudo apt-get install python3-pip
From here: https://docs.python.org/3/installing/
Here is how to install packages for various versions that are installed at the same time linux, mac, posix:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
python3.5 -m pip install SomePackage # specifically Python 3.5
python3.6 -m pip install SomePackage # specifically Python 3.6
On Windows, use the py Python launcher in combination with the -m switch:
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
I ran into this issue myself recently and found that I wasn't getting the right pip for Python 3, on my Linux system that also has Python 2.
First you must ensure that you have installed pip for your python version:
For Python 2:
sudo apt-get install python-pip
For Python 3:
sudo apt-get install python3-pip
Then to install packages for one version of Python or the other, simply use the following for Python 2:
pip install <package>
or for Python 3:
pip3 install <package>
pip is also a python package. So the easiest way to install modules to a specific python version would be below
python2.7 /usr/bin/pip install foo
or
python2.7 -m pip install foo
So apparently there are multiple versions of easy_install and pip. It seems to be a big mess. Anyway, this is what I did to install Django for Python 2.7 on Ubuntu 12.10:
$ sudo easy_install-2.7 pip
Searching for pip
Best match: pip 1.1
Adding pip 1.1 to easy-install.pth file
Installing pip-2.7 script to /usr/local/bin
Using /usr/lib/python2.7/dist-packages
Processing dependencies for pip
Finished processing dependencies for pip
$ sudo pip-2.7 install django
Downloading/unpacking django
Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
Running setup.py egg_info for package django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: django
Running setup.py install for django
changing mode of build/scripts-2.7/django-admin.py from 644 to 755
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /usr/local/bin/django-admin.py to 755
Successfully installed django
Cleaning up...
$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>
On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2.7 -m pip install SomePackage
python3.4 -m pip install SomePackage
(appropriately versioned pip commands may also be available)
On Windows, use the py Python launcher in combination with the -m switch:
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3.4 -m pip install SomePackage # specifically Python 3.4
if you get an error for py -3.4 then try:
pip install SomePackage
Installation of multiple versions of Python and respective Packages.
Python version on the same windows machine : 2.7 , 3.4 and 3.6
Installation of all 3 versions of Python :
Installed the Python 2.7 , 3.4 and 3.6 with the below paths
PATH for all 3 versions of Python :
Made sure the PATH variable ( in System Variables ) has below paths included - C:\Python27\;C:\Python27\Scripts;C:\Python34\;C:\Python34\Scripts;C:\Python36\;C:\Python36\Scripts\;
Renaming the executables for versions :
Changed the python executable name in C:\Python36 and C:\Python34 to python36 and python34 respectively.
Checked for the command prompt with all versions :
Installing the packages separately for each version
If you have multiple versions as well as multiple architectures (32 bit, 64 bit) you will need to add a -32 or -64 at the end of your version.
For windows, go to cmd and type py --list and it will produce the versions you have installed. The list will look like the following:
Installed Pythons found by py Launcher for Windows
-3.7-64 *
-3.7-32
-3.6-32
The full command as an example will be:
py -3.6-32 -m pip install (package)
If you want to get more indepth, to install a specific version of a package on a specific version of python, use ==(version) after the package. As an example,
py -3.6-32 -m pip install opencv-python==4.1.0.25
Here is my take on the problem. Works for Python3. The main features are:
Each Python version is compiled from source
All versions are installed locally
Does not mangle your system's default Python installation in any way
Each Python version is isolated with virtualenv
Prerequisites: If you are using some bare-bones thin client with no extra turf installed, you should run this first (in ubuntu 18.04 at least, extra packages added for convenience):
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository universe
sudo apt-get update
sudo apt-get install -y build-essential cmake
sudo apt-get install -y zlib1g zlib1g-dev libsqlite3-dev \
openssl libssl-dev libffi-dev unzip pciutils net-tools \
libblas-dev gfortran libblas3
The steps are as follows:
If you have several extra python versions installed in some other way, get rid of them, e.g., remove $HOME/.local/lib/python3.x, etc. (also the globally installed ones). Don't touch your system's default python3 version though.
Download source for different python versions under the following directory structure:
$HOME/
python_versions/ : download Python-*.tgz packages here and "tar xvf" them. You'll get directories like this:
Python-3.4.8/
Python-3.6.5/
Python-3.x.y/
...
At each "Python-3.x.y/" directory, do the following (do NOT use "sudo" in any of the steps!):
mkdir root
./configure --prefix=$PWD/root
make -j 2
make install
virtualenv --no-site-packages -p root/bin/python3.x env
At "python_versions/" create files like this:
env_python3x.bash:
#!/bin/bash
echo "type deactivate to exit"
source $HOME/python_versions/Python-3.x.y/env/bin/activate
Now, anytime you wish to opt for python3.x, do
source $HOME/python_versions/env_python3x.bash
to enter the virtualenv
While in the virtualenv, install your favorite python packages with
pip install --upgrade package_name
To exit the virtualenv and python version just type "deactivate"
UPDATE
It seems that --no-site-packages is deprecated. There's an easy fix for this: Once you have activated the virtualenv, just point the HOME env variable to somewhere else than your actual home directory, i.e.:
export HOME=some/where/else
A nice way to do this in general is:
Create virtualenv
Activate virtualenv
If you want to "recycle" existing libraries to your virtualenv, softlink them from your existing install, i.e.
ln -s $HOME/.local/lib/python3.6/site-packages/numpy $PWD/venv/lib/python3.6/site-packages/
Do export PYTHONPATH=, export HOME=/some/other/dir
Now you should have custom-isolated virtualenv.
UPDATE 2 / SUDO
Wan't to force sudo to use your virtualenv?
Defaults secure_path="/home/USENAME/Python-3.x.y/env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
Defaults env_keep += "VIRTUAL_ENV"
Defaults env_keep += "PYTHONPATH"
Now try "sudo python3 --version" and magic should happen
UPDATE 3 / DOCKER
Enable virtualenv inside your docker (of course, you have built it in your docker image):
ENV VIRTUAL_ENV=/home/USER/Python-3.x.y/env
ENV PYTHONPATH=
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
You can use one of the following commands:
pip2 install SomePackage
pip3 install SomePackage
python2 -m pip install SomePackage
python3 -m pip install SomePackage
And of course, make sure that you have the correct version of pip installed
sudo apt-get install python-pip
sudo apt-get install python3-pip
I haven't used these commands myself but, some answers above suggests using them to specify exactly the version of python you want to use
pip-2.7 install SomePackage
python-3.6 -m pip install SomePackage
For python 3 and Windows OS, I always use this syntax to install packages on different version:
First I always use Git Bash Command Prompt.
Here an example installing urllib package.
Default Python version:(The normal pip command)
pip install urllib3
For the other versions
py -3.8 -m pip install urllib3
py => for python
-3.8 => for the version (I'm using the 3.8.7 version) but if you're using the 3.7.7 version it will be "-3.7"
-m : just because or for modify
pip install urllib3 : the normal pip command
Most of the answers here address the issue but I want to add something what was continually confusing me with regard to creating an alternate installation of python in the /usr/local on CentOS 7. When I installed there, it appeared like pip was working since I could use pip2.7 install and it would install modules. However, what I couldn't figure out was why my newly installed version of python wasn't seeing what I was installing.
It turns out in CentOS 7 that there is already a python2.7 and a pip2.7 in the /usr/bin folder. To install pip for your new python distribution, you need to specifically tell sudo to go to /usr/local/bin
sudo /usr/local/bin/python2.7 -m ensurepip
This should get pip2.7 installed in your /usr/local/bin folder along with your version of python. The trick is that when you want to install modules, you either need to modify the sudo $PATH variable to include /usr/local/bin or you need to execute
sudo /usr/local/bin/pip2.7 install <module>
if you want to install a new module. It took me forever to remember that sudo wasn't immediately seeing /usr/local/bin.
Simple and recent
On windows
1- Supposed that you have a different version of Python installed in your system. To check use the following command to check:
> py --list
-3.10-64 *
-3.7-64
2- Set your preferred default version:
by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.7).
by settting the py.ini file usually located on C:\Users\<your user name>\AppData\Local if not create one. For example, setting PY_PYTHON=3 and PY_PYTHON3=3.7 environment variables are equivalent to the INI file containing:
[defaults]
python=3
python3=3.7
3- check by typing again py --list:
> py --list
-3.10-64
-3.7-64 *
4- If you would like to run virtual environment with spesific version of python and pip see this post.
On Linux (Ubuntu)
An easy approach for managing different python versions on Linux is update-alternatives command. This command giving us the ability to switch between many versions of the same software easily.
command format:update-alternatives --install link name path priority, the name is the generic name for the master link, the link is the name of its symlink, the path is the alternative being introduced for the master link, and priority is the priority of the alternatives group.
Usage: Suppose you installed two versions of python (python3.10 , python3.7). Now by running this command you will link the command name (python3) to different versions of python and assign a priority number. A higher priority number means a higher priority.
$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
$ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
List installed versions of python with this command:
$ update-alternatives --list python3
/usr/bin/python3.7
/usr/bin/python3.10
Switching between versions: Just manually select the priority number of the desired python version after running following command.
$ update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.10 2 auto mode
* 1 /usr/bin/python3.7 1 manual mode
2 /usr/bin/python3.10 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
Context: Archlinux
Action:
Install python2-pip:
sudo pacman -S python2-pip
You now have pip2.7:
sudo pip2.7 install boto
Test (in my case I needed 'boto'):
Run the following commands:
python2
import boto
Success: No error.
Exit: Ctrl+D
for example, if you set other versions (e.g. 3.5) as default and want to install pip for python 2.7:
download pip at https://pypi.python.org/pypi/pip (tar)
unzip tar file
cd to the file’s directory
sudo python2.7 setup.py install
If you have both python3.6 and python3.7 installed and want to use pip with python3.7 by default, here's what you should do:
First make sure you have pip installed for python3.7
python3.7 -m pip install -U pip
Now pip3.7 must be available, so we edit .bashrc
nano ~/.bashrc
adding the following line to it
alias pip=pip3.7
In order for the changes to take effect type in the shell:
source ~/.bashrc
Now if you type:
pip --version
you should get:
pip 20.1.1 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7)
which means, if you use, for example:
pip install <package>
it would install the <package> for python3.7
Another possible way could be using conda and pip. Some time you probably want to use just one of those, but if you really need to set up a particular version of python I combine both.
I create a starting conda enviroment with the python I want. As in here https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html. Alternatively you could set up the whole enviroment just using conda.
conda create -n myenv python=3.6.4
Then activate your enviroment with the python you like. This command could change depending on the OS.
source activae myenv
Now you have your python active then you could continue using conda but if you need/want to use pip:
python -m pip -r requirements.txt
Here you have a possible way.
You can go to for example C:\Python2.7\Scripts and then run cmd from that path. After that you can run pip2.7 install yourpackage...
That will install package for that version of Python.
This is probably the completely wrong thing to do (I'm a python noob), but I just went in and edited the pip file
#!/usr/bin/env python3 <-- I changed this line.
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
To use multiple versions of pip, just type
pip{version} and run command
Example: for python 3.10
pip3.10
pip3.10 list
pip3.10
for Python 3.7
pip3.7
pip3.7 list
For windows specifically:
\path\to\python.exe -m pip install PackageName works.
for Blender:
/usr/bin $ python3.7 -m pip install irc
Some useful information for debugging this is the pip debug command. It shows the location of the python interpreter that it is attached to in the 1st line (after the warning).
$ pip debug
WARNING: This command is only meant for debugging. Do not use this with automation for parsing and getting these details, since the output and options of this command may change without notice.
pip version: pip 21.2.4 from /data/akshay/anaconda3/lib/python3.9/site-packages/pip (python 3.9)
sys.version: 3.9.12 (main, Apr 5 2022, 06:56:58)
sys.executable: /data/akshay/anaconda3/bin/python
I'm running CentOS 8 that came with native Python 3.6.8. I needed Python 3.7 so I installed Python 3.7.0 from sources. Now, python command is unknown to the system, while commands python3 and python3.7 both use Python 3.7.
All good until now, but I can't seem to get pip working.
Command pip returns command not found, while python3 -m pip, python3.7 -m pip, python3 -m pip3, and python3.7 -m pip3 return No module named pip. Only pip command that works is pip3.
Now whatever package I install via pip3 does not seem to install properly. Example given, pip3 install tornado returns Requirement already satisfied, but when I try to import tornado in Python 3.7 I get ModuleNotFoundError: No module named 'tornado'. Not the same thing can be said when I try to import it in Python 3.6, which works flawlessly. From this, I understand that my pip only works with Python 3.6, and not with 3.7.
Please tell me how can I use pip with Python 3.7, thank you.
It looks like your python3.7 does not have pip.
Install pip for your specific python by running python3.7 -m easy_install pip.
Then, install packages by python3.7 -m pip install <package_name>
Another option is to create a virtual environment from your python3.7. The venv brings pip into it by default.
You create venv by python3.7 -m venv <venv_name>
I think the packages you install will be installed for the previous version of Python. I think you should update the native OS Python like this:
Install the python3.7 package using apt-get
sudo apt-get install python 3.7
Add python3.6 & python3.7 to update-alternatives:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
Update python3 to point to Python 3.7:
`sudo update-alternatives --config python3
Test the version:
python3 -V
I got a message in my terminal while install a software : You are using pip version 8.1.1, however version 10.0.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
So I upgraded pip version: $ pip install --upgrade pip
Now I am getting this error:
~$ pip install ipython
Traceback (most recent call last):
File "/usr/bin/pip", line 9, in <module>
from pip import main
ImportError: cannot import name main
TL;DR
The 'ideal' solution (Ubuntu/Debian way):
$ python -m pip uninstall pip to uninstall the new pip 10 and retain your Ubuntu/Debian-provided patched pip 8. For a system-wide installation of modules use apt wherever possible (unless you are in a virtualenv), more on it below. In older Ubuntu/Debian versions, always add --user flag when using pip outside of virtualenvs (installs into ~/.local/, default in python-pip and python3-pip since 2016).
If you still want to use the new pip 10 exclusively, there are 3 quick workarounds:
simply re-open a new bash session (a new terminal tab, or type bash) - and pip 10 becomes available (see pip -V). debian's pip 8 remains installed but is broken; or
$ hash -d pip && pip -V to refresh pip pathname in the $PATH. debian's pip 8 remains installed but is broken; or
$ sudo apt remove python-pip && hash -d pip (for Python 3 it's python3-pip) -- to uninstall debian's pip 8 completely, in favor of your new pip 10.
Note: You will always need to add --user flag to non-debian-provided pip 10, unless you are in a virtualenv! Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip!
Details:
https://github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100
Ubuntu 16.04 with Python 2.7.12
Introduction:
Ironically, despite being suggested by pip itself to do such an upgrade via the pip install --upgrade pip command explicitely in the terminal (ugh!), upgrading it is not recommended in prepackaged GNU/Linux distributions. Ubuntu generally expects using APT package manager for any system-wide Python module updates/installs (including of pip itself), for example:
$ sudo apt-get update (resync Ubuntu package index files from sources)
$ apt-cache search <python-package-name> (full text-search on all available packages)
$ apt-cache show <python-package-name> (displays the package description)
$ sudo apt-get install python-numpy python-scipy python-matplotlib (easily installs heavy modules for data science, resolving all system dependencies automatically)
$ sudo apt-get install ipython (installs the IPython-notebook you were looking for)
$ sudo apt-get install python-pip (installs/upgrades pip to the latest version available in Ubuntu repository – usually slightly behind pypi.org, but it doesn't matter)
If you ever have to use pip install command on Ubuntu/Debian instead of apt-get install, please make sure it runs isolated and does not change the default system-wide Python packages (never use sudo with pip) – more on this below.
ImportError: cannot import name main
The error is caused by the pip install --upgrade pip command: that installs the latest pip version 10 alongside Ubuntu's default pip version from python-pip debian package from the OS distribution (the system Python installation), completely bypassing Ubuntu apt subsystem. It breaks the Ubuntu's default pip: the debian-patched launcher script from python-pip (system-installed to /usr/bin/pip*) tries to do import main() from your newly installed pip 10 library, but with a different import path, so it fails.
This error is discussed in more detail in a developer thread of the pip issue tracker, including a few proposed solutions, such as:
The $ hash -d pip command: when hash is invoked, the full pathname of pip is determined by searching the directories in $PATH and remembered. Any previously-remembered pathname is discarded. The -d option causes the shell to "forget" the remembered location of the given package name; or
Similarly, you can simply re-open a new bash session (a new terminal tab) to refresh pip pathname in $PATH; or
You could just use pip2 command (or pip3 for Python 3) instead of pip to invoke the older system-installed pip script /usr/bin/pip2 , whereas any pip launcher script located in $HOME/.local/bin dir (pip, pip2, pip2.7) will start your new user-installed pip 10 version;
You can also use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip, for example:
$ python2 -m pip install --user SomePackage # default Python 2
$ python2.7 -m pip install --user SomePackage # specifically Python 2.7
That is handy if you have several versions of Python and need an extension from PyPI for a specific Python version. The --user switch is only required when pip is used outside of virtualenv.
Or, uninstall one of the two pips – either user-installed or system-installed – to resolve the conflict:
$ python -m pip uninstall pip – to remove your manually-installed pip in favour of the previously installed Ubuntu-shipped version from python-pip debian package (python3-pip for Python 3); it is slightly older, but it finds and installs latest modules from PyPI just fine, and has a working pip command in the $PATH by default; or
$ sudo apt-get remove python-pip – to uninstall Ubuntu-provided pip in favour of your latest pip 10; if it is not accessible via the short pip command, just add your $HOME/.local/bin directory to your $PATH environment variable to use pip command (see above).
Note: Ubuntu 16.04 pip v8.1.1 and the latest pip v10.0.1 produce exactly the same PyPI index search results and can pull the same module versions;
Finally, you could ignore both pips altogether in favor of APT, and install Python packages system-wide from Ubuntu repo with:
$ apt search <python-package> # e.g. python-pandas
$ apt show <python-package> # e.g. python-flask
$ sudo apt install <python-package>
Packages prefixed with python- are for Python 2; with python3- are for Python 3.
Installation via apt-get may be what you need, in fact, python-packages from Ubuntu repository are preferred whenever possible, particularly in case of heavy system dependencies or when used system-wide. Of course, the amount of Python packages in Ubuntu repository (few thousand!) is relatively smaller compared to PyPI (and have only one version of them), because any OS repository is lagging slightly behind PyPI versions. But the upside of APT is that all the Ubuntu-provided packages underwent integration testing within Ubuntu, plus apt-get quickly resolves heavy dependencies like C extensions automatically. You will always get any required system libraries as part of the apt install, but with pip you have no such guarantees.
APT may not be an option, however, if you really need the latest (or certain older) package version, or when it can only be found at PyPI, or when modules need to be isolated; then pip is indeed more appropriate tool. If you use pip install command on Ubuntu instead of apt-get install, please ensure that it runs in an isolated virtual development environment, such as with virtualenv (sudo apt-get install python-virtualenv), or a built-in venv module (available in python3 only), or at a per-user level (pip install --user command option), but not system-wide (never sudo pip!).
Note: Using sudo pip command (with root access) should be avoided, because it interferes with the operation of the system package manager (apt) and may affect Ubuntu OS components when a system-used python module is unexpectedly upgraded, particularly by dependencies on another pip package. It is advised to never use Pip to change your system-wide Python packages, as these are managed by apt-get on Ubuntu.
I implemmented #catalinpopescu response from ImportError: cannot import name main when running pip --version command in windows7 32 bit
Find pip's path:
$ which pip
Modify file (choose your favorite editor):
$ sudo nano `which pip`
Then modify lines #catalinpopescu said:
Comment/replace lines:
from pip import main
sys.exit(main())
to:
from pip import __main__
sys.exit(__main__._main())
Immediately I upgrade to Pip Version 10.0.1, which appears it hasn't have this error.
try to upgrade the system pip
sudo pip install --upgrade pip
pip install --upgrade pip
this may be useful
I just installed python 2.7 and also pip to the 2.7 site package.
When I get the version with:
pip -V
It shows:
pip 1.3.1 from /usr/lib/python2.6/site-packages (python 2.6)
How do I use the 2.7 version of pip located at:
/usr/local/lib/python2.7/site-packages
There should be a binary called "pip2.7" installed at some location included within your $PATH variable.
You can find that out by typing
which pip2.7
This should print something like '/usr/local/bin/pip2.7' to your stdout. If it does not print anything like this, it is not installed. In that case, install it by running
$ wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
$ sudo python2.7 get-pip.py
Now, you should be all set, and
which pip2.7
should return the correct output.
An alternative is to call the pip module by using python2.7, as below:
python2.7 -m pip <commands>
For example, you could run python2.7 -m pip install <package> to install your favorite python modules. Here is a reference: https://stackoverflow.com/a/50017310/4256346.
In case the pip module has not yet been installed for this version of python, you can run the following:
python2.7 -m ensurepip
Running this command will "bootstrap the pip installer". Note that running this may require administrative privileges (i.e. sudo). Here is a reference: https://docs.python.org/2.7/library/ensurepip.html and another reference https://stackoverflow.com/a/46631019/4256346.
as noted here, this is what worked best for me:
sudo apt-get install python3 python3-pip python3-setuptools
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
pip has now dropped support for python2, therefore you can't use python2 pip
You can't find python2-pip in apt-get anymore, and you won't get pip when installing python2 from source
You can still install python modules using apt-get. To install a python prepend ‘python-’ to the module name
apt-get install python-six # install six
I want to install pip. It should support Python 3, but it requires setuptools, which is available only for Python 2.
How can I install pip with Python 3?
edit: Manual installation and use of setuptools is not the standard process anymore.
If you're running Python 2.7.9+ or Python 3.4+
Congrats, you should already have pip installed. If you do not, read onward.
If you're running a Unix-like System
You can usually install the package for pip through your package manager if your version of Python is older than 2.7.9 or 3.4, or if your system did not include it for whatever reason.
Instructions for some of the more common distros follow.
Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 2.x
Run the following command from a terminal:
sudo apt-get install python-pip
Installing on Debian (Wheezy and newer) and Ubuntu (Trusty Tahr and newer) for Python 3.x
Run the following command from a terminal:
sudo apt-get install python3-pip
Note:
On a fresh Debian/Ubuntu install, the package may not be found until you do:
sudo apt-get update
Installing pip on CentOS 7 for Python 2.x
On CentOS 7, you have to install setup tools first, and then use that to install pip, as there is no direct package for it.
sudo yum install python-setuptools
sudo easy_install pip
Installing pip on CentOS 7 for Python 3.x
Assuming you installed Python 3.4 from EPEL, you can install Python 3's setup tools and use it to install pip.
# First command requires you to have enabled EPEL for CentOS7
sudo yum install python34-setuptools
sudo easy_install pip
If your Unix/Linux distro doesn't have it in package repos
Install using the manual way detailed below.
The manual way
If you want to do it the manual way, the now-recommended method is to install using the get-pip.py script from pip's installation instructions.
Install pip
To install pip, securely download get-pip.py
Then run the following (which may require administrator access):
python get-pip.py
If setuptools is not already installed, get-pip.py will install setuptools for you.
I was able to install pip for python 3 on Ubuntu just by running sudo apt-get install python3-pip.
Python 3.4+ and Python 2.7.9+
Good news! Python 3.4 (released March 2014) ships with Pip. This is the best feature of any Python release. It makes the community's wealth of libraries accessible to everyone. Newbies are no longer excluded by the prohibitive difficulty of setup. In shipping with a package manager, Python joins Ruby, Nodejs, Haskell, Perl, Go--almost every other contemporary language with a majority open-source community. Thank you Python.
Of course, that doesn't mean Python packaging is problem solved. The experience remains frustrating. I discuss this at Does Python have a package/module management system?
Alas for everyone using an earlier Python. Manual instructions follow.
Python ≤ 2.7.8 and Python ≤ 3.3
Follow my detailed instructions at https://stackoverflow.com/a/12476379/284795 . Essentially
Official instructions
Per https://pip.pypa.io/en/stable/installing.html
Download get-pip.py, being careful to save it as a .py file rather than .txt. Then, run it from the command prompt.
python get-pip.py
You possibly need an administrator command prompt to do this. Follow http://technet.microsoft.com/en-us/library/cc947813(v=ws.10).aspx
For me, this installed Pip at C:\Python27\Scripts\pip.exe. Find pip.exe on your computer, then add its folder (eg. C:\Python27\Scripts) to your path (Start / Edit environment variables). Now you should be able to run pip from the command line. Try installing a package:
pip install httpie
There you go (hopefully)!
if you're using python 3.4+
just type:
python3 -m pip
For Ubuntu 12.04 or older,
sudo apt-get install python3-pip
won't work. Instead, use:
sudo apt-get install python3-setuptools ca-certificates
sudo easy_install3 pip
Update 2015-01-20:
As per https://pip.pypa.io/en/latest/installing.html the current way is:
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
I think that should work for any version
Original Answer:
wget http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
Single Python in system
To install packages in Python always follow these steps:
If the package is for python 2.x: sudo python -m pip install [package]
If the package is for python 3.x: sudo python3 -m pip install [package]
Note: This is assuming no alias is set for python
Through this method, there will be no confusion regarding which python version is receiving the package.
Multiple Pythons/Virtual Envs
Say you have python3 ↔ python3.6 and python3.7 ↔ python3.7
To install for python3.6: sudo python3 -m pip install [package]
To instal for python3.7: sudo python3.7 -m pip install [package]
This is essentially the same method as shown previously.
Note 1
How to find which python? Do one of the following:
~ » python3 -c "import sys; print(sys.version)"
3.9.5 (default, Nov 18 2021, 16:00:48)
your python3 command spawns:
~ » python3
Python 3.9.5 (default, Nov 18 2021, 16:00:48)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Notice python 3.9.5 in the second line.
or say you are using virtual env and see where your python points to:
» which python
/home/ganesh/os/np-test/bin/python
Note 2
Change what python3 or python points to: https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
python3 -m ensurepip
I'm not sure when exactly this was introduced, but it's installed pip3 for me when it didn't already exist.
Older version of Homebrew
If you are on macOS, use homebrew.
brew install python3 # this installs python only
brew postinstall python3 # this command installs pip
Also note that you should check the console if the install finished successfully. Sometimes it doesn't (e.g. an error due to ownership), but people simply overlook the log.
UPDATED - Homebrew version after 1.5
According to the official Homebrew page:
On 1st March 2018 the python formula will be upgraded to Python 3.x and a python#2 formula will be added for installing Python 2.7 (although this will be keg-only so neither python nor python2 will be added to the PATH by default without a manual brew link --force). We will maintain python2, python3 and python#3 aliases.
So to install Python 3, run the following command:
brew install python3
Then, the pip is installed automatically, and you can install any package by pip install <package>.
If your Linux distro came with Python already installed, you should be able to install PIP using your system’s package manager. This is preferable since system-installed versions of Python do not play nicely with the get-pip.py script used on Windows and Mac.
Advanced Package Tool (Python 2.x)
sudo apt-get install python-pip
Advanced Package Tool (Python 3.x)
sudo apt-get install python3-pip
pacman Package Manager (Python 2.x)
sudo pacman -S python2-pip
pacman Package Manager (Python 3.x)
sudo pacman -S python-pip
Yum Package Manager (Python 2.x)
sudo yum upgrade python-setuptools
sudo yum install python-pip python-wheel
Yum Package Manager (Python 3.x)
sudo yum install python3 python3-wheel
Dandified Yum (Python 2.x)
sudo dnf upgrade python-setuptools
sudo dnf install python-pip python-wheel
Dandified Yum (Python 3.x)
sudo dnf install python3 python3-wheel
Zypper Package Manager (Python 2.x)
sudo zypper install python-pip python-setuptools python-wheel
Zypper Package Manager (Python 3.x)
sudo zypper install python3-pip python3-setuptools python3-wheel
This is the one-liner I copy-and-paste:
curl https://bootstrap.pypa.io/get-pip.py | python3
Alternate:
curl -L get-pip.io | python3
From Installing with get-pip.py:
To install pip, securely download get-pip.py by following this link:
get-pip.py. Alternatively, use
curl:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Then run the following command in the folder where you have downloaded
get-pip.py:
python get-pip.py
Warning: Be cautious if you are using a Python install that is managed
by your operating system or another package manager. get-pip.py does
not coordinate with those tools, and may leave your system in an
inconsistent state.
If you use several different versions of python try using virtualenv http://www.virtualenv.org/en/latest/virtualenv.html#installation
With the advantage of pip for each local environment.
Then install a local environment in the current directory by:
virtualenv -p /usr/local/bin/python3.3 ENV --verbose
Note that you specify the path to a python binary you have installed on your system.
Then there are now an local pythonenvironment in that folder. ./ENV
Now there should be ./ENV/pip-3.3
use
./ENV/pip-3.3 freeze to list the local installed libraries.
use ./ENV/pip-3.3 install packagename to install at the local environment.
use ./ENV/python3.3 pythonfile.py to run your python script.
Here is my way to solve this problem at ubuntu 12.04:
sudo apt-get install build-essential libncursesw5-dev libssl-dev libgdbm-dev libc6-dev libsqlite3-dev tk-dev
Then install the python3 from source code:
wget https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tar.xz
tar xvf Python-3.4.0.tar.xz
cd Python-3.4.0
./configure
make
make test
sudo make install
When you finished installing all of them, pip3 will get installed automatically.
This is what I did on OS X Mavericks to get this to work.
Firstly, have brew installed
Install python 3.4
brew install python3
Then I get the latest version of distribute:
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.7.3.zip#md5=c6c59594a7b180af57af8a0cc0cf5b4a
unzip distribute-0.7.3.zip
cd distribute-0.7.3
sudo setup.py install
sudo easy_install-3.4 pip
sudo pip3.4 install virtualenv
sudo pip3.4 install virtualenvwrapper
mkvirtualenv py3
python --version
Python 3.4.1
I hope this helps.
pip is installed together when you install Python. You can use
sudo pip install (module)
or
python3 -m pip install (module).
Please follow below steps to install python 3 with pip:
Step 1 : Install Python from download here
Step 2 : you’ll need to download get-pip.py
Step 3 : After download get-pip.py , open your commant prompt and go to directory where your get-pip.py file saved .
Step 4 : Enter command python get-pip.py in cmd.
Step 5 : Pip installed successfully , Verify pip installation by type command in cmd pip --version
What’s New In Python 3.4
...
pip should always be available
...
By default, the commands pipX and pipX.Y will be installed on all platforms (where X.Y stands for the version of the Python installation), along with the pip Python package and its dependencies.
https://docs.python.org/3/whatsnew/3.4.html#whatsnew-pep-453
so if you have python 3.4 installed, you can just: sudo pip3 install xxx
For python3 try this:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
The good thing is that It will also detect what version of python you have (even if it's an environment of python in your custom location).
After this you can proceed normally with (for example)
pip install numpy
source:
https://pypi.python.org/pypi/setuptools/1.1.6#upgrading-from-setuptools-0-6
Assuming you are in a highly restricted computer env (such as myself) without root access or ability to install packages...
I had never setup a fresh/standalone/raw/non-root instance of Python+virtualenv before this post. I had do quite a bit of Googling to make this work.
Decide if you are using python (python2) or python3 and set your PATH correctly. (I am strictly a python3 user.) All commands below can substitute python3 for python if you are python2 user.
wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-x.y.z.tar.gz
tar -xzvf virtualenv-x.y.z.tar.gz
python3 virtualenv-x.y.z/virtualenv.py --python $(which python3) /path/to/new/virtualenv
source /path/to/new/virtualenv/bin/activate
Assumes you are using a Bourne-compatible shell, e.g., bash
Brilliantly, this virtualenv package includes a standalone version of pip and setuptools that are auto-magically installed into each new virtualenv. This solves the chicken and egg problem.
You may want to create an alias (or update your ~/.bashrc, etc.) for this final command to activate the python virtualenv during each login. It can be a pain to remember all these paths and commands.
Check your version of python now: which python3 should give: /path/to/new/virtualenv/bin/python3
Check pip is also available in the virtualenv via which pip... should give: /path/to/new/virtualenv/bin/pip
Then... pip, pip, pip!
Final tip to newbie Pythoneers: You don't think you need virtualenv when you start, but you will be happy to have it later. Helps with "what if" installation / upgrade scenarios for open source / shared packages.
Ref: https://virtualenv.pypa.io/en/latest/installation.html
To install pip, securely download get-pip.py.
Then run the following:
python get-pip.py
Be cautious if you're using a Python install that's managed by your
operating system or another package manager. get-pip.py does not
coordinate with those tools, and may leave your system in an
inconsistent state.
Refer: PIP Installation
And for Windows 8.1/10 OS Users just open cmd (command prompt)
write this : C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python36-32\Scripts
then
just write this : pip3 install {name of package}
Hint: the location of folder Python36-32 may get different for new python 3.x versions
If you used the command "python get-pip.py", you should have the 'pip' function for Python3. However, 'pip' for Python2 might still be present. In my case I uninstalled 'pip', which removed it from Python2.
After that I ran "python get-pip.py" again. (Make sure that 'get-pip.py' is saved in the same folder as Python3.) The final step was to add the directory with 'pip' command to $PATH. That solved it for me.
=>Easy way to install Python any version on Ubuntu 18.04 or Ubuntu 20.04 follow these steps:-
Step 1: Update Local Repositories:-
sudo apt update
Step 2: Install Supporting Software:-
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Step3: Create directory on your home directory To download the newest release of Python Source Code, navigate to the /python-source-files directory and use the wget command:-
mkdir python-source-files
Step 4: Download the Latest Version of Python Source Code:-
wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
"you can change python version by just modifies this:-"3.7.5" with the version you want example:-"3.5.2"
Step 5: Extract Compressed Files:-
tar –xf Python-3.7.5.tgz
or
tar xvzf Python-3.7.5.tgz
Step 6: Test System and Optimize Python:-
cd python-3.7.5 or your version of python.
Step 7: Now configure(Using the ––optimization option speeds code execution by 10-20%.):-
./configure ––enable–optimizations
OR you can also do this also if you facing ssl error:-
./configure --with-openssl
Step 8: Install a Second Instance of Python:-
sudo make altinstall
"It is recommended that you use the altinstall method. Your Ubuntu system may have software packages dependent on Python 2.x.
OR
If you want to Overwrite Default Python Installation/version:-
sudo make install"
Step 9:Now check Python Version:-
python3 ––version
Step 10: To install pip for python3 just go with this command:-
sudo apt-get install python3-pip