Python: Don't run a module's code when it is imported

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Python: Don't run a module's code when it is imported

To not run a module's code when it is imported:

  1. Use an if statement that checks if __name__ is equal to "__main__".
  2. All of the code in the if statement won't run when you import the module into another module.

Suppose we have the following folder structure.

shell
my-project/ └── main.py └── another.py

Here is the code for the main.py file.

main.py
import another

The main.py file simply imports the another module.

Here is the code for the another.py file.

another.py
# 👇️ 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()
The code for this article is available on GitHub

Here is the output of running the python main.py command.

shell
python main.py # Or python3 python3 main.py

dont run module code when it is imported

Only the following print() call runs.

another.py
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.

another.py
if __name__ == '__main__': # This doesn't run on import # It only runs when the module is run directly main()
The code for this article is available on GitHub

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.

shell
python another.py python3 another.py

run another file directly

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.

another.py
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.

shell
python main.py # Or python3 python3 main.py

name variable is not set to main

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.

another.py
if __name__ == '__main__': # This doesn't run on import # It only runs when the module is run directly pass
The code for this article is available on GitHub

# Not running any code when the module is imported

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.

main.py
import another

And here is the updated another.py file.

another.py
# 👇️ 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 code for this article is available on GitHub

The print statement from the global scope is always run, even when the module is imported.

not running code on import

The code in the if block is only run when the module is run directly with python another.py.

You can move your function and class definitions to the 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.

another.py
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.

another.py
def main(): site = 'bobbyhadz.com' print(site) if __name__ == '__main__': main()
The code for this article is available on GitHub

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.

main.py
import another another.main()

And the following another.py script.

another.py
def main(): site = 'bobbyhadz.com' print(site) if __name__ == '__main__': main()

Running the python main.py command produces the following output.

explicitly run main function on import

The main.py file explicitly called the main() function in another.py.

main.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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.