Python — Pip Commands you might not know

rahulbanerjee99

Rahul Banerjee

Posted on March 18, 2021

Python — Pip Commands you might not know

Pip can be used to do more than just install packages. A list of common pip commands you might find useful are listed in the article

Table of Contents

  1. What is Pip?
  2. Setting up Virtual Environment
  3. Update Pip
  4. Install A Package
  5. Update Package
  6. Install Specific version of a package
  7. Uninstall a Package
  8. Information about an installed package
  9. List all installed packages
  10. List all installed packages which are not up to date
  11. Generate a requirements.txt file
  12. Install all dependencies from requirements.txt file
  13. Verify installed packages have compatible dependencies
  14. A hack to update all packages

What is Pip?

Pip is a package manager tool for Python. If you have worked with Python before, you have most probably worked with pip as well. The most common use of pip is to install packages but there are a few other helpful commands you might not know.

Pip fetches packages from PyPI

Setting up Virtual Environment

Install virtualenv

On macOS and Linux:

python3 -m pip install --user virtualenv

On Windows:

python -m pip install --user virtualenv

Create virtual environment

On macOS and Linux:

python3 -m venv env

On Windows:

python -m venv env

This will create a virtual environment named venv

Activate Virtual Environment

Ensure your file paths are correct.

On macOS and Linux:

source venv/bin/activate

On Windows:

venv\Scripts\activate

Select Interpreter for VS Code

  • Press ‘Ctrl+Shift+P’ for Windows/Linux and ⇧⌘P for Mac to open the command palette
  • Search for ‘Select Interpreter’
  • Click ‘Enter Interpreter Path’ > ‘Find’
  • A file explore should open up, go to venv>Scripts>python.exe

Deactivate/Leave the virtual environment

Type the following command

deactivate

Update Pip

python -m pip install --upgrade pip

Install A Package

pip install <package-name>

If I want to install pandas, a python package, I’d type the following

pip install pandas

This is going to install the latest version of a package available on PyPI

Update Package

pip install -U <package name>

If I want to update my version of pandas, I’d type the following

pip install -U pandas

If an old version of the package exists, pip will first uninstall that version. It will then install the most up-to-date version from PyPI. If an up-to-date version is already installed, pip won’t do anything.

Install Specific version of a package

pip install <package-name>==<version>

If I wanted to install pandas version 1.20, I’d type the following

pip install pandas==1.2.0

If a version of a package is already installed, pip will uninstall the existing package and install the specified version of the package

If you want to install a version of a package newer than a specific version, type the following

pip install <packagename>>=<version>

To install a version of pandas, newer than 1.2.0

pip install pandas>=1.2.0

Uninstall a Package

pip uninstall pandas

Information about an installed package

pip show <package name>

To get information about the package pandas which I installed earlier

pip show pandas

If you try to get information about a package that is not installed in your environment, pip will return a warning ‘Package Not Found’

List all installed packages

pip list

This will list all the installed packages in the environment in a tabular format

An alternative is the following

pip freeze

List all installed packages which are not up to date

pip list -o

Generate a requirements.txt file

pip freeze > requirements.txt

Install all dependencies from requirements.txt file

pip install -r requirements.txt

Pip will ignore all the packages which have already been installed before

Verify installed packages have compatible dependencies

pip check

A hack to update all packages

  • Generate a requirements.txt file
pip freeze > requirements.txt
  • Open the requirements.txt file and replace all instances of == with >=
  • Installed dependencies from requirements.txt 
pip install -r requirements.txt --upgrade
  • Any packages which are up to date will be ignored and packages with a newer version available on PyPI will be installed.
💖 💪 🙅 🚩
rahulbanerjee99
Rahul Banerjee

Posted on March 18, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related