How to Prevent Code Execution on Import in Python: if __name__ == '__main__'
When you import a Python module, the code within that module is executed. Often, you want to define functions and classes in a module that can be used by other scripts, without running the module's main execution logic when it's imported.
This guide explains the standard Python idiom, if __name__ == '__main__':
, which is used to control when code runs: only when the script is run directly, not when it's imported as a module.
Understanding __name__
and __main__
-
__name__
: This is a built-in variable in Python. Its value depends on how the current file (module) is being used:- When a file is run directly: If you run a Python file directly (e.g.,
python my_script.py
), Python sets the__name__
variable within that file to the string'__main__'
. - When a file is imported: If a file is imported as a module (e.g.,
import my_module
), Python sets__name__
to the module's name (e.g.,'my_module'
).
- When a file is run directly: If you run a Python file directly (e.g.,
-
__main__
: This is just a string literal, but it's the conventional value that__name__
takes when a script is run directly.
Using if __name__ == '__main__':
The if __name__ == '__main__':
block is the standard way to conditionally execute code only when the script is run directly, and not when it's imported as a module:
# another.py
print('this line is always run (even on import)')
def main():
site = 'tutorialreference.com'
print(site)
def greet(name):
return f'hello {name}'
print(greet('tom nolan'))
if __name__ == '__main__':
# This code runs ONLY when the script is executed directly
# (e.g., python another.py).
# It does NOT run when another.py is imported.
main()
print('this line is always run ...')
: This line always executes, whetheranother.py
is run directly or imported. Definitions (likedef main():
) are also always executed; they just define the function, they don't call it.if __name__ == '__main__':
: This is the key. This condition is onlyTrue
when the script is run directly.main()
: This is a common convention. You put the main logic of your script inside a function calledmain()
, and then you callmain()
only if the script is being run directly.
Example:
import another #Imports the file, runs code that is outside the conditional.
print('this line is always run (even on import)') #Always runs
def main():
site = 'tutorialreference.com'
print(site)
def greet(name):
return f'hello {name}'
print(greet('tom nolan'))
if __name__ == '__main__':
main() # Only runs if this file is directly executed.
If you run python main.py
, the output will be only this line is always run (even on import)
. The rest of the code in another.py
is not executed because __name__
is another
and not __main__
.
If you run python another.py
, the output will be:
this line is always run (even on import)
tutorialreference.com
hello tom nolan
Function and Class Definitions
Function and class definitions (def my_function(): ...
and class MyClass: ...
) are always executed when a module is imported. However, the code inside those functions and classes is not executed unless it's called. This is why putting your main execution logic inside main()
and calling main()
within the if __name__ == '__main__':
block is so important.
Explicitly Calling Functions from Another Module
Even if a module uses if __name__ == '__main__':
, you can still call its functions from another module:
# main.py
import another # Import the module
another.main() # Explicitly call the main() function from another.py
- This demonstrates the best practice of using
if __name__== "__main__"
, since the functions can be called directly if they are needed, but they won't get executed when the file is imported.