Back to Tutorials

Installing Python Packages with Pip

What is a Package Installer?

A package installer is a tool that helps you download, install, update, and manage software packages from a central repository or other sources. Package installers streamline the process of adding external functionality to your project by handling the installation of these packages and their dependencies without you having to manually download or configure them

What is pip?

pip is the recommended package installer for Python. pip allows developers to easily integrate libraries and frameworks into their projects by downloading packages from the Python Package Index (PyPI) or other repositories. PyPI is the default package repository for the Python community.

Installing Packages

Best Practice: Always activate your virtual environment prior to installing a package.

Let us activate our virtual environment, then enter the following command in your terminal:

1
pip install [package_name]

You can also specify a particular version of the package if needed:

1
pip install [package_name]==[version]

This command retrieves the package from PyPI (or another source if specified) and installs it into your environment. pip will also handle the installation of the package's dependencies. When you install a package, pip checks whether these required dependencies have already been installed. If not, pip will automatically install them for you.

Lessons Learned: Do not remove other dependencies that were installed along with your main package.

For example, let's say you are installing the requests library, which is used to make HTTP requests in Python. You might notice pip is installing other packages along with it. This is because requests rely on other packages, such as urllib3 and certifi, to function correctly. These additional packages are the dependencies, and without them, the main package wouldn't work as intended.

Note: In my previous tutorial, Installing Python on Windows, we did not add python.exe to the PATH environment variable. This setup means that we do not have access to the python.exe globally, so typing pip before activating your virtual environment will not work. This setup is a good sanity check in case you forget to activate your virtual environment prior to installing a package.

Managing Installed Packages

To view a list of all packages installed in your current environment, use the following command in your terminal:

1
pip list

This will display the installed packages along with their respective versions. If you want to check if a specific package is installed, you can use:

1
pip show [package_name]

To keep your packages up to date, you can upgrade a package by running:

1
pip install --upgrade [package_name]

Uninstalling Packages

To remove a package you no longer need, simply run the following command in your terminal:

1
pip uninstall [package_name]

Pip will prompt you for confirmation before removing the package from your environment.

Take Note: The pip uninstall [package_name] command will only remove the specified package itself, not its dependencies.

Unfortunately, pip doesn’t have a built-in command to uninstall unused package's dependencies automatically. If you want to remove a package's dependencies, you'll need to check for and uninstall unused packages manually.

If you are using packages other than venv, some keep track of installed packages and their dependencies. So, when you uninstall a package, it ensures that unused dependencies are also removed.