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\\python38.zip', '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__
    'C:\\Users\\zhaosong\\anaconda3\\envs\\python_example\\lib\\os.py'
    
    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:
            # else print out the module source file path.
            print(module, "module file => ", __import__(module).__file__)
    
    
    if __name__ == '__main__':
        find_module('os')  
        find_module('time')
        find_module('string')
        
        # below module do not exist, then an error will be thrown.
        find_module('def')
    
    Below is above code execue resule.
    
    os module file =>  C:\Users\zhaosong\anaconda3\lib\os.py
    time  is builtin module 
    
    string module file =>  C:\Users\zhaosong\anaconda3\lib\string.py
    
    Traceback (most recent call last):
      File "C:\WorkSpace\Work\dev2qa.com-example-code\PythonExampleProject\com\dev2qa\example\standard\sys\find_modules.py", line 22, in 
        find_module('def')
      File "C:\WorkSpace\Work\dev2qa.com-example-code\PythonExampleProject\com\dev2qa\example\standard\sys\find_modules.py", 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:
    
    NAME
        string - A collection of string constants.
    
    MODULE REFERENCE
        https://docs.python.org/3.8/library/string
    
        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.
    
    DESCRIPTION
        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
    
    CLASSES
        builtins.object
        ........
    FILE
        c:\users\zhaosong\anaconda3\envs\python_example\lib\string.py
    
    
READ :   How To Send Email Use Gmail Free SMTP Service

Leave a Reply

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.