Tutorials  /  Python

Python main function

Ccentron Redaktion · February 2024 ·4 min read ·Python, Tutorial

Python main function is executed only when it’s being executed as a python program. As you know, we can also import a python program as a module, in that case python main method should not execute.

Main Function in Python

Main function is the entry point of any program. But python interpreter executes the source file code sequentially and doesn’t call any method if it’s not part of the code. But if it’s directly part of the code then it will be executed when the file is imported as a module. That’s why there is a special technique to define main method in python program, so that it gets executed only when the program is run directly and not executed when imported as a module.

Let’s see how to define python main function in a simple program. python_main_function.py

Python
print("Hello")
print("__name__ value: ", __name__)
def main():
print("python main function")

if __name__ == '__main__':
main()

def main():

print("python main function")

if __name__ == '__main__':

main()

When a python program is executed, python interpreter starts executing code inside it. It also sets few implicit variable values, one of them is __name__ whose value is set as __main__.

For python main function, we have to define a function and then use if __name__ == '__main__' condition to execute this function.

If the python source file is imported as module, python interpreter sets the __name__ value to module name, so the if condition will return false and main method will not be executed.

Python provides us flexibility to keep any name for main method, however it’s best practice to name it as main() method. Below code is perfectly fine, however not recommended.

Python
def main1():
    print("python main function")


if __name__ == '__main__':
    main1()

Below image shows the output when python_main_function.py is executed as source file.

Python main function
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Python Main Function as Module

Now let’s use above python source file as a module and import in another program. python_import.py

Python
import python_main_function

print("Done")

Now when above program is executed, below output is produced.

Python if name main
Code
Hello
__name__ value:  python_main_function
Done

Notice that first two lines are getting printed from python_main_function.py source file. Notice the value of __name__ is different and hence main method is not executed. Notice that python program statements are executed line by line, so it’s important to define the main() method first before the if condition to execute main method. Otherwise you will get error as NameError: name 'main' is not defined. That’s all about python main function.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren