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

print (help('modules') )

# -*- coding: utf-8 -*-

# prints a list of existing modules
Please wait a moment while I gather a list of all available modules...

CDROM               binhex              itertools           site
DLFCN               bisect              json                sitecustomize
IN                  builtins            keyword             smtpd
TYPES               bz2                 lib2to3             smtplib

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

# -*- coding: utf-8 -*-
# python

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

# -*- coding: utf-8 -*-
# python

import re

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

# sample output:

# ['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']

Using Module

import module_name → load a module.

module_name.function_name → call a function in module.

# -*- coding: utf-8 -*-
# python

# import the standard module named os

import os

# example of using a function
print 'current dir is:', os.getcwd()

Import Function Names from Module

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

# -*- coding: utf-8 -*-
# python

from os import getcwd
print getcwd()  # current working dir

Default “module”

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

# -*- coding: utf-8 -*-
# python

print dir() # ['__builtins__', '__doc__', '__file__', '__name__', '__package__']

print dir("__main__")

# ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', '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.

# -*- coding: utf-8 -*-
# python

import sys
import pprint

# pretty print module search paths

# sample output

List Loaded Modules

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

# -*- coding: utf-8 -*-
# python

import sys
import pprint

# pretty print loaded modules

# sample output
{'UserDict': <module 'UserDict' from '/usr/lib/python2.7/UserDict.pyc'>,
 '__builtin__': <module '__builtin__' (built-in)>,
 '__main__': <module '__main__' from '/home/joe/web/'>,
 '_abcoll': <module '_abcoll' from '/usr/lib/python2.7/_abcoll.pyc'>,
 '_codecs': <module '_codecs' (built-in)>,
 '_sre': <module '_sre' (built-in)>,
 '_warnings': <module '_warnings' (built-in)>,
 '_weakref': <module '_weakref' (built-in)>,
 '_weakrefset': <module '_weakrefset' from '/usr/lib/python2.7/_weakrefset.pyc'>,
 'abc': <module 'abc' from '/usr/lib/python2.7/abc.pyc'>,
 'apport_python_hook': <module 'apport_python_hook' from '/usr/lib/python2.7/dist-packages/apport_python_hook.pyc'>,
 'cStringIO': <module 'cStringIO' (built-in)>,
 'codecs': <module 'codecs' from '/usr/lib/python2.7/codecs.pyc'>,
 'copy_reg': <module 'copy_reg' from '/usr/lib/python2.7/copy_reg.pyc'>,
 'encodings': <module 'encodings' from '/usr/lib/python2.7/encodings/__init__.pyc'>,
 'encodings.__builtin__': None,
 'encodings.aliases': <module 'encodings.aliases' from '/usr/lib/python2.7/encodings/aliases.pyc'>,
 'encodings.codecs': None,
 'encodings.encodings': None,
 'encodings.utf_8': <module 'encodings.utf_8' from '/usr/lib/python2.7/encodings/utf_8.pyc'>,
 'errno': <module 'errno' (built-in)>,
 'exceptions': <module 'exceptions' (built-in)>,
 'genericpath': <module 'genericpath' from '/usr/lib/python2.7/genericpath.pyc'>,
 'linecache': <module 'linecache' from '/usr/lib/python2.7/linecache.pyc'>,
 'os': <module 'os' from '/usr/lib/python2.7/os.pyc'>,
 'os.path': <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>,
 'posix': <module 'posix' (built-in)>,
 'posixpath': <module 'posixpath' from '/usr/lib/python2.7/posixpath.pyc'>,
 'pprint': <module 'pprint' from '/usr/lib/python2.7/pprint.pyc'>,
 're': <module 're' from '/usr/lib/python2.7/re.pyc'>,
 'signal': <module 'signal' (built-in)>,
 'site': <module 'site' from '/usr/lib/python2.7/site.pyc'>,
 'sitecustomize': <module 'sitecustomize' from '/usr/lib/python2.7/sitecustomize.pyc'>,
 'sre_compile': <module 'sre_compile' from '/usr/lib/python2.7/sre_compile.pyc'>,
 'sre_constants': <module 'sre_constants' from '/usr/lib/python2.7/sre_constants.pyc'>,
 'sre_parse': <module 'sre_parse' from '/usr/lib/python2.7/sre_parse.pyc'>,
 'stat': <module 'stat' from '/usr/lib/python2.7/stat.pyc'>,
 'sys': <module 'sys' (built-in)>,
 'sysconfig': <module 'sysconfig' from '/usr/lib/python2.7/sysconfig.pyc'>,
 'traceback': <module 'traceback' from '/usr/lib/python2.7/traceback.pyc'>,
 'types': <module 'types' from '/usr/lib/python2.7/types.pyc'>,
 'warnings': <module 'warnings' from '/usr/lib/python2.7/warnings.pyc'>,
 'zipimport': <module 'zipimport' (built-in)>}

Python: Paths Topic

  1. File Path Manipulation
  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 by Example

  1. Python Basics
  2. Print Version String
  3. Builtin Help
  4. Quote String
  5. String Operations
  6. String Methods
  7. Format String
  8. True, False
  9. if then else
  10. for, while, Loops
  11. List Basics
  12. Loop Thru List
  13. Map Function to List
  14. List Comprehension
  15. List Methods
  16. Dictionary
  17. Loop Thru Dict
  18. Dict Methods
  19. Function
  20. Class
  21. List Modules
  22. Write a Module
  23. Unicode 🐍


  1. Regex Basics
  2. Regex Reference

Text Processing

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


  1. Send Email
  2. GET Web Page
  3. Web Crawler


  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Complex Numbers
  7. Sort
  8. Copy Nested List
  9. Tuple vs List
  10. Sets, Union, Intersection
  11. Closure in Python 2
  12. Decorator
  13. Append String in Loop
  14. Timing f timeit
  15. Keyword Arg Default Value Unstable
  16. Check Page Load Size
  17. Thumbnail Generation

Python 3

  1. Python 3 Basics
  2. Print Version String
  3. Quoting String
  4. String Operations
  5. Format String
  6. Operators
  7. Object, ID, Type
  8. Traverse Directory
  9. Sort List, Matrix, Object
  10. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  11. Python 3 Closure
  12. Python 2 and 3 Difference