73

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?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Wizzard
  • 12,582
  • 22
  • 68
  • 101

6 Answers6

103

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 
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 4
    or outside of the interpreter with the `pydoc` tool: `pydoc modules`. – Thomas Wouters Oct 17 '10 at 12:28
  • 1
    typing "modules spam" will not show all the modules that can be imported from "spam", for example the PolynomialFeatures in from sklearn.preprocessing is not shown for some reason... Whereas @intuited's answer *does* show it. Not so sure why this is the case – hello_there_andy Oct 09 '17 at 14:43
  • another straightforward approach would be using `help('modules')` and `help('modules keyword')` for searching for a keyword – Bruno Alexandro García Tejada Apr 13 '22 at 22:25
  • The help() function also allows parameters, e.g. `help("modules")`. This is very useful for scripts. – Maf Jul 17 '23 at 04:57
15

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.

Community
  • 1
  • 1
intuited
  • 23,174
  • 7
  • 66
  • 88
  • 1
    Good answer, but easy_install should not be used directly with sudo since there are no easy way to uninstall. Use the package manager of your distro or virtualenv. – esamatti Oct 17 '10 at 10:08
  • @Epeli: Good point. I've updated my answer to explain this shortcoming of `easy_install`, and to tentatively suggest the use of `pip`. I've found through light usage that it does keep track of installed scripts and whatnot as advertised, though I've not tried to use it to uninstall anything as complex as ipython. – intuited Oct 17 '10 at 11:02
  • Nice. `help("modules")` also list all packages besides the installed ones. – Maf Jul 17 '23 at 05:00
12

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', '')
Rajat Sewal
  • 117
  • 1
  • 3
  • This example uses Python 2 syntax for the print command. See the comment I added to the one-line answer ("python -c"). – Alan Nov 17 '21 at 18:38
6

You can list available modules like so:

python -c "for dist in __import__('pkg_resources').working_set:print dist.project_name.replace('Python', '')"
rann
  • 69
  • 1
  • 3
    I've got `ImportError('No module named pkg_resources',)` in jython 2.7a – n611x007 Jan 22 '13 at 15:56
  • A couple of packages seem to be missing here. For example `math`. – Martin Thoma Dec 07 '15 at 07:19
  • This example uses Python 2 syntax for the print command. To work in both Python 2 and Python 3, wrap the thing being printed in parentheses. (I would make the edit myself, but am being told that the suggested edit queue is full.) Also, you may not need to remove instances of "Python", which means you can simplify the command. – Alan Nov 17 '21 at 18:37
3

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.

pyfunc
  • 65,343
  • 15
  • 148
  • 136
0

You may use the pip module:

from pip._internal.operations.freeze import freeze

for line in freeze():
    print(line.split('=='))