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

Renesh Bedre    3 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
# Windows, Linux, and Mac
# using pip
pip --version
# output if installed
pip 20.1.1

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

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

Install pip using ensurepip and conda

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

# within conda environment
conda install 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

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.

# 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.

Create and activate a virtual environment

# requires Python3
python -m venv 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

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

References

Learn more about Python

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

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

Tags:

Updated: