how do I determine whether a python script is imported as module or run as script?

Question:

The question is rather straightforward but not answered by searching. How do I determine in a python script whether this script is imported as a module or run as a script? Is there a difference at all in python?

The problem is, that I want to evaluate the command line parameters only if run as a script, but not if the module is only imported to use it in another script. (I want to be able to use one script as both library and program.) I am afraid the vanilla way would be to build the lib and a second script that uses it, but I’d like to have a second option for small tool/libs.

Asked By: Don Johe

||

Answers:

from python docs:

When you run a Python module with

python fibo.py

the code in the module will be
executed, just as if you imported it,
but with the __name__ set to
"__main__". That means that by adding
this code at the end of your module:

if __name__ == '__main__':
    # Running as a script

you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file

Answered By: Nadia Alramli

As pointed out by @bobince:

You will also be __main__ if you are a module invoked by python -m somemodule

Let’s suppose you have a Python file bar.py and an empty __init__.py, both inside a folder called foo:

$ tree
.
└── foo
    ├── __init__.py
    └── bar.py

$ cat foo/__init__.py

The Python code blocks below are the content of foo/bar.py.

Using __name__ (not working)

print('Code executed as a %s' % 'script' if __name__ == '__main__' else 'module')

This will produce:

$ python foo/bar.py
Code executed as a script

$ python -m foo.bar
Code executed as a script

Solution 1: using vars() and sys.modules

import sys
mod_name = vars(sys.modules[__name__])['__package__']
print('Code executed as a ' + ('module named %s' % mod_name if mod_name else 'script'))

This will produce:

$ python foo/bar.py
Code executed as a module named foo

$ python -m foo.bar
Code executed as a script

Solution 2: Using a try-except block on module import

import sys
try:
    import foo
    print('Code executed as a module')
except ImportError:
    print('Code executed as a script')
    # Code will fail here, but you can still print a comprehensive error message before exiting:
    print('Usage: python -m foo.bar')
    sys.exit()

This will produce:

$ python foo/bar.py
Code executed as a module

$ python -m foo.bar
Code executed as a script
Usage: python -m foo.bar
Answered By: roipoussiere
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.