Какие библиотеки установлены в python
Перейти к содержимому

Какие библиотеки установлены в python

  • автор:

pip list#

Packages are listed in a case-insensitive sorted order.

Options#

List outdated packages

List uptodate packages

List editable projects.

If in a virtualenv that has global access, do not list globally-installed packages.

Only output packages installed in user-site.

Restrict to the specified installation path for listing packages (can be used multiple times).

Include pre-release and development versions. By default, pip only finds stable versions.

Select the output format among: columns (default), freeze, or json. The ‘freeze’ format cannot be used with the —outdated option.

List packages that are not dependencies of installed packages.

Exclude editable package from output.

Include editable package from output.

Exclude specified package from the output

Base URL of the Python Package Index (default https://pypi.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format.

Extra URLs of package indexes to use in addition to —index-url. Should follow the same rules as —index-url.

ART HUB

В Python есть несколько способов узнать, какие библиотеки установлены. Один из наиболее простых способов — это использовать команду help(«modules»).

Команда help(«modules») выдаст список всех модулей, доступных для использования в Python. Этот список включает в себя встроенные модули, такие как os и sys, а также дополнительные библиотеки, установленные с помощью pip.

Еще один способ узнать, какие библиотеки установлены, — это использовать модуль pip. Вы можете использовать команду pip freeze, чтобы получить список всех установленных пакетов и их версий.

В заключение, существует множество способов узнать, какие библиотеки установлены в Python. Вы можете использовать команду help(«modules»), чтобы получить список всех доступных м

модулей, или же использовать модуль pip с командой pip freeze, чтобы получить список установленных пакетов. Выбирайте тот способ, который вам более подходит и комфортно использовать.

Where Are Python Packages Installed

A package in Python can be defined as a directory that contains Python files. These files are usually Python Modules.

As the program grows larger and gets more complex, similar modules are positioned in a package, which helps in making the program easier to manage and have better readability. This approach is often called Modular Programming, and packages help in achieving it.

Please enable JavaScript

The file __init__.py must be contained inside the directory in order for Python to consider it as a Package. This file usually has the initialization code for the package, but it can be left empty.

This tutorial will discuss different methods to find the directories in which python packages are installed.

Use the pip Command to List the Packages Installed

In Python, the packages can be installed both globally and locally.

A package, when installed globally, is available to all the users in the system. The same package, when installed locally, would only be available to the user that manually installed it.

By default, the pip command installs the packages globally.

The following code uses the pip command to list the packages installed globally.

Although, by default, the pip command installs packages globally, the packages that have been manually installed locally can also be seen using this command.

The following code uses the pip command to list the packages installed locally.

Use the conda Command to List the Locally Installed Packages

This method works only for programmers working on Anaconda IDE. It is possible to list the locally installed package in a conda environment. To execute this, we just have to write a single line of code in the Anaconda prompt.

The following code uses conda to list the packages installed locally.

Use the python Command to List the Packages Installed

The python command can be used to find the package-site directories.

Global Site Packages

The global site packages are found to be listed in sys.path .

The following code uses the python command to list the globally installed packages.

The site module can also be used along with the python command to get a better and more concise list of packages. This method uses the getsitepackages() from the site module.

The following code uses the python command along with the site module to list the globally installed packages.

Note that the getsitepackages() function is not available with virtualenv.

Locally Installed Packages

The local packages are installed in the per-user site-packages directory (PEP 370).

The following code uses the python command to list the locally installed packages.

Use the distutils.sysconfig Module to List the Packages Installed

The distutils package can be utilized to provide functions for installing and building additional modules into a Python installation. In this case, it can be used to list the packages as well.

The following code uses the distutils.sysconfig to list the globally installed packages.

The only drawback is that it points us to the directory of the dist-packages or the packages automatically installed by the Operating System.

Use the sysconfig Module to List the Packages Installed

In Python 3 and above, the sysconfig module is available to use for listing the packages installed.

The sysconfig module isn’t to be mistaken with the distutils.sysconfig submodule that was mentioned above. The latter is an altogether different module and it’s deficient in the get_paths function that will be used here.

The following code uses the sysconfig module to list the installed packages.

We use the purelib path here, which is where the standard Python packages are installed, with the help of tools like pip .

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

How do I get a list of locally installed Python modules?

How do I get a list of Python modules installed on my computer?

Mateen Ulhaq's user avatar

Léo Léopold Hertz 준영's user avatar

33 Answers 33

in a Python shell/prompt.

Solution

Do not use with pip > 10.0!

My 50 cents for getting a pip freeze -like list from a Python script:

As a (too long) one liner:

Scope

This solution applies to the system scope or to a virtual environment scope, and covers packages installed by setuptools , pip and (god forbid) easy_install .

My use case

I added the result of this call to my flask server, so when I call it with http://example.com/exampleServer/environment I get the list of packages installed on the server’s virtualenv. It makes debugging a whole lot easier.

Caveats

I have noticed a strange behaviour of this technique — when the Python interpreter is invoked in the same directory as a setup.py file, it does not list the package installed by setup.py .

Steps to reproduce:

We have behave’s setup.py in /tmp/behave :

Install the python package from the git repo

If we run the aforementioned solution from /tmp

If we run the aforementioned solution from /tmp/behave

behave==1.2.5a1 is missing from the second example, because the working directory contains behave ‘s setup.py file.

I could not find any reference to this issue in the documentation. Perhaps I shall open a bug for it.

Adam Matan's user avatar

Now, these methods I tried myself, and I got exactly what was advertised: All the modules.

Alas, really you don’t care much about the stdlib, you know what you get with a python install.

Really, I want the stuff that I installed.

What actually, surprisingly, worked just fine was:

I say «surprisingly» because the package install tool is the exact place one would expect to find this functionality, although not under the name ‘freeze’ but python packaging is so weird, that I am flabbergasted that this tool makes sense. Pip 0.8.2, Python 2.7.

chiggsy's user avatar

Since pip version 1.3, you’ve got access to:

Which seems to be syntactic sugar for «pip freeze». It will list all of the modules particular to your installation or virtualenv, along with their version numbers. Unfortunately it does not display the current version number of any module, nor does it wash your dishes or shine your shoes.

In ipython you can type » import Tab «.

In the standard Python interpreter, you can type » help(‘modules’) «.

At the command-line, you can use pydoc modules .

johnsyweb's user avatar

I just use this to see currently used modules:

which shows all modules running on your python.

For all built-in modules use:

Which is a dict containing all modules and import objects.

In normal shell just use

As of pip 10, the accepted answer will no longer work. The development team has removed access to the get_installed_distributions routine. There is an alternate function in the setuptools for doing the same thing. Here is an alternate version that works with pip 10:

Please let me know if it will or won’t work in previous versions of pip, too.

Works Regardless of Pip Version

Run the following in your python editor or IPython:

Read other answers and pulled together this combo, which is quickest and easiest inside Python.

Find the specific Packages

Conveniently you can then get items from your dict easily, i.e.

Using Pip List Well

!pip list will run inside your jupyter notebook if working there, simplifying the ‘quick check’ Combine with other utilities like grep(if you have installed) pip list | grep pandas will get you your current pandas version for example

jabberwocky's user avatar

If we need to list the installed packages in the Python shell, we can use the help command as follows

Sadheesh's user avatar

I normally use pip list to get a list of packages (with version).

This works in a virtual environment too, of course. To show what’s installed in only the virtual environment (not global packages), use pip list —local .

Here’s documentation showing all the available pip list options, with several good examples.

James's user avatar

This will help

In terminal or IPython, type:

Abdullah Akhtar's user avatar

Amit Gupta's user avatar

Very simple searching using pkgutil.iter_modules

on windows, Enter this in cmd

Léo Léopold Hertz 준영's user avatar

I ran into a custom installed python 2.7 on OS X. It required X11 to list modules installed (both using help and pydoc).

To be able to list all modules without installing X11 I ran pydoc as http-server, i.e.:

Then it’s possible to direct Safari to http://localhost:12345/ to see all modules.

This solution is primary based on modules importlib and pkgutil and work with CPython 3.4 and CPython 3.5, but has no support for the CPython 2.

Explanation

  1. sys.builtin_module_names — names all built-in modules (look my answer here)
  2. pkgutil.iter_modules() — returns an information about all available modules
  3. importlib.util.find_spec() — returns an information about importing module, if exists
  4. BuiltinImporter — an importer for built-in modules (docs)
  5. SourceFileLoader — an importer for a standard Python module (by default has extension *.py) (docs)
  6. ExtensionFileLoader — an importer for modules as shared library (written on the C or C++)

Full code

Usage

For the CPython3.5 (truncated)

For the CPython3.4 (truncated)

Warning: Adam Matan discourages this use in pip > 10.0. Also, read @sinoroc’s comment below

This was inspired by Adam Matan’s answer (the accepted one):

which then prints out a table in the form of

which lets you then easily discern which packages you installed with and without sudo .

A note aside: I’ve noticed that when I install a packet once via sudo and once without, one takes precedence so that the other one isn’t being listed (only one location is shown). I believe that only the one in the local directory is then listed. This could be improved.

Daniel F's user avatar

In case you have an anaconda python distribution installed, you could also use

in addition to solutions described above.

Aside from using pip freeze I have been installing yolk in my virtual environments.

Tomasz Jakub Rup's user avatar

I’m comparing five methods to retrieve installed "modules", all of which I’ve seen in this thread

iter_modules help("modules") builtin_module_names pip list working_set
Includes distributions ✔️ ✔️
Includes modules (No built-in) ✔️ ✔️
Includes built-in modules ✔️ ✔️
Includes frozen ✔️ ✔️
Includes venv ✔️ ✔️ ✔️ ✔️
Includes global ✔️ ✔️ ✔️ ✔️
Includes editable installs ✔️ ✔️ ✔️ ✔️
Includes PyCharm helpers ✔️
Lowers capital letters ✔️
Time taken (665 modules total) 53.7 msec 1.03 sec 577 nsec 284 msec 36.2 usec

Summary

  • pip list and working_set are for distributions, not modules.
  • iter_modules and help("modules") are very similar, the biggest difference is that iter_modules doesn’t include built-in.
  • pip list and working_set are very similar, only difference is that working_set lowers all capital letters.
  • Built-in modules are only included by help("modules") and builtin_module_names .

Related caveats

  • Distributions, packages, and modules often have identical names making it easy to mistake one for the other.
  • importlib.util.find_spec is for modules and is case-sensitive.
  • sys.modules only lists imported modules.

Distributions

I’m saying distribution instead of package because I think it will reduce misunderstandings. A distribution/package can have multiple packages/modules inside it.

An installed distribution is not always importable by the same name. For example pip install Pillow is imported with import PIL . Sometimes a distribution even makes multiple modules importable.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *