Pip, the python package manager, is used to install, upgrade, remove packages written in Python programming language. In this comprehensive guide, we will be discussing how to install pip and manage python packages such as installing, updating, and removing packages using pip. Additionally, we will see what is virtual environment, how to create it, and how to isolate packages in the virtual environment using venv and/or virtualenv tools.
Table of Contents
Install Pip using Linux Package Managers
On Arch Linux and its variants like Antergos, Manjaro Linux, you can install pip using command:
Python 2:
$ sudo pacman -S python2-pip python2-wheel python2-setuptools
Python 3:
$ sudo pacman -S python-pip python-wheel python-setuptools
On Fedora 21:
Python 2:
$ sudo yum upgrade python-setuptools
$ sudo yum install python-pip python-wheel
Python 3:
$ sudo yum install python3 python3-wheel
Fedora 22:
Python 2:
$ sudo dnf upgrade python-setuptools
$ sudo dnf install python-pip python-wheel
Python 3:
$ sudo dnf install python3 python3-wheel
To get newer versions of pip, setuptools, and wheel for Python 2, enable the PyPA Copr Repo using command:
$ dnf copr enable pypa/pypa
and then run:
$ sudo dnf upgrade python-setuptools
$ sudo dnf install python-pip python-wheel
On CentOS/RHEL:
Pip and wheel is not available in the default repositories of CentOS and RHEL. To install pip on CentOS, RHEL, Scientific Linux and other RPM based systems, enable EPEL repository using command:
$ sudo yum install epel-release
And then run the following command to install pip:
$ sudo yum install python-pip
$ sudo yum install python-wheel
Since setup-tools package is available in the default repositories, you can install it using command:
$ sudo yum upgrade python-setuptools
Python 3:
$ sudo yum install python3-pip
openSUSE:
Python 2:
$ sudo zypper install python-pip python-setuptools python-wheel
Python 3:
$ sudo zypper install python3-pip python3-setuptools python3-wheel
Debian/Ubuntu:
For Python 2.x:
$ sudo apt-get install python-pip
For Python 3.x:
Replace “python” with “python3” for installing Python 3.x version.
$ sudo apt-get install python3-pip
In Ubuntu 12.04 version, pip3 didn't come packaged. If you're using Ubuntu 12.04, you can install pip3 using the following commands:
$ sudo apt-get install python3-setuptools
$ sudo easy_install3 pip
Installing Pip from Binaries
If you'd like to install pip from binaries, just run:
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py
Please note that get-pip.py will also install setuptools and wheel. As I mentioned earlier, some Linux distros doesn't has wheel in the core repositories. In such cases, you may need to add some third party repositories, for example EPEL.
pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4 binaries downloaded from python.org. However, you will need to upgrade pip using command:
$ sudo pip install -U pip
Or,
$ sudo pip install --upgrade pip
To update all (pip, setuptools, whell), run:
$ sudo pip install --upgrade pip setuptools wheel
To find out the installed version of pip/pip3, run:
$ pip --version
Or,
$ pip -V
Sample output would be:
pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
Creating Python Virtual Environments
Before installing any python packages, it is recommended to create a virtual environment. Why do we need to create virtual environment? You might ask. Because, the Python “Virtual Environments” allows us to install a Python package in an isolated location, rather than installing it globally.
Let us say you want to install a python package, for example youtube-dl, that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications?
If you install everything into /usr/lib/python2.7/site-packages or /usr/lib/python3.6/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.
To avoid this, we isolate the packages in the virtual environment. All virtual environments have their own installation directories and doesn't interact or conflict with one another.
We can create isolated python environments using two modules, namely:
- venv,
- virtualenv.
For Python 2.x, you need to install virtualenv. To do so, run:
$ sudo pip install virtualenv
Let us create a virtual environment now.
Using virtualenv:
$ virtualenv <DIR_NAME>
$ source <DIR_NAME>/bin/activate
For Python 3.x, you need to install venv. On Ubuntu-based systems, you can install it using command:
$ sudo apt-get install python3-venv
On Fedora and RHEL, run:
$ sudo dnf install python-virtualenv
Now create virtual environment using venv:
$ python3 -m venv <DIR_NAME>
$ source <DIR_NAME>/bin/activate
Once, you run the above command, you will be placed in your virtual environment immediately.
To deactivate the virtual environment and come to back to your normal shell, run:
$ deactivate
Manage Python Packages using Pip
Now we will see most common basic pip usage with examples.
To view the list of all pip commands and general options, run:
$ pip
You should see an output something like below.
To know what install command does, run:
$ pip install --help
The most common usage of pip is to install from the PyPi (Python Package Index). PyPi is a repository that contains all the packages created by the community of developers in Python.
Install Python Packages
Create virtual environment first as shown below:
Using virtualenv:
$ virtualenv MYENV
Replace MYENV with your own name.
Using venv:
$ python3 -m venv MYENV
Finally, activate it using command:
$ source MYENV/bin/activate
Once, you run the above command, you will be placed inside your virtual environment:
Now, it is time to install some packages. To install a package, for example youtube-dl, run:
$ pip install youtube-dl
This command will install youtube-dl with all its dependencies.
To install a specific version, run:
$ pip install youtube_dl=2017.12.14
To install a version other than specified version, run:
$ pip install youtube_dl!=2017.12.14
Note the '!" in-front of equal symbol.
To install a version equal to or greater than the specified version, run:
$ pip install youtube_dl>=2017.12.14
To install a version in the specific range, for instance greater than or equal to one version and less than another, run:
$ pip install youtube_dl>=2017.12.14, <2017.12.20
To install a version that’s “compatible” with a certain version:
$ pip install youtube_dl~=2017.12.14
Download Packages
To download a package with all dependencies (without installing it), run:
$ pip download youtube-dl
List all Installed Packages
To find which packages were installed by pip, run:
$ pip freeze
Or,
$ pip list
These commands will display all installed packages using pip in your system.
Search Packages
To search for a specific package, for example youtube-dl, run:
$ pip search youtube-dl
This command will search and display the result that matches the string "youtube-dl".
Update Packages
To list all outdated packages in a simple tabular column format, run:
$ pip list --outdated --format=columns
To update an outdated package, run:
$ pip install --upgrade youtube-dl
We can also dump all packages in a file and update them all in one go. First, export all files to a file:
$ pip freeze > mypackages.txt
Now update all packages at once using command:
$ pip install -r mypackages.txt --upgrade
Or,
$ pip install -r mypackages.txt -U
If the above command didn't work for any reason, use the following command to update all packages at once:
$ pip freeze --local | grep -v '^\e' | cut -d = -f 1 | xargs -n1 pip install -U
Export all Installed Packages in a File
Sometimes, you might want to export all installed packages in a file to test them in a different environment. To do so, run:
$ pip freeze > MYENV_packages.txt
Now, deactivate the current virtual environment:
$ deactivate
and create a new using commands:
$ virtualenv MYENV1
Replace MYENV1 with your own name.
Using venv:
$ python3 -m venv MYENV1
Activate the newly created environment:
$ source MYENV1/bin/activate
Now, install all packages which we exported earlier.
$ pip install -r MYENV_packages.txt
To install all at once without user interaction, run:
$ pip install -r MYENV_packages.txt -y
Similarly, you can remove all packages from the list using command:
$ pip uninstall -r MYENV_packages.txt -y
View Package Information
To view the details of a package, run:
$ pip show youtube-dl Name: youtube-dl Version: 2017.12.14 Summary: YouTube video downloader Home-page: https://github.com/rg3/youtube-dl Author: Sergey M. Author-email: dstftw@gmail.com License: UNKNOWN Location: /home/sk/MYENV/lib/python3.6/site-packages Requires:
View Package Dependencies
We can visualize the dependencies of all installed packages using "pipdeptree" tool.
Install it using command:
$ pip install pipdeptree
Once installed, you can view the dependency tree using command:
$ pipdeptree
Uninstall Packages
To uninstall/remove an installed package, run:
$ pip uninstall youtube-dl
To uninstall multiple packages, specify them with comma separated like below:
$ pip uninstall package1, package2
To remove all python packages installed using pip, run:
$ pip freeze | xargs pip uninstall -y
Sometimes pip doesn't allow you to uninstall packages owned by the OS. In such cases, you can uninstall all the packages which are not owned by the OS using command:
$ pip uninstall -y $(pip freeze | sed 's;==.*;;g' | tr '\n' ' ')
As I mentioned in the previous section, we can dump all installed packages in a file and uninstall them from the list using command:
$ pip uninstall -r MYENV_packages.txt -y
At this stage, you might get some idea about pip and its usage. For more details, refer the official documentation and the pip help section by running the following command:
$ pip --help
Or just,
$ pip
Suggested Read:
- Pipenv - The Officially Recommended Python Packaging Tool
- Pipx – Install And Run Python Applications In Isolated Environments
Troubleshooting
This error message typically occurs when the pip
executable script cannot find the pip
module in your Python environment. This might be due to an incomplete or corrupted installation of pip
.
To fix the No Module Named Pip error, refer to the following link:
In this detailed tutorial, we discussed what is Pip, how to install Pip and how to manage Python packages using Pip with example commands. If you happen to often use Python, Pip is strongly recommended to have in your toolkit.
Resource:
1 comment
Very usefull! Thank You!