What does if __name__ == ‘__main__’ mean in Python?

RS Punia 💠
7 min readMar 29, 2023
What does if __name__ == ‘__main__’ mean in Python?

Python, with its simplicity and versatility, empowers developers to write clear and organized code. While exploring Python scripts or inspecting existing projects, you might have come across the mysterious and fascinating construct: if __name__ == ‘__main__’:. What does it mean, and why is it a common sight in many Python scripts?

In this blog post, we’ll unravel the significance of this construct, demystifying its purpose and showcasing why it’s a fundamental part of structuring Python programs. Also, we will discover how to use it in our own Python code.

Python File As The Main Program or Being Imported As A Module

In Python, if __name__ == '__main__' is a common construct that is often used to make a module both importable and executable. The if __name__ == '__main__' construct is a conditional statement that checks whether the current script is being run as the main program or if it is being imported as a module by another script.

When a Python file is run directly as the main program, the value of the special variable __name__ is set to __main__. However, when a Python file is imported as a module by another script, the value of __name__ is set to the name of the module. Wait! What! I want to know more about this… well, keep scrolling…

By using the if __name__ == '__main__' construct, you can specify code that should only be executed when the file is run as the main program, and not when it is imported as a module.

First question first:

What is the difference between a Python Script and A Python module?

Photo by Joshua Sortino on Unsplash

A Python module is a file containing Python definitions and statements. It is used to organize code into logical, reusable chunks. A module can define functions, classes, and variables, and can also include runnable code. Modules are meant to be imported and used in other programs. For example, the math module in Python provides various mathematical functions like sqrt, cos, sin, etc.

On the other hand, a Python script is a file containing a sequence of Python statements that are executed when the file is run.

Scripts are used to automate tasks, run standalone programs, or test functionality. Scripts are typically standalone programs, unlike modules, which are meant to be imported and used in other programs.

Here is an example of a Python module:

# file: math_ops.py

def add(a, b):
return a + b


def subtract(a, b):
return a - b


def multiply(a, b):
return a * b


def divide(a, b):
if b == 0:
raise ValueError("Cannot divide by zero")
return a / b

This module contains four functions that perform basic mathematical operations. The functions can be imported and used in other programs. Here’s an example of how to use this module in another Python program:

# file: main.py

import math_ops
a = 5
b = 3

print(math_ops.add(a, b))
print(math_ops.subtract(a, b))
print(math_ops.multiply(a, b))
print(math_ops.divide(a, b))

This program imports the math_ops module and uses its functions to perform various mathematical operations on two numbers. Now let’s look at an example of a Python script:

# file: hello.py

name = input("What is your name? ")
print("Hello, " + name + "!")

This script prompts the user for their name and then prints a personalized greeting. To run this script, you can use the following command in your terminal or command prompt:

python hello.py

This will execute the script and prompt the user for their name. Once the name is entered, the script will print out the greeting.

Therefore, while both Python modules and Python scripts are text files containing Python code, they have different purposes and use cases. Modules are meant to be imported and used in other programs, while scripts are standalone programs that can be executed on their own from the command line or terminal.

Python File As A Module As Well As A Script

The code within the if __name__ == '__main__' block executes only if the script is the main program, not when it is imported as a module. This enables you to structure your code in a way that separates reusable components from executable scripts.

The most common use of if __name__ == '__main__' is to define a block of code that should only be executed when the Python file is run as the main program. Here's an example:

# example.py

def main():
print('Hello, world!')


if __name__ == '__main__':
main()

In this example, the main() function is defined to print a message to the console.

The if __name__ == '__main__' statement checks whether the current script is being run as the main program, and if it is, it calls the main() function to execute the code.

When you run this script from the command line using python example.py, the main() function will be executed and the message "Hello, world!" will be printed to the console.

However, if you import this script as a module in another Python file, the main() function will not be executed, since the if __name__ == '__main__' statement will evaluate to False.

So in Python, __name__is a special variable that contains the name of the current module. When a Python script is executed, the interpreter sets the __name__ variable of the script to "__main__".

The Role of __name__ Variable in Main and Imported Files

Create two files with names file1.py and file2.py. In file1.py write the following code and execute it.

# file1.py

print(__name__)

You will get the output as __main__. Defining it as the main program file from where the execution started.

__main__

However, when you import this file1.py in file2.py and execute the code.

# file2.py

import file1

The same print(__name__) line gives a different output this time.

file1

Now we get the output as the name of this file. This is how the if __name__ == '__main__' construct recognises the file as a main program file or as an imported file.

This represents that if you have a Python script that you want to use both as a standalone program and as a module that can be imported by other scripts, you can use the __name__ variable to determine if the script is being run as a standalone program or as a module

Here’s an example of how to import this script as a module:

import example


# Nothing is printed when importing the module

Here’s another example that demonstrates how the __name__ variable works:

# example2.py

def say_hello(name):
print(f"Hello, {name}!")


print(f"__name__ is {__name__}")
if __name__ == "__main__":
say_hello("Alice")

Now, if you run this script from the command line with python example2.py, you will see the following output:

__name__ is __main__
Hello, Alice!

The output shows that the __name__ variable is set to "__main__", and that the say_hello() function is called with the argument "Alice".

However, if you import the example2 module into another script with import example2, you will see the following output:

__name__ is example2

The output shows that the __name__ variable is set to "example2", and the say_hello() function is not called, since it is inside the if __name__ == "__main__": block.

Benefits of using if name == ‘main’

Using the if __name__ == '__main__' construct has several benefits:

Modularity: By encapsulating executable code within the if __name__ == '__main__' block, you create modular and reusable components. These components can be seamlessly imported into other scripts without unintended execution.

Code Clarity: It enhances the readability of your code by clearly drafting the code intended for execution upon script initiation. This makes it easier for others (or future-you) to comprehend the script’s primary functionality.

Testability: The construct facilitates the testing of individual components by isolating code that should run when the script is executed independently. This aids in creating test cases and ensures that specific functionality remains intact.

Avoiding Unintended Execution: When you import a Python script as a module, the code within the if __name__ == '__main__' block remains dormant, preventing unintended execution of certain operations when the script is imported elsewhere.

Conclusion

In summary, the if __name__ == "__main__": statement is a useful way to ensure that code is only executed when a Python script is run as a standalone program, and not when it is imported as a module. By checking the value of the __name__ variable, you can write code that is flexible and can be used in multiple contexts.

Hey there👋! If you found this tutorial helpful, feel free to show your appreciation by clapping for it! Remember, you can clap multiple times if you like it.

You can clap multiple times “if you really liked it”

If you’re interested in exploring more Python content, be sure to follow me, RS Punia, on Medium. As a passionate software programmer with a love for computer vision, artificial intelligence, and machine learning, I’m always seeking new coding challenges and sharing my knowledge with others. Follow me now and let’s continue to learn and grow together!

--

--

RS Punia 💠

🐍 Python software programmer passionate about computer vision, AI & ML 📈 Enjoys analyzing data and building projects with Django and React🚀