How to List Installed Python Packages

How to Convert PNG to TIFF in Python?

In this era of top-notch software development, having pre-written code is nothing less than a blessing.

If you are working with Python, you must need to know about Python packages and how they make your life way easier than just re-writing codes repeatedly.

Due to the vast, diverse applications and benefits this language provides and the fact that it contributes to the open-source platform, there are way too many packages available on the internet. Even you can make your own and share it with others.

Many times a single project requires multiple packages that may or may not be closely related to each other. So it is hard to keep track of the installed packages on your system. Normally, whenever you need a package, you just install it without actually wondering about the total number of modules or packages already available on your system. So there is a need to list down the packages you have been installing since the beginning.

In this article, we will be explaining Python packages and why these are a necessary part of our programming lifestyle. We will be exploring different ways in which we can visualize and list down all the packages available on our system, using different methods and features provided by Python.

 

What is a Python Package?

When working on a very large application with a great number of modules, it becomes difficult to keep track of this many modules.

It happens because modules have similar names or they even have the same functionality. At this point, one thinks of a way to organize and manage these files.

The solution is the hierarchical structuring in the form of packages, which allows us to avoid any discrepancy which may arise due to module name or functionality.

So, a Python package is a set of modules. These modules are somewhat related to each other and therefore are placed on the same package.

Since there are multiple packages already available for a great variety of resources on an open-source platform, if there is a need for a specific module, one can import and install these packages easily.

 

Using Pip to List Down Installed Packages

There are two ways to list down packages using pip.

  1. Pip list
  2. Pip freeze

Pip list and pip freeze, both will generate a list of installed packages. The only difference visible will be in the differently formatted results.

It is important to remember that the pip list will list all the installed packages regardless of the method used to install them. On the other hand, pip freeze will list packages that have been installed using pip only. So that is another main difference between the ways.

Consider the following example:

pip list

The output of the above:

Package                             Version

---------------------------------- ----------

absl-py                              0.7.0

For pip freeze:

pip freeze

The output of the above:

absl-py==0.7.0

 

Using Pip to List Installed Python Packages in Console

Installed packages can be list down in the console using a simple script. This script uses pip to import pkg resources and then display their contents.

The following script can be utilized for this purpose:

import pkg_resources

installed_packages = pkg_resources.working_set

installed_packages_list = sorted(["%s==%s" % (i.key, i.version)

for i in installed_packages])

print(installed_packages_list)

The output will be in the following format:

['absl-py==0.7.0', 'adodbapi==2.6.0.7', 'alabaster==0.7.12', 'alembic==1.0.7', 'amqp==2.4.1', 'anyjson==0.3.3',

 

Listing Down Modules in Console Without Pip

We can also list down modules in the console without using pip. To do so, we use the help command, in the following way:

help("modules")
Please wait a moment while I gather a list of all available modules...

BaseHTTPServer      brain_nose          markupbase          stat

Bastion             brain_numpy         marshal             statvfs

CGIHTTPServer       brain_pkg_resources math                string

Canvas              brain_pytest        matplotlib          stringold

...

...

It is important to mention some drawbacks to using this method, some of which are as follows:

  • If there are too many packages installed in the system, this method can take a long time to import each module before it can search that module’s path for sub-modules.
  • Modules that have code outside of an if __name__ == “__main__”: code block, and if user input is expected, may cause the code to enter an infinite loop or hang.

 

Using Pipenv to List Down Installed Packages

A simple command can be used in this method. This works in a pipenv environment and generates output from a pipfile.lock file. An advantage of using this is method is that it will also list down the dependencies in the output. Use the following command:

pipenv lock -r

Output:

-i https://pypi.org/simple

certifi==2019.11.28

chardet==3.0.4

idna==2.9

requests==2.23.0

urllib3==1.25.8

 

Using Anaconda Navigator to List Packages

If you are using an Anaconda environment for Python, then Anaconda Navigator will help you to easily view the list of installed packages.

Follow the given steps:

  • Start the Anaconda Navigator application.

Anaconda Navigator

  • Select Environments in the left column.

Anaconda Environments

  • A dropdown box at the center-top of the GUI should list installed packages. If not, then select Installed in the dropdown menu to list all packages.

Anaconda Dropdown Box

 

Using Conda to List Packages

The conda list command can be used to list all packages in a conda environment:

conda list

Output:

# packages in an environment at C:\Anaconda2_4.3.1:

#

_license                  1.1 py27_1

alabaster 0.7.9           py27_0

 

Conclusion

In this brief article, we explored the importance of packages and the availability of thousands of open-source ready-to-use packages and the need for organizing and listing down the available packages in your system.

We dig down to different methods. We used pip list and pip freeze and then used pip in the console environment. Furthermore, the help function was used in the console to list installed Python packages without the use of pip.

Methods to be used in different environments were also touched, including Anaconda, Conda, and Pipenv. And 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