How to manage Python packages using Pip
PIP, a python package manager, is used to install, update, remove packages written in the Python programming language. In this tutorial, we will discuss how to install pip and manage python packages such as installing, updating and removing packages using pip. We will also see what a virtual environment is, how to create one, and how to isolate packages in a virtual environment using the venv and / or virtualenv tools. Let’s get down to business.
Installation
Install pip using package managers
On Arch Linux and its variants such as Antergos, Manjaro Linux, you can install pip using the command:
Python 2:
sudo pacman -S python2-pip
Python 3:
sudo pacman -S python-pip
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 wheels for Python 2, enable PyPA COPR Repo with the 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 are not available in the default CentOS and RHEL repositories. To install pip on CentOS, RHEL, Scientific Linux and other RPM based systems, enable Epel repository using the 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 the setup-tools package is available in the default repository, you can install it using the command:
sudo yum upgrade python-setuptools
OpenSUSE:
Python 2:
sudo zypper install python-pip python-setuptools python-wheel
Python 3:
sudo zypper install python3-pip python3-setuptools python3-wheel
sudo apt-get install python-pip
Replace “python” with “python3” for Python 3.
There is no PIP3 version in Ubuntu 12.04. If you are using Ubuntu 12.04, you can install PIP3 using the following commands:
sudo apt-get install python3-setuptools
sudo easy_install3 pip
Install pip from binaries
If you want to install pip from binaries, just run:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
Note that get-pip.py will install Setuptools and well as well. As we mentioned earlier, some Linux distributions don’t have well in the main repositories. In such cases, it may be necessary to add multiple third party repositories such as EPEL.
pip is already installed if you are using Python 2> = 2.7.9 or Python 3> = 3.4 binaries downloaded from python.org. However, you will need to upgrade pip using the command:
sudo pip install -U pip
or,
sudo pip install --upgrade pip
To update everything (pip, setuptools, whell), run the following command:
sudo pip install --upgrade pip setuptools wheel
To find out the installed version of pip / pip3, run the following command:
pip --version
or,
pip -V
An example output would be like this:
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)
Creation of virtual environments
Before installing any pyton packages, it is recommended to create a virtual environment. Why do we need to create a virtual environment, you may ask? Because a “virtual environment” allows you to install a Python package in an isolated location rather than installing it globally.
Let’s say you want to install a Python package like YouTube for version 1 of LibFoo, but another application requires version 2. How can you use both of these applications? If you set everything to /usr/lib/python2.7/site-packages or /usr/lib/python3.6/site-packages (or something the standard position of your platform is), it is easy to find yourself in a situation where you have unintentionally update an app that shouldn’t be upgraded… To avoid this, we allocate packages in a virtual environment. All virtual environments have their own installation directories and do not interact or conflict with each other.
We can create isolated pyton environments with two tools.
- venv
- virtualenv
If you are using Python 3.3 and later, venv is installed by default. For Python 2.x, you need to have virtualenv installed. To do this, run the command:
sudo pip install virtualenv
Let’s create a virtual environment now.
Using virtualenv:
virtualenv <DIR_NAME>
source <DIR_NAME>/bin/activate
Using venv:
python3 -m venv <DIR_NAME>
source <DIR_NAME>/bin/activate
After you run the above command, you will be placed in a virtual environment.
To shut down the virtual environment and switch back to the normal shell, run the following command:
deactivate
Python package management using pip
We now see the most common basics of using PIP with examples.
In order to view a list of all commands, individual and general parameters, run the following command:
pip
To know what the install command does, run the following command:
pip install --help
The most common use of pip is to install from PyPi (Python Package Index)PyPi is a repository containing all packages created by the Python developer community.
Installing packages
Creating a virtual environment:
Using virtualenv:
virtualenv MYENV
Replace MYENV with your own name.
Using venv:
python3 -m venv MYENV
Finally, enable it with the command:
source MYENV/bin/activate
After you run the above command, you will be placed in a virtual environment.
Now is the time to install some packages. To install a package like YouTube-DL, run the command:
pip install youtube-dl
This command will install YouTube-DL with all its dependencies.
To install a specific version, run the following command:
pip install youtube_dl=2017.12.14
To install a version other than the specified version, run the following command:
pip install youtube_dl!=2017.12.14
Pay attention to the “!” Symbol.
To install a version equal to or greater than the specified version, run the command:
pip install youtube_dl>=2017.12.14
To set a version to a specific range, for example, greater than or equal to one of the versions, and less than the other, run the following command:
pip install youtube_dl>=2017.12.14, <2017.12.20
To install a version “compatible” with a specific version:
pip install youtube_dl~=2017.12.14
Download packages
To download a package with all dependencies (without installation), run:
pip download youtube-dl
List of all installed packages
To find out which packages pip installed, run the command:
pip freeze
or,
pip list
These commands will list all installed packages using pip on your system.
Search for packages
To search for a specific package, for example YouTube-DL, run the command:
pip search youtube-dl
This command will search and display a result that matches the string “YouTube-DL”.
Updating packages
To update an outdated package, run the command:
pip install --upgrade youtube-dl
To get a list of all obsolete packages in a simple tabular format, run the following command:
pip list --outdated --format=columns
Output example:
Package Version Latest Type ----------------- ------------ ---------- ----- alembic 0.8.10 0.9.6 sdist argcomplete 1.9.2 1.9.3 wheel arrow 0.10.0 0.12.0 sdist arxiv2bib 1.0.7 1.0.8 sdist asn1crypto 0.22.0 0.24.0 wheel attrs 17.2.0 17.3.0 wheel awesome-finder 1.0.5 1.1.1 wheel cffi 1.10.0 1.11.2 wheel classifier 1.7 2.0 sdist cryptography 1.9 2.1.4 wheel decorator 4.0.11 4.1.2 wheel fuzzywuzzy 0.15.0 0.15.1 wheel hyperlink 17.2.1 17.3.1 wheel idna 2.5 2.6 wheel imageio 2.1.2 2.2.0 sdist lxml 3.8.0 4.1.1 wheel Mako 1.0.6 1.0.7 sdist MarkupSafe 0.23 1.0 sdist papis 0.4.6 0.5.0 sdist parse 1.6.5 1.8.2 sdist praw 4.3.0 5.3.0 wheel prawcore 0.7.0 0.13.0 wheel pyasn1 0.2.3 0.4.2 wheel pyasn1-modules 0.0.9 0.2.1 wheel pycairo 1.13.3 1.15.4 sdist pyOpenSSL 17.1.0 17.5.0 wheel pyperclip 1.5.27 1.6.0 sdist python-dateutil 2.6.0 2.6.1 wheel python-magic 0.4.13 0.4.15 wheel python-ptrace 0.8.1 0.9.3 wheel python-xlib 0.20 0.21 wheel redlib 1.5.6 1.5.7 sdist requests 2.12.5 2.18.4 wheel requests-toolbelt 0.7.0 0.8.0 wheel setuptools 36.6.0 38.2.4 wheel speedtest-cli 1.0.6 1.0.7 wheel SQLAlchemy 1.1.5 1.1.15 sdist tabulate 0.7.7 0.8.2 sdist torrench 1.0.56 1.0.61 wheel tqdm 4.11.2 4.19.5 wheel Twisted 17.5.0 17.9.0 sdist urllib3 1.21.1 1.22 wheel wpm 1.33 1.34 sdist youtube-dl 2017.10.15.1 2017.12.14 wheel zope.interface 4.3.3 4.4.3 wheel
We are now updating outdated packages in most of the latest available versions using the command:
pip freeze --local | grep -v '^e' | cut -d = -f 1 | xargs -n1 pip install -U
Export all installed packages to a file
Sometimes, you can export all installed packages to a file to test them under different conditions. To do this, run the following command:
pip freeze > MYENV_packages.txt
Now, disable the current virtual environment:
deactivate
and create a new one using the 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 the packages we exported earlier.
pip install -r MYENV_packages.txt
To install everything at once without user intervention, run the following command:
pip install -r MYENV_packages.txt -y
Alternatively, you can remove all packages from the list using the command:
pip uninstall -r MYENV_packages.txt -y
Viewing package information
To view detailed information about a package, run the command:
$ 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: [email protected] 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 the pipdeptree tool.
Install it using the command:
pip install pipdeptree
After installation, you can view the dependency tree using the command:
pipdeptree
Removing packages
To uninstall / remove an installed package, run the command:
pip uninstall youtube-dl
To remove multiple packages, specify them by separating:
pip uninstall package1, package2
In order to remove all pyton packages installed with pip, run the command:
pip freeze | xargs pip uninstall -y
Sometimes pip won’t allow you to remove packages belonging to the OS. In such cases, you can remove all packages that are not owned by the OS using the command:
pip uninstall -y $(pip freeze | sed 's;==.*;;g' | tr 'n' ' ')
As I said in the previous section, we can dump all installed packages to a file and remove them from the list using the command:
pip uninstall -r MYENV_packages.txt -y
At this point, you can get some idea of pip and how to use it. For more details, refer to official documentation (https://pip.pypa.io/en/stable/user_guide/) and the pip help section by running the following command:
pip --help
or simply,
pip
It’s all. Be in touch!