How can we get the current module in Python?

In Python, the built-in variable __name__ can be used to obtain the name of the current module. When running a .py file directly, the value of __name__ is set to __main__, while when a .py file module is imported, the value of __name__ is the name of that module. The following code can be used to get the name of the current module:

module_name = __name__
print(module_name)

The output is “__main__” if the current module is the main module, and the output is the module name if the current module is an imported module.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds