Python get all installed modules

How to list the 3rd party projects (libraries, etc.) installed using pip in Python 3 [closed]

Is there any Python script or Python code that can give me the list of Python projects (distribution packages: libraries, etc.) installed (with pip, for example) so that I can iterate over them in a loop? It should be working in python3.x .

I found this answer, I think this is your question Check this out: stackoverflow.com/questions/739993/…

4 Answers 4

There is a very simple and straightforward way to do this directly in Python’s standard library. Use importlib.metadata .

#!/usr/bin/env python3 import importlib.metadata for distribution in importlib.metadata.distributions(): print(distribution.metadata['Name'], distribution.metadata['Version']) 

This will work for Python 3.8+. For versions older than 3.8 I would recommend using importlib-metadata to get the exact same features.

The pip list command will give you a list of all packages and their versions installed:

Package Version ----------------------------- ------------ ailment 9.0.4663 alabaster 0.7.12 angr 9.0.4663 appdirs 1.4.4 apsw 3.33.0.post1 . more lines that I cut off 

If you want to loop through this in python, then you can use subprocess.check_output to capture the output then do a bit of string processing:

import subprocess packages = subprocess.check_output(["python3", "-m", "pip", "list"]).decode("utf8") package_lines = map(str.split, packages.splitlines()[2:]) for package_name, version in package_lines: print(f"Package has version ") 

on your terminal to list all the installed python modules.

Читайте также:  Php get local file content

If you want those modules as a list in your python program, you would use pkg_resources module

# Credit: https://www.activestate.com/resources/quick-reads/how-to-list-installed-python-packages/ 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) 

You can easily do that using the pip library in python:

try: from pip._internal.operations import freeze except ImportError: # in case pip version < 10.0 from pip.operations import freeze modules = list(freeze.freeze()) 

Now the modules variable is a list containing all the pip installed modules/packages.

here's just whats stored in modules for me when i ran it:

['altgraph==0.17', 'appdirs==1.4.4', 'argon2-cffi==20.1.0', 'async-generator==1.10', 'attrs==20.2.0', 'auto-py-to-exe==2.7.8', 'autopep8==1.5.4', 'backcall==0.2.0', 'beautifulsoup4==4.9.1', 'bleach==3.2.1', 'bottle==0.12.18', 'bottle-websocket==0.2.9', 'cachetools==4.1.1', 'certifi==2020.6.20', 'cffi==1.14.3', 'chardet==3.0.4', 'click==7.1.2', 'colorama==0.4.3', 'cryptography==3.2.1', 'cycler==0.10.0', 'decorator==4.4.2', 'defusedxml==0.6.0', 'distlib==0.3.1', 'docopt==0.6.2', 'Eel==0.12.4', 'entrypoints==0.3', 'et-xmlfile==1.0.1', and so on. ] 

I have cut it short as there is alot for me.

Источник

How to find Python List Installed Modules and Version using pip?

Do you want to know all the Python version installed on your system?

I have also recorded a video with a live demo. You can watch or else continue reading.

The main strength of the Python is, the wide range of external libraries are available. As we keep coding in Python, we install many packages. It is easy getting a Python list installed modules on the system. There are a couple of ways you can do that.

Following are the two ways that will work for you to get this list…

1. Using help() function (without pip):

The simplest way is to open a Python console and type the following command…

This will gives you a list of the installed module on the system. This list contains modules and packages that come pre-installed with your Python and all other you have installed explicitly.

Here is an example of running help function on my system (Python version 2).

list of Python modules using help function example

You don’t need to install any external module to get this list with help() function. But this command does not give you any other information about the package.

If you want to know the version of each installed modules, you can use pip program.

2. Using pip to find Python list installed modules and their Versions:

To find the list of Python packages installed on the system, you can use pip program.

Those who don’t know about pip, it is the best program which is used to install and to manage other Python packages on your system. For more understanding, you can check the complete guide for managing Python modules using pip.

If you have the latest version of Python, pip comes preinstalled with Python.

Run following commands on the command line (not on Python console). You get the complete list of installed Python modules with their versions.

Here is an example of listing Python package you have installed on your system using the pip tool.

