What is the main method in Python?

Programming languages like C++, Java, and C# use the main function as the starting point for their programs. However, in Python, this is not the case. As an interpretive language, Python does not require an initial entry point for the program to run. Instead, the code is executed sequentially. An example of this is if the code starts executing from line 1 and continues until it reaches the last line of the code.

However, we can still have a main method in Python to make the code more modular and readable. It will, however, just be like a normal method. Unlike other languages, we’ll have to explicitly call the main function to execute it.

Example

The example below shows how the main method is called:

print("Hello World")
def main():
print("In main method")
print("After main function")
main()
print("End of code")

Note: We can also comment line 8 and check that the main function will not execute.

How to call the main method

Within Python, there are two ways to define and then call the main method. Before we look at both these implementations, we need to understand an important concept of the __name__ variable.

Python creates and sets the values of implicit variables. These are variables that don’t need a data type to be declared for them. One such variable is the __name__ variable. The value of this variable varies depending on the file we are in. It can have the following two values:

  • __main__: It has this value if the main function is defined in the same file.
  • __fileName__: It has this value if the main function is defined in another file that is imported into the file we executed. It also signifies that certain functions from this file can be imported into another. As a result, we don’t want the complete file to run.

We can execute the main function when it is either defined in the same file or in another file that has been imported as a module in the executing file. Both these scenarios are explained below.

Defined in the same file

The first implementation shows how the main method can be defined and executed in the same file.

Example 1

The code below shows how to do this:

print("Hello World")
def main():
print("In main method")
if __name__ == "__main__":
main()
  • Line 1: We print the "Hello World" string.

  • Lines 3–4: We first define the main() method.

  • Lines 7–8: We use an if condition to check the file we're in and then call the main function.

Example 2

However, unlike other languages, the main() method can be given any name, and it will still execute in Python. While it is good practice to call this method main, the example below shows how it still works with any other name.

print("Hello World")
def otherName():
print("In otherName method")
if __name__ == "__main__":
otherName()

Note: The value of __name__ remains the same.

Import from another file

The second implementation of calling the main method is when this method is imported from another file, also called a moduleA module is simply a program that is imported in another file so that it can be used multiple times without writing the same code repeatedly..

Example 3

The code below shows how this works:

main.py
newMain.py
def main():
print("In newMain's main fucntion")
# The main fucntion will only run when this
# file is imported in another file
if __name__ == "newMain":
main()

Example

  • We first run the main.py file:

    • Line 1: We execute the print statement.

    • Line 2: We execute the line and import newMain.

  • In the newMain.py file, we do the following:

    • Lines 1–2: We define the main method.

    • Line 6: The if condition in the newMain.py file is true. It calls the main method.

    • Line 7: The main method executes the code in the main method and then moves back to line 3 of the main.py file.

    • The code executes the print statement in line 3 of the main.py file.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved