Install and upgrade Python packages using pip and virtual environment on Windows, Linux, and Mac

Renesh Bedre    5 minute read

Install and upgrade Python packages

  • pip is Python’s official package manager and is a recommended method for installing, upgrading, and uninstalling the Python packages.
  • Check below how to install and upgrade the pip on Windows, Linux, and Mac for managing the Python packages

Installing and upgrading pip

  • pip is a package (bundle of software) installer for Python.
  • In order to use pip, it is required to install Python (Python 3 >=3.4). You can check here for installing Python 3 on Windows, Linux, and Mac from the source. Additionally, you can also check the Python installation guide for installing Python from OS specific package manager.

Note: If you have installed Python 3 from the source, you should add the Python executable in the system path variable to be able to run Python commands from any path in OS. Check how to add Python executable in system path variable for Windows, Linux, and Mac

Check pip version

  • If you have already installed Python 3 (>=3.4), you can run the below shell command to check the pip version
  • If you get error as “ ‘pip’ is not recognized as an internal or external command “ on Windows, then you need to add pip installation directory to the system variable (check here how to do this)
# Windows, Linux, and Mac
# using pip
pip --version
# output if installed
pip 22.0.3

python -m pip --version
# output if installed
pip 22.0.3

If pip is not installed, use ensurepip to install pip (see below)

Install pip using ensurepip, conda, or get-pip.py script

# Windows, Linux, and Mac
python -m ensurepip --default-pip

# within conda environment
conda install pip

To install using get-pip.py, first download the script and run following command,

# Windows, Linux, and Mac
python get-pip.py

To install on Ubuntu, use the following command

# Ubuntu Linux
sudo apt update
sudo apt install python3-pip

upgrading pip to the latest version

# Windows, Linux, and Mac
# using pip
pip install --upgrade pip

# using Python
python -m pip install --upgrade pip

Installing a specific version of pip

# Windows, Linux, and Mac
# using pip
pip install --upgrade pip==21.0.1

# using Python
python -m pip install --upgrade pip==21.0.1

# using easy_install (deprecated)
easy_install pip==21.0.1

Installing and upgrading Python packages using pip

Note: pip should be used for installing/upgrading of Python packages and it is not ideal for installing/upgrading Python

Installing Python packages

# Windows, Linux, and Mac
# using pip (replace bioinfokit with required python package name)
pip install bioinfokit

# using Python
python -m pip install bioinfokit

Upgrading installed Python packages to the latest version

# Windows, Linux, and Mac
# using pip (replace bioinfokit with required python package name)
pip install --upgrade bioinfokit

# using Python
python -m pip install --upgrade bioinfokit

# upgrade all installed packages at same time using pip-review (need to install pip-review package)
pip-review --auto

Install a specific version of Python packages

General syntax to install the specific version of Python packages is pip install <python_package_name>==<version>. If you don’t specify the version, the latest version of the Python package will be installed.

If you want to install the older version, you need to provide the specific version of Python package. For example, to install the 1.22.0 version of NumPy, use the command as pip install numpy==1.22.0

See more examples,

# Windows, Linux, and Mac
# using pip (replace bioinfokit with required python package name)
pip install bioinfokit==2.0.0

# using Python
python -m pip install bioinfokit==2.0.0

Install specific packages from requirements file

  • requirements file (requirements.txt) let you install the multiple Python packages with specific or latest versions at once
  • requirements file (requirements.txt) contains the name of Python packages that need to be installed
# Windows, Linux, and Mac
# using pip
pip install -r requirements.txt

# using Python
python -m pip install -r requirements.txt

Uninstall Python packages

# Windows, Linux, and Mac
# using pip (replace bioinfokit with required python package name)
pip uninstall bioinfokit

# using Python
python -m pip uninstall bioinfokit

Check Python package version

# Windows, Linux, and Mac
# using pip (replace bioinfokit with required python package name)
pip show bioinfokit
# output
Name: bioinfokit
Version: 2.0.4
...

pip freeze # it will list all installed packages with their version
# output
adjustText==0.7.3
bioinfokit==2.0.4
...

# using Python
python -c "import bioinfokit; print(bioinfokit.__version__)"
2.0.4

# using Python interpreter
>>> import bioinfokit
>>> bioinfokit.__version__
'2.0.4'

Virtual environments to install Python packages

  • When you have multiple applications to run and each application has a specific requirement of Python packages, then running those multiple applications with one Python installation is not convenient. For example, if one application needs v1.0 of a specific Python package and another application needs v2.0 of the same Python package.
  • In such situations, the virtual environment is a helpful tool to install the specific versions of Python packages required for each application. You can use either Python or conda to create virtual environment.

Create and activate a virtual environment

# using Python3
python -m venv example_venv

# using conda
conda create --name example_venv

To use a virtual environment, you need to first activate it

# Linux and Mac
source example_venv/bin/activate

# Windows
example_venv/Scripts/activate

# for virtual environment created using conda 
conda activate example_venv

Once virtual environment is activated, you should see virtual environment name (example_venv) on your command prompt

Installing Python packages in a virtual environment

Once the virtual environment is activated, you can install the specific Python packages as discussed in the previous section

Enhance your skills with courses on Python

References

Learn more about Python

If you have any questions, comments or recommendations, please email me at reneshbe@gmail.com

If you enhanced your knowledge and practical skills from this article, consider supporting me on

Buy Me A Coffee


This work is licensed under a Creative Commons Attribution 4.0 International License

Some of the links on this page may be affiliate links, which means we may get an affiliate commission on a valid purchase. The retailer will pay the commission at no additional cost to you.

Tags:

Updated: