Python get module file

How do I find the current module name in Python?

A module can find out its own module name by looking at the predefined global variable __name__. If this has the value ‘__main__’, the program is running as a script.

Example

def main(): print('Testing…. ') ... if __name__ == '__main__': main()

Output

Modules that are usually used by importing them also provide a command-line interface or a selftest, and only execute this code after checking __name__.

The__name__ is an in-built variable in python language, we can write a program just to see the value of this variable. Here’s an example. We will check the type also −

Example

print(__name__) print(type(__name__))

Output

Example

def myFunc(): print('Value of __name__ = ' + __name__) if __name__ == '__main__': myFunc()

Output

Value of __name__ = __main__

Example

Now, we will create a new file Demo2.py. In this we have imported Demo and called the function from Demo.py.

import Demo as dm print('Running the imported script') dm.myFunc() print('\n') print('Running the current script') print('Value of __name__ = ' + __name__)

Output

Running the imported script Value of __name__ = Demo Running the current script Value of __name__ = __main__

Источник

Get Module File Information in Python

In Python sometimes you need to figure out where a function lives on the hard disk, or what file is calling your function. The os module provides tools for getting file and directory information and the inspect module provides the ability to inspect the stack and see what module is calling your function.

Читайте также:  Element wise multiplication python

Get file information about a module

The os module in Python provides many useful ways for getting the file name of where a module lives. There are functions to get file names, directory names, and get absolute paths. Check out this example:

# Example assumes this file is named test.py
# and lives in /home/nanodano/
# https://docs.python.org/3/library/os.path.html

import os

# Prints the full path of where the os.py module resides
print(os.__file__)


# The name of file where this print statement is written
print(__file__)
# test.py

# Get the full absolute path using os.path.abspath()
print(os.path.abspath(__file__))
# /home/nanodano/test.py

# Get the directory name of a file with os.path.dirname()
print(os.path.dirname(__file__))
# Relative to current directory
# Prints an empty string if being run from current directory
# Prints .. if being called from a deeper directory

# Combine to get absolute path of the directory containing the python file
print(os.path.abspath(os.path.dirname(__file__)))
# /home/nanodano

## Other functions that might be useful
# os.path.join() # Join directory names using system specific dir separator
# os.sep() # Path separator character (system specific)
# os.getcwd() # Full absolute path
# os.walk() # Generator to walk dirs recursively
# os.listdir() # Current dir or specific dir

Get module and file name of caller

In some cases, your function is getting called by code from other .py files. If you want to figure out where your function is being called from, you can inspect the stack. The inspect module provides a way to get this data.

import inspect

# Import this to other module and call it
def print_caller_info():
# Get the full stack
stack = inspect.stack()

# Get one level up from current
previous_stack_frame = stack[1]
print(previous_stack_frame.filename) # Filename where caller lives

# Get the module object of the caller
calling_module = inspect.getmodule(stack_frame[0])
print(calling_module)
print(calling_module.__file__)


if __name__ == '__main__':
print_caller_info()

Conclusion

After reading this, you should know how to get the relative and absolute filename of the current Python modules as well as modules that are calling your function.

Источник

Оцените статью