Last updated: Apr 11, 2024
Reading time·4 min

To not run a module's code when it is imported:
if statement that checks if __name__ is equal to "__main__".if statement won't run when you import the module
into another module.Suppose we have the following folder structure.
my-project/ └── main.py └── another.py
Here is the code for the main.py file.
import another
The main.py file simply imports the another module.
Here is the code for the another.py file.
# 👇️ Code that you always want to run print('this line is always run (even on import)') def main(): site = 'bobbyhadz.com' print(site) def greet(name): return f'hello {name}' print(greet('bobby hadz')) if __name__ == '__main__': # This doesn't run on import # It only runs when the module is run directly main()
Here is the output of running the python main.py command.
python main.py # Or python3 python3 main.py

Only the following print() call runs.
print('this line is always run (even on import)')
The order code that is run when the another.py module is imported is the
definition of the main function.
However, notice that the code in the main() function is not run, even though
we called it in the if statement at the bottom.
if __name__ == '__main__': # This doesn't run on import # It only runs when the module is run directly main()
The __name__ global variable is only going to be set to the string
'__main__' if the module is run directly.
The condition in the if statement won't be met if the module is imported.
For example, if I run the another.py file with python another.py, the if
block runs.
python another.py python3 another.py

Notice that the code in the if block runs because the __name__ variable is
set to __main__.
You can add the following print() call to another.py to print the value of
the __name__ variable.
print('__name__ is: ', __name__)
On the other hand, the __name__ variable won't be set to the string
'__main__' when the module is imported.
You can verify this with python main.py.
python main.py # Or python3 python3 main.py

When the module is imported, the __name__ variable is set to the module's
name.
Therefore the following if statement enables us to not run the code in the
if block when the module is imported.
if __name__ == '__main__': # This doesn't run on import # It only runs when the module is run directly pass
The previous example defined the main() function in the global scope of the
module.
This means that the code that defines the function will run when the another
module is imported.
If you don't want to run any code when the module is imported, move your code to
the if block.
Here is the code for the main.py file.
import another
And here is the updated another.py file.
# 👇️ Code that you always want to run print('this line is always run (even on import)') # 👇️ Code that doesn't run when the module is imported if __name__ == '__main__': site = 'bobbyhadz.com' print(site) def greet(name): return f'hello {name}' print(greet('bobby hadz'))
The print statement from the global scope is always run, even when the module
is imported.

The code in the if block is only run when the module is run directly with
python another.py.
if block if you don't want to run the code that defines the functions or classes when the module is imported.By default, when you import a module into a Python script, the module's code is run.
This includes function, class and variable declarations.
To be more specific, the functions are not invoked and the classes are not instantiated but the code that defines them is run.
The only way to prevent the code of a module from being run when it's imported
is to use the following if statement.
if __name__ == '__main__': # This is only run when the module is run directly # With `python another.py` pass
The most common pattern is to wrap the code you don't want to run on import in a
main() function.
def main(): site = 'bobbyhadz.com' print(site) if __name__ == '__main__': main()
The main() function is only run when the another.py module is run directly
with python another.py.
The main() function won't get invoked when the another module is imported.
Note that another Python script can explicitly run the main function.
Suppose you have the following main.py script.
import another another.main()
And the following another.py script.
def main(): site = 'bobbyhadz.com' print(site) if __name__ == '__main__': main()
Running the python main.py command produces the following output.

The main.py file explicitly called the main() function in another.py.
import another another.main()
This approach enables you to run the main function from other files only when
you need to.
Not every time the another.py module is imported.
You can learn more about the related topics by checking out the following tutorials: