Python Forum

Full Version: module detecting if imported vs not
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how can a module detect if it is being imported, possibly by a command script, as opposed to being run directly?
https://docs.python.org/3/library/__main__.html Wrote:a module can discover whether or not it is running in the top-level environment by checking its own __name__, which allows a common idiom for conditionally executing code when the module is not initialized from an import statement:

if __name__ == '__main__':
    # Execute when the module is not initialized from an import statement.
    ...
...