How To Find Where Python Modules Stored

In this article, i will tell you 3 methods to find where a python module stores as below.

  1. Find python module in sys.path. The python sys module’s path attribute save all python module stored path. Then you can find your python module in the output path directory.
    >>> import sys
    >>> sys.path
    ['', 'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example\\', 'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example\\DLLs', 'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example\\lib', 'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example', 'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example\\lib\\site-packages']
  2. Use __import__( '<module_name>' ).__file__to get the python module stored file location.
    >>> __import__('os').__file__
    import sys
    # define a function to check whether module is a builtin module or not.
    def find_module(module):
        # if the module is a builtin module.
        if module in sys.builtin_module_names:
            print(module, " is builtin module ")
            # else print out the module source file path.
            print(module, "module file => ", __import__(module).__file__)
    if __name__ == '__main__':
        # below module do not exist, then an error will be thrown.
    Below is above code execue resule.
    os module file =>  C:\Users\zhaosong\anaconda3\lib\
    time  is builtin module 
    string module file =>  C:\Users\zhaosong\anaconda3\lib\
    Traceback (most recent call last):
      File "C:\WorkSpace\Work\\PythonExampleProject\com\dev2qa\example\standard\sys\", line 22, in 
      File "C:\WorkSpace\Work\\PythonExampleProject\com\dev2qa\example\standard\sys\", line 15, in find_module
        print(module, "module file => ", __import__(module).__file__)
    ModuleNotFoundError: No module named 'def'
  3. Use help( '<module_name>' ) method.
    >>> help('string')
    Help on module string:
        string - A collection of string constants.
        The following documentation is automatically generated from the Python
        source files.  It may be incomplete, incorrect or include features that
        are considered implementation detail and may vary between Python
        implementations.  When in doubt, consult the module reference at the
        location listed above.
        Public module variables:
        whitespace -- a string containing all ASCII whitespace
        ascii_lowercase -- a string containing all ASCII lowercase letters
        ascii_uppercase -- a string containing all ASCII uppercase letters
        ascii_letters -- a string containing all ASCII letters
        digits -- a string containing all ASCII decimal digits
        hexdigits -- a string containing all ASCII hexadecimal digits
        octdigits -- a string containing all ASCII octal digits
        punctuation -- a string containing all ASCII punctuation characters
        printable -- a string containing all ASCII characters considered printable

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.