Categories
python

Python: Print all modules and packages available on the python path

When working with python a ImportError or ModuleNotFoundError: No module named 'xxx' may arise.

This error usually means that the script or python file you are running does not know about a module you are importing.

How can we get a list of modules that are accessible?

Using Sys Modules

This gets all package modules and sub modules:

import sys
modules =   sys.modules
for key, value in modules.items():
    print('module name', key)
    print('module location', value)

Get the module you are currently in:

print(self.__module__)
Using Help

Set a breakpoint or print the below code in the code before the erroneous import

print(help('modules'))

This displays all the modules available:

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

IPython             ast                 importlib           rst2xml
__future__          asynchat            importlib_metadata  rstpep2html
_abc                asyncio             iniconfig           runpy
_ast                asyncore            inspect             s3transfer
_asyncio            atexit              io                  sched
_bisect             attr                ipaddress           secrets
_blake2             attrs               ipdb                select
_bootlocale         audioop             itertools           selectors
_bz2                autoreload          jedi                setuptools
_codecs             backcall            jmespath            shelve
_codecs_cn          base64              jp                  shlex
_codecs_hk          bdb                 json                shutil
_codecs_iso2022     binascii            keyword             signal
_codecs_jp          binhex              lib2to3             site
_codecs_kr          bisect              linecache           six
_codecs_tw          boto3               locale              smtpd
_collections        botocore            logging             smtplib
_collections_abc    builtins            lzma                sndhdr
_compat_pickle      bz2                 macpath             sniffio
_compression        cProfile            mailbox             socket
_contextvars        calendar            mailcap             socketserver
_crypt              certifi             marshal             sqlite3
_csv                cgi                 math                sre_compile
_ctypes             cgitb               matplotlib_inline   sre_constants
_ctypes_test        charset_normalizer  mimetypes           sre_parse
_curses             chunk               mmap                ssl
_curses_panel       cmath               modulefinder        stat
_datetime           cmd                 multiprocessing     statistics
_dbm                code                netrc               storemagic
_decimal            codecs              nis                 string
_distutils_hack     codeop              nntplib             stringprep
_dummy_thread       collections         ntpath              struct
_elementtree        colorama            nturl2path          subprocess
_functools          colorsys            numbers             sunau
_hashlib            compileall          opcode              symbol
_heapq              concurrent          operator            sympyprinting
_imp                configparser        optparse            symtable
_io                 contextlib          os                  sys
_json               contextvars         packaging           sysconfig
_locale             copy                parser              syslog
_lsprof             copyreg             parso               tabnanny
_lzma               crypt               pathlib             tarfile
_markupbase         csv                 pdb                 telnetlib
_md5                ctypes              pexpect             tempfile
_multibytecodec     curses              pickle              termios
_multiprocessing    cythonmagic         pickleshare         test
_opcode             data                pickletools         test_parsing
_operator           dataclasses         pip                 tests
_osx_support        datetime            pipes               textwrap
_pickle             dateutil            pkg_resources       this
_posixsubprocess    dbm                 pkgutil             threading
_py_abc             decimal             platform            time
_pydecimal          decorator           plistlib            timeit
_pyio               difflib             pluggy              tkinter
_pytest             dis                 poplib              token
_queue              distutils           posix               tokenize
_random             doctest             posixpath           toml
_scproxy            docutils            pprint              tomli
_sha1               dummy_threading     profile             tornado
_sha256             elastic_transport   prompt_toolkit      trace
_sha3               elasticsearch       pstats              traceback
_sha512             elasticsearch5      pty                 tracemalloc
_signal             email               ptyprocess          traitlets
_sitebuiltins       encodings           pwd                 tty
_socket             ensurepip           py                  turtle
_sqlite3            enum                py_compile          turtledemo
_sre                errno               pyasn1              types
_ssl                faulthandler        pyclbr              typing
_stat               fcntl               pydoc               typing_extensions
_string             filecmp             pydoc_data          unicodedata
_strptime           fileinput           pyexpat             unittest
_struct             fnmatch             pygments            urllib
_symtable           formatter           pyparsing           urllib3
_sysconfigdata_m_darwin_darwin fractions           pytest              uu
_testbuffer         ftplib              queue               uuid
_testcapi           functools           quopri              venv
_testimportmultiple gc                  random              warnings
_testmultiphase     genericpath         re                  wave
_thread             getopt              readline            wcwidth
_threading_local    getpass             reprlib             weakref
_tkinter            gettext             requests            webbrowser
_tracemalloc        glob                requests_aws4auth   wheel
_uuid               grp                 resource            wsgiref
_virtualenv         gzip                rfc3986             xdrlib
_warnings           h11                 rlcompleter         xml
_weakref            hashlib             rmagic              xmlrpc
_weakrefset         heapq               rsa                 xxlimited
_xxtestfuzz         hmac                rst2html            xxsubtype
_yaml               html                rst2html4           yaml
abc                 http                rst2html5           zipapp
activate_this       httpcore            rst2latex           zipfile
aifc                httpx               rst2man             zipimport
antigravity         idlelib             rst2odt             zipp
anyio               idna                rst2odt_prepstyles  zlib
appnope             imaplib             rst2pseudoxml       zmq
argparse            imghdr              rst2s5              
array               imp                 rst2xetex           

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose name or summary contain the string "spam".

How can Python Find your Module?

If your module is not in the output above then python cannot find your module.

In that case you have 2 options:

  • Explicitly add the path to your module/package
  • Import your module relatively

First let us see all the current paths:

import sys
print(sys.path)

If the path your module is in is not listed there you need to either add the path or structure your modules into a package.

To modify the path, use the PYTHONPATH environment variable. Example: PYTHONPATH=. pytest.

Multiple paths are separated with a : on mac

Extra: List available Functions and Variable names

dir() returns the list of names in the current local scope

print(dir(__name__))

output example:

['@py_builtins', '@pytest_ar', '__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']

Extra: Print the local symbol table:

Use locals()

print(locals())

Sources