Ever wondered where is the starting point of the program in python. Though python is interpreted line by line from the source file, but don’t we need a file where our main function or main class persists. Programmers from C++ and Java background will be definitely obsessed with *void main()*.
In case of Java or C++ we are giving a particular file to the compiler in which main function resides.
But in case of python we can give any .py file to the python interpreter and it reads it line by line and executes it irrespective of the contents.
Here is a answer for the beginners learning python. We have __main__.py file in a directory which acts as a entry point of the execution.
Step 1: Create a directory e.g. "mathops" (or any name you like) Step 2: Create "printcontent.py" in mathops directory. paste the following - def printContent(content): print(content) Step 3:Open Terminal and pass mathops directory to python interpreter. >python mathops python.exe: can't find '__main__' module in 'mathops' (it gives the above error because interpreter is expecting __main__.py) Step 4:Create __main__.py in mathops directory. paste the following - import printcontent print("executing __main__") printcontent.printContent("This content is passed to function") Output: executing __main__ This content is passed to function
Using this feature of python we can define an entry point of executing a directory(project) in python where __main__.py can import and execute logic present in other python files in the scope of the interpreter.