When a Python script is being executed as the main program or imported as a module into another script, the special variable ‘name‘ is used to identify the execution mode. It is usual practice to use the expression “if name == “main“:” to verify that the Python script is being run as the main program.
# module_example.py
def some_function():
print("Function in PythonCorner")
if __name__ == "__main__":
# This code will only run if the script is executed directly
print("This is the main program")
some_function()
The process is as follows:
When the interpreter runs a Python script, if the script is the main program, it will give the value “main” to the ‘name‘ variable.
- A When a script is loaded into another script as a module, its name is stored in the ‘name‘ variable.
To prevent script code from being performed when imported as a module, you may use the ‘if name == “main“:’ block. If you want to build reusable modules that can function independently, this is a great way to go.
Find the below evaluation for __name__ == “__main__”
__name__
: This is a special variable in Python that is automatically set by the interpreter. When a Python script is executed,__name__
is set to “main” if the script is the main program being run. If the script is imported as a module into another script,__name__
is set to the name of the script/module.==
: This is the equality comparison operator. It checks whether the value on the left is equal to the value on the right."__main__"
: This is a string literal. It’s the value to which__name__
is compared.
Running this script straight will provide the following output: “This is the main program” and “Function in module.” The code within the ‘if name == “main“:’ block will not be run if you import this script as a module into another script.
In Development, how is the value of name equal to “main” useful?
When writing your script, keep these scenarios in mind for when to utilize the if-statement:Not only can testing assist find flaws, but it also guarantees that your code will function as expected. Importing a function or object is necessary for test files. We usually don’t want the script to be launched as the main module in these kinds of situations.
You’d want to include a demo or other specific run-time situations for users when constructing a library. Python modules that use your code in their libraries will remain untouched by this if-statement.
Here is an example of a Python module:
# file: string_operations.py
def stringupper(s):
return s.upper()
def stringlower(s):
return s.lower()
There are two functions in this module that do simple math. Other applications may import and utilize the functions.
Here’s an example of how to use this module in another Python program:
# file: main.py
import string_operations
s="PythonCorner"
print(string_operations.stringupper(s))
print(string_operations.stringlower(s))
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()
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.
Below points to Remember
- Python modules all have a name declared. If this is ‘main,’ then the user is running the module independently and we may execute the proper steps.
- The name is modified to reflect the name of the script or module when it is imported as a module in another script.
Python files have two uses: as standalone programs or as modules that may be reused. - This code is only executed if the file was run directly, without being imported, and the value of name is equal to “main.”
Summary
- Python scripts are executed as main programs or imported as modules.
- The ‘name’ variable is used to identify the execution mode.
- The ‘if name == “main”:’ block is used to verify if the script is being run as the main program.
- The ‘if name == “main”:’ block prevents script code from being executed when imported as a module.
- Python modules have a declared name, and if it’s’main’, the user is running the module independently.
- The name is modified to reflect the name of the script or module when imported as a module in another script.
- Python files can be used as standalone programs or as reusable modules.