Python: List Modules, Search Path, Loaded Modules

By Xah Lee. Date: . Last updated: .

List Available Modules

To list all modules, type pydoc modules in terminal.

or in Python code:

print (help('modules') )

# python 3

# prints a list of existing modules
print( help('modules'))
pydoc3 modules 2019-03-12 rrvw5
pydoc3 modules 2019-03-12

Note: in Ubuntu Linux, as of , there's a bug that both pydoc modules and help('modules') will crash Python. Use python3 instead.

sudo apt-get install python3

pydoc3 modules

Reading Module Documentation

To read the doc of a module, in terminal, type pydoc module_name, or in Python program call help(module_name).

# python 3

import os

# print the module's online manual
print(help(os))

Note: the documentation from pydoc is not identical to the official documentation. Doc from pydoc is generated from the module source code, and is terse and technical.

List Module's Function/Variable Names

# python 3

import re

# print all names exported by the module
print(dir(re))

# output

# ['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'Match', 'Pattern', 'RegexFlag', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_cache', '_compile', '_compile_repl', '_expand', '_locale', '_pickle', '_special_chars_map', '_subx', 'compile', 'copyreg', 'enum', 'error', 'escape', 'findall', 'finditer', 'fullmatch', 'functools', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'template']

Using Module

import module_name → load a module.

module_name.function_name → call a function in module.

# python 3

# import the standard module named os

import os

# example of using a function
# get current dir
print(os.getcwd())

# /Users/xah/web/xahlee_info/python

Import Function Names from Module

Module's function name can be imported directly by the syntax:

# python 3

from os import getcwd

# current dir
print(getcwd())

Default “module”

All global (variable/function) names in Python are considered to be in the pseudo-module namespace named __main__.

# python 3

print(dir())
# ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

print(dir("__main__"))
# ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Module Search Paths

Python module search paths is stored in the variable sys.path.

# python 3

import sys
import pprint

# pretty print module search paths
pprint.pprint(sys.path)

# ['/Users/xah/web/xahlee_info/python',
#  '/Users/xah/anaconda3/lib/python37.zip',
#  '/Users/xah/anaconda3/lib/python3.7',
#  '/Users/xah/anaconda3/lib/python3.7/lib-dynload',
#  '/Users/xah/anaconda3/lib/python3.7/site-packages',
#  '/Users/xah/anaconda3/lib/python3.7/site-packages/aeosa']

List Loaded Modules

Loaded module names is stored in the variable sys.modules.

# python 3

import sys
import pprint

# pretty print loaded modules
pprint.pprint(sys.modules)

# sample output
# {'__main__': <module '__main__' from '/Users/xah/web/xahlee_info/python/xxtemp.20190318.38b.py3'>,
#  '_abc': <module '_abc' (built-in)>,
#  '_bootlocale': <module '_bootlocale' from '/Users/xah/anaconda3/lib/python3.7/_bootlocale.py'>,
#  '_codecs': <module '_codecs' (built-in)>,
# ...
#  'zipimport': <module 'zipimport' (built-in)>}

Python: Paths Topic

  1. File Path
  2. Print Version String from Script
  3. Executable Path, Script Path
  4. List Modules, Search Path, Loaded Modules
  5. How to Write a Module

If you have a question, put $5 at patreon and message me.

Python

  1. Python 3 Basics
  2. Python 2 Basics
  3. Python 2 and 3 Difference
  4. Print Version
  5. Builtin Help
  6. Quote String
  7. String Methods
  8. Format String
  9. Operators
  10. Complex Numbers
  11. True, False
  12. if then else
  13. Loop
  14. List Basics
  15. Loop Thru List
  16. Map f to List
  17. Copy Nested List
  18. List Comprehension
  19. List Methods
  20. Sort
  21. Dictionary
  22. Loop Thru Dict
  23. Dict Methods
  24. Tuple
  25. Sets
  26. Function
  27. Closure
  28. 2 Closure
  29. Decorator
  30. Class
  31. Object, ID, Type
  32. List Modules
  33. Write a Module
  34. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. File Path
  4. Process Unicode
  5. Convert File Encoding
  6. Find Replace in dir
  7. Find Replace by Regex
  8. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Append String in Loop
  7. Timing f timeit
  8. Keyword Arg Default Value Unstable
  9. Check Page Load Size
  10. Thumbnail Generation