How to List Python Packages (Globally Installed vs Locally Installed)

How to Convert PNG to TIFF in Python?

A package manager is a tool that allows developers to create a project environment and import external dependencies easily and efficiently.

It allows you to make the most out of the tools at your disposal without reinventing the wheel.

The Python Package Index (PyPI) indexes all the libraries and applications available in a beautiful manner covering all possible use cases. Problems arise when newcomers try the installation of these packages. They usually run into problems like missing permissions and incompatible library dependencies, breaking their installations in surprising ways.

In Python, we use Python package managers to install packages. Package manager installs and manages packages for a Python environment, both locally or globally.

By default, managers install packages globally under:

/usr/local/bin/ (Unix) or \Program Files\ (Windows).

These packages are accessible to all users. Similarly, local packages are (by default) installed under:

~/.local/bin/ (Unix) or \Users\Username\AppData\Local\Programs\ (Windows).

 

Global vs Local Installation

Imagine that you have two applications, each requiring a different version of the TensorFlow package. What version of TensorFlow are you going to install? The answer is simple. You install them both Locally.

Let us dive further into it and see what a Local and Global installation means:

  • Local Installation: These packages are installed under your project/virtual environment’s directory. For instance, if you want to use TensorFlow 2 package for a project, you would install it locally inside your project’s directory.
  • Global Installation: These packages are installed under /usr/local/bin/ (Unix) or \Program Files\ (Windows) and are accessible globally (as their names suggest!). Typically, a global installation is required when you want to use a single package that is to be used by every application running on the system. That is, a single package installation is shared among the system.

Generally, packages are installed locally to avoid unexpected errors in applications. For instance, an app can be written with version xx of a package. Upgrading that package could cause unexpected behavior. Hence it is recommended to install packages locally for every project.

In this article, we will see how to list global and local installed Python packages for the following package managers:

  • Pip
  • Pipevn
  • Conda

Additionally, we will also cover listing modules from a standard Python environment.

 

Listing Installed Packages

Pip package manager is capable of listing both global and locally installed packages. Pipenv and Conda, on the other hand, can list local packages installed in their environments.

Tip: As a best practice, use a separate virtual environment to install packages for projects. This isolates the environment to avoid collision problems that arise in shared dependences.

 

Listing Global Packages

By default, pip packages are installed globally under ~/.local/bin/ (Linux) or \Users\Username\AppData\Local\Programs\ (Windows). Use the following command to list globally installed packages and their version:

pip list

or

pip freeze

To search and list a specific globally installed package, use:

For A Linux Based Environment

pip freeze | grep <packagename>

For A Windows Based Environment

pip freeze | findstr <packagename>

 

Listing Local Packages

Packages installed locally are accessible only to a single user. The following section shows how to list locally installed packages in Pip, Pipenv, and Conda.

Pip

Pip can install packages locally with -user option. To list locally install packages in pip, use:

pip list --user

or

pip freeze --user

To search and list a specific globally installed package, use:

For A Linux Based Environment

pip freeze --user | grep <packagename>

For A Windows Based Environment

pip freeze --user | findstr <packagename>

 

Pipenv

To list locally installed packages and their dependencies within the Pipenv environment, navigate to the Pipenv project and use:

pipenv lock -r

In Pipenv, packages and their dependence information are stored in a pipfile.lock file

 

Conda

Packages can be listed in Conda in either Anaconda prompt or using the Anaconda Navigator. We cover both methods below:


Using Anaconda Prompt

To list locally installed packages within Conda, open the Anaconda prompt as follows:

  • Windows: Click Start and search for Anaconda Prompt.

Anaconda Prompt

  • Linux: Open Applications > System Tools > Terminal.

In the Anaconda prompt, use:

conda list

to list all locally installed packages.

Anaconda List in Prompt


Using Anaconda Navigator

Follow these steps:

  • Open Anaconda Navigator.

Anaconda Navigator

  • On the left-hand pane, select Environment.

Anaconda Environments

  • A drop-down at the center-top lists installed packages.

Anaconda Dropdown Box

Note: If the list is not visible, select installed from the dropdown to list all packages.


 

Listing Using Standard Python Interpreter

The above steps included listing packages from a package manager environment. In this section, we will look at how to list installed packages using a standard Python interpreter.

Using pkg_resources

The Pkg_resourcesmodule provides an easy way to get all installed packages in a standard Python runtime. A simple call pkg_resources.working_set does the job. The step by step procedure is as follows:

Import pkg_resources

After importing the pkg_resources module, we just need to iterate over the working_set and build a Python list of all installed packages along with their version # and installation directory.

packages = [p for p in pkg_resources.working_set]

Print

We can now pretty print individual packages using the pprint module.

from pprint import pprint

pprint(packages)

 

Conclusion

Managing packages for a project is critical when it comes to development. Its importance can be seen from the fact that package managers are included by default in a standard Python installation.

Unmanaged packages can result in incompatible library dependencies, breaking our installations or generating unexpected results, making the lives of developers a hell lot more difficult than it already is! (lol)

In this article, we looked at different ways to list global and locally installed packages in Python. We described the steps to list installed packages for commonly used package managers, which include Pip, Pipenv, and Conda environments for Linux and Windows-based PCs.

Additionally, we also wrote a small Python script that lists installed packages using the pkg_resources library. We hope this article was insightful and helped you in listing Python packages globally as well as locally.

The next time you encounter an expected error, don’t forget to briefly go over the installed package versions. As always, happy coding!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share