Using pip to install Python packages

1 minute read comments

pip is another open source and command line based package installer and manager for Python like conda. We can use it to install Python packages from the Python Package Index (PyPI) and other indexes. It works cross-platform, i.e., we can install it on any operating system (Linux, Mac, Windows). Learn in this post, how to use pip for installing and managing Python packages in your projects.

img

Before we begin, I recommend to create a virtual environment either with conda or with Python’s built-in venv command.

Does it matter for pip, how we have created a virtual environment?
No. For using pip it doesn’t matter, whether we have created the virtual environment with conda or with Python. With conda, just an extra step of installing pip into the virtual environment might be required: conda install -y pip. Since pip usually comes along with the default Python installation, we do not need to extra install it, when we create the virtual environment with Python.

Installing and managing Python packages via pip

Installing Python packages via pip is quite easy:

pip install PACKAGENAME

or

pip install PACKAGENAME1 PACKAGENAME2 PACKAGENAME3

if you’d like to install several packages at once. For instance, a typical package installation for data scientists could again be

pip install numpy matplotlib scipy pandas seaborn pingouin notebook jupyter_contrib_nbextensions

We can search for a specific package with pip,

pip search PACKAGENAME

or visit the PyPI website.

To list all installed packages, type:

pip list

We can remove installed packages via

pip uninstall PACKAGENAME

To list all outdated packages, type:

pip list --outdated

and to update a package:

pip install --upgrade PACKAGENAME

To update pip itself, just type:

pip install --upgrade pip

Install packages from a requirements.txt file

To install packages, that are specified in a “requirements.txt” file, simply type

pip install -r requirements.txt

where you can replace the argument “requirements.txt” by the path to the actual “requirements.txt” file.

Install code from GitHub: How to install Python code from GitHub, that is not available via conda or pip, is described in this post.


Comments

Commenting on this post is currently disabled.

Comments on this website are based on a Mastodon-powered comment system. Learn more about it here.