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).

import os

# print the module's online manual

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

List all names exported by the module module_name.
List all names in current scope (but not standard functions). To list standard functions, import __builtin__ first, then dir(__builtin__).
import re

# print all names exported by the module

# 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.
Call a function in module.
# import the standard module named os

import os

# example of using a function
# get current dir

# /Users/xah/web/xahlee_info/python

Import Function Names from Module

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

from module_name import name_1, name_2, …
Import several functions.
from module_name import *
Import all functions.
from os import getcwd

# current dir

Default “module”

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

# ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

# ['__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.

import sys
import pprint

# pretty print module search paths

# ['/Users/xah/web/xahlee_info/python',
#  '/Users/xah/anaconda3/lib/',
#  '/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.

import sys
import pprint

# pretty print loaded 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/'>,
#  '_codecs': <module '_codecs' (built-in)>,
# ...
#  'zipimport': <module 'zipimport' (built-in)>}

Python: Paths Topic



Text Processing