list of Python modules using pip freeze example

Unlike help function, it does not list down preinstalled Python packages.

You can see all the Python packages followed by their version.

Note: Before running this command, ensure if there is a pip installed on your system. For Python version 2.7+ and 3.4+, it comes pre-installed with Python.

The format of the output list of both commands is totally different. Suppose you are using these command in shell scripting. You can choose any of the commands which you find easy for parsing the output package list and get the information.

If you already have parsing code for any of the output from two commands, you can use that command.

Related Read: Why you should learn Shell scripting? (Python vs Shell Scripting)

For more detail about any specific module, run command.

It returns the name of the module/package, version, author, author email, license, location of the installed module and requires.

You can get the author’s email. You can reach out to the author for any specific query related to the Python package.

If you are using python code for commercial purpose, knowing the package’s license is important.

How to Check if Python module is installed?

You can use pip commands with grep command to search for any specific module installed on your system.

For instance, you can also list out all installed modules with the suffix “re” in the module name.

How to count the number of Python modules installed on your system?

You can use wc (word count) command.

Note: grep and wc commands only work with Linux based systems.

What is the use of these commands?

  • You can use these commands to list out all the installed modules on your system. Later you can use this list to set up a new identical environment.
  • If you face any issue in installed Python package, running these commands make debugging easier.
  • Knowing Python module version, you can update the module if a new version of the module is available.

In an upcoming article, I will share, how you can write a Python program to get a list of Python packages and save them in a list.

If you find these commands useful for Python list installed modules, share with your friends. Feel free to write a comment if you have any question regarding handling Python packages.

Источник

How to List Installed Python Packages

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages.

You can also use the ActiveState Platform’s command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command. For a complete list of all packages and dependencies (including OS-level and transitive dependencies, as well as shared libraries), you can use the Web GUI, which provides a full Bill of Materials view. Give it a try by signing up for a free ActiveState Platform account .

Before getting a list of installed packages, it’s always a good practice to ensure that up-to-date versions of Python, Pip, Anaconda Navigator and Conda are in place.

List Installed Packages with Pip

Both pip list and pip freeze will generate a list of installed packages, just with differently formatted results. Keep in mind that pip list will list ALL installed packages (regardless of how they were installed). while pip freeze will list only everything installed by Pip.

Package Version ---------------------------------- ---------- absl-py 0.7.0

List Packages in a Console with Pip

To list all installed packages from a Python console using pip, you can utilize the following script:

>>> 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)
['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',

List Modules in a Console without Pip

To list all installed modules from a python console without pip, you can use the following command:

Note that there are some drawbacks to this approach, including:

  • If there are a lot of installed packages, 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.

List Installed Packages with Pipenv

The pipenv lock -r command can be used to generate output from a pipfile.lock file in a pipenv environment. All packages, including dependencies will be listed in the output. For example:

-i https://pypi.org/simple certifi==2019.11.28 chardet==3.0.4 idna==2.9 requests==2.23.0 urllib3==1.25.8

List Installed Packages with Anaconda Navigator

To list installed packages in an Anaconda environment using Anaconda Navigator, do the following:

  • Start the Anaconda Navigator application.
  • Select Environments in the left column.
  • 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.

List Installed Packages with Conda

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

# packages in environment at C:\Anaconda2_4.3.1: # _license 1.1 py27_1 alabaster 0.7.9 py27_0

Globally vs Locally Installed Packages

For information about generating a list of installed packages globally vs locally, refer to:

List Installed Packages with the ActiveState Platform

To view a list of installed Python packages in your currently active project using the ActiveState Platform, run the following command on the command line:

The output is a full list of installed packages in your current project:

matplotlib numpy pandas scikit-learn scipy

You can also obtain a complete software bill of materials view of all packages, dependencies, transitives dependencies (ie., dependencies of dependencies), OS-level dependencies and shared libraries (ie., OpenSSL) using the ActiveState Platform’s Web GUI:

BOM using Platform Web GUI

The ActiveState Platform automatically builds all Python packages including linked C libraries from source code, and packages them for Windows, Linux and macOS. Because it does it all server-side, there’s no need to maintain local build environments.

Источник

Оцените статью