Python Built-in Modules

As discussed in the python module chapter, the module is a file that will contain a set of functions, variables, classes, etc., to use in your applications.

 

Python has a set of built-in modules such as math, os, sys, etc., to use in any python script file. The built-in modules will load automatically whenever the interpreter starts, and these modules are available as a part of python libraries.

 

The built-in modules that are available in python interpreter written in C. To see the list of all available built-in modules, execute the help('modules') command in the python console as shown below.

 

>>> help('modules')

The above command will return all the available modules in the python interpreter.

 

__future__          _tkinter            getpass             sched
_abc                _tracemalloc        gettext             secrets
_ast                _warnings           glob                select
_asyncio            _weakref            gzip                selectors
_bisect             _weakrefset         hashlib             setuptools
_blake2             _winapi             heapq               shelve
_bootlocale         _xxsubinterpreters  hmac                shlex
_bz2                abc                 html                shutil
_codecs             aifc                http                signal
_codecs_cn          antigravity         idlelib             site
_codecs_hk          argparse            imaplib             smtpd
_codecs_iso2022     array               imghdr              smtplib
_codecs_jp          ast                 imp                 sndhdr
_codecs_kr          asynchat            importlib           socket
_codecs_tw          asyncio             inspect             socketserver
_collections        asyncore            io                  sqlite3
_collections_abc    atexit              ipaddress           sre_compile
_compat_pickle      audioop             itertools           sre_constants
_compression        base64              json                sre_parse
_contextvars        bdb                 keyword             ssl
_csv                binascii            lib2to3             stat
_ctypes             binhex              linecache           statistics
_ctypes_test        bisect              locale              string
_datetime           builtins            logging             stringprep
_decimal            bz2                 lzma                struct
_dummy_thread       cProfile            mailbox             subprocess
_elementtree        calendar            mailcap             sunau
_functools          cgi                 marshal             symbol
_hashlib            cgitb               math                symtable
_heapq              chunk               mimetypes           sys
_imp                cmath               mmap                sysconfig
_io                 cmd                 modulefinder        tabnanny
_json               code                msilib              tarfile
_locale             codecs              msvcrt              telnetlib
_lsprof             codeop              multiprocessing     tempfile
_lzma               collections         netrc               test
_markupbase         colorsys            nntplib             textwrap
_md5                compileall          nt                  this
_msi                concurrent          ntpath              threading
_multibytecodec     configparser        nturl2path          time
_multiprocessing    contextlib          numbers             timeit
_opcode             contextvars         opcode              tkinter
_operator           copy                operator            token
_osx_support        copyreg             optparse            tokenize
_overlapped         crypt               os                  trace
_pickle             csv                 parser              traceback
_py_abc             ctypes              pathlib             tracemalloc
_pydecimal          curses              pdb                 tty
_pyio               dataclasses         pickle              turtle
_queue              datetime            pickletools         turtledemo
_random             dbm                 pip                 types
_sha1               decimal             pipes               typing
_sha256             difflib             pkg_resources       unicodedata
_sha3               dis                 pkgutil             unittest
_sha512             distutils           platform            urllib
_signal             doctest             plistlib            uu
_sitebuiltins       dummy_threading     poplib              uuid
_socket             easy_install        posixpath           venv
_sqlite3            email               pprint              warnings
_sre                encodings           profile             wave
_ssl                ensurepip           pstats              weakref
_stat               enum                pty                 webbrowser
_statistics         errno               py_compile          winreg
_string             faulthandler        pyclbr              winsound
_strptime           filecmp             pydoc               wsgiref
_struct             fileinput           pydoc_data          xdrlib
_symtable           fnmatch             pyexpat             xml
_testbuffer         formatter           queue               xmlrpc
_testcapi           fractions           quopri              xxsubtype
_testconsole        ftplib              random              zipapp
_testimportmultiple functools           re                  zipfile
_testmultiphase     gc                  reprlib             zipimport
_thread             genericpath         rlcompleter         zlib
_threading_local    getopt              runpy

Get Help on Modules

To get the details about a particular built-in module, you can use the help() method. For example, after importing the math module, you can call help(math) to get the math module details in python.

 

Following is an example of getting the details about the math module in python.

 

>>> import math
>>> help(math)

It will return the detailed info about the math module like as shown below.

 

Get help on modules in python

 

If the information is more than one page, it will display -- More -- as shown in the image, if you want to see more info, press Enter.

 

You can also use the built-in dir() function to get the particular module's available names and attributes.

 

>>> dir(math)

The above statement will return the result as shown below.

 

Python dir() function to get help on modules

 

We will learn more about built-in modules in the next chapters.