Python is a modular language which lets you implement different modules and code logic in multiple files. Logical division of your code into multiple files not only leads to a cleaner code but also improves code reusability.

In Python, you can execute the code in a Python file by directly running the file from a terminal or command line, or you can import the file from another file and then call the functionalities of the imported file in a new file. Because of that flexibility, you can define different code logic for when a file is run directly versus when it’s imported by another file. But how can you do that? The answer lies in the __name__ variable which is a default Python variable. In this article, you’ll see how if __name__ == '__main__' can help you implement different code logic depending on whether a Python file is run directly or imported by another file.

What is the __name__ Variable in Python

The __name__ variable in Python is a default Python variable. You can tell by the leading and trailing double underscores. When a Python file is executed directly, the __name__ variable is assigned a value __main__ . In case the script in a file is run by importing the file in another file, the __name__ variable is not initialized and the value __main__ is not assigned to the __name__ variable. The statement if __name__ == 'main' returns true if the file is run directly, otherwise it returns false. Let’s see this with an example.

Running a Python File Directly

Create a Python file named file_one.py. The contents of the file should look like this:

print("This is file one")

def myfunc():
    print("This is a function in file 1")

if __name__ == '__main__':
    # these lines execute if the file is directly
    print("File one was run directly")
    myfunc()

else:
    # this line executes if the file is imported in another file
    print("File one is imported in another file")

The logical in this file is pretty simple. The Python script file, file_one.py, prints a string to the console. The file contains a function myfunc() which also prints a string. Finally, you define a condition, if __name__ == 'main'. If the condition returns true you print a string and then call the myfunc() function. If the condition returns false, you print a different string.

If you run the file_one.py from a terminal, you will see the following output:

Output:

This is file one
File one was run directly
This is a function in file 1

In the output you see the string “This is file one”. Next, since file_one.py is run directly, the if __name__ == 'main' condition returns true, and the statement “File one was run directly” is printed along with the output of the function myfunc(), “This is a function in file 1”.


Attention Python Geeks!

This tutorial is actually really neat, but you can do a whole lot more with Python. To take your Python scripts to the next level, enter your email address below and we'll send you more powerful Python tips and tricks.

Python is powerful! Show me more free Python tips

Importing and Running a Python File script

Lets now execute the script of file_one.py by importing it from another Python script file. To do that, create another file named file_two.py in the same directory as file_one.py. The script inside file_two.py should look like this.:

# importing the file_one.py
import file_one

print("This is file two")

file_one.myfunc()

if __name__ == '__main__':
    # these line execute if the file is directly
    print("File two was run directly")

else:
    # this line executes if the file is imported in another file
    print("File two is imported in another file")

Notice at the beginning of file_two.py, you import the script from file file_one.py. Next, you simply print a string on the console, followed by a function call to myfunc(), which resides in file_one.py. After that, you check if file_two.py was run directly or not using the condition if __name__ == 'main', and you print a message accordingly.

When you run file_two.py, you’ll get this output:

Output:

This is file one
File one is imported in another file
This is file two
This is a function in file 1
File two was run directly

Let’s take a minute to help you understand why you get the messages above.

Since you import file_one.py at the beginning of file_two.py, the entire script from file_one.py executes first. You can see the string message “This is file one” prints before anything else. This is the first line of the file file_one.py.

Next, the if __name__ == 'main' condition that sits inside file_one.py executes, but now since file_one.py was not run directly, the condition if __name__ == 'main' returns false and you see the string “File one is imported in another file” in the output.

After that the script from file_two.py starts executing and you see the string “This is file two”. In the next line the myfunc() function of the file_one.py is called from inside file_two.py. That’s why you see the string “This is a function in file 1” on the console.

Finally, the condition if __name__ == 'main' inside file_two.py is executed. Since the file_two.py is being executed directly, the condition returns true and you see the string “The file is run directly” on the console.

That’s quite literally all you need to know about the expression if __name__ == 'main'. It’s a very common expression you’ll see in a lot of Python scripts, so it was worth having its own dedicated tutorial. I hope you enjoyed this lesson designed to help you learn how to run different commands based on whether your script was called directly or whether it was imported from a different script. If you found it helpful, please subscribe using the form below!


Attention Python Geeks!

This tutorial is actually really neat, but you can do a whole lot more with Python. To take your Python scripts to the next level, enter your email address below and we'll send you more powerful Python tips and tricks.

Python is powerful! Show me more free Python tips