Get available modules

Question:

With PHP you have the phpinfo() which lists installed modules and then from there look up what they do.

Is there a way to see what packages/modules are installed to import?

Asked By: Wizzard

||

Answers:

If you use ipython, which is an improved interactive Python shell (aka “REPL“), you can type import  (note the space at the end) followed by a press of the [TAB] key to get a list of importable modules.

As noted in this SO post, you will have to reset its hash of modules after installing (certain?) new ones. You likely don’t need to worry about this yet.

If you don’t use ipython, and you haven’t tried it, it might be worth checking out. It’s a lot better than the basic Python shell, or pretty much any other REPL I’ve used.

ipython Installation

If you’re running linux, there is most likely an ipython package that you can install through your system management tools. Others will want to follow these instructions.

If your installation route requires you to use easy_install, you may want to consider instead using pip. pip is a bit smarter than easy_install and does a better job of keeping track of file locations. This is very helpful if you end up wanting to uninstall ipython.

Listing packages

Note that the above tip only lists modules. For a list which also includes packages —which contain modules— you can do from  + [TAB]. An explanation of the difference between packages and modules can be found in the Modules chapter of the helpful official Python tutorial.

#rtfm

As an added note, if you are very new to python, your time may be better spent browsing the standard library documentation than by just selecting modules based on their name. Python’s core documentation is well-written and well-organized. The organizational groups —File and Directory Access, Data Types, etc.— used in the library documentation’s table of contents are not readily apparent from the module/package names, and are not really used elsewhere, but serve as a valuable learning aid.

Answered By: intuited

Type help() in the interpreter

then

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".                                     

help> modules 
Answered By: ghostdog74

As aaronasterling says, all .py or .pyc files on sys.path is a module because it can be imported. There are scripts that can let you find what external module is installed in site-packages.

Yolk is a Python command-line tool and library for obtaining information about packages installed by setuptools, easy_install and distutils and it can also query pypi packages.

Answered By: pyfunc

You can list available modules like so:

python -c "for dist in __import__('pkg_resources').working_set:print dist.project_name.replace('Python', '')"
Answered By: rann

This was very useful. Here is a script version of this:

# To list all installed packages just execfile THIS file
# execfile('list_all_pkgs.py')
for dist in __import__('pkg_resources').working_set:
   print dist.project_name.replace('Python', '')
Answered By: Rajat Sewal

You may use the pip module:

from pip._internal.operations.freeze import freeze

for line in freeze():
    print(line.split('=='))
Answered By: David Lador
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.