how to find out the starting point of code execution in any python project?

  • Last Update :
  • Techknowledgy :

Usually if __name__ == '__main__': defines the entry point :

if __name__ == '__main__':
   print('Started from commandline')
else:
   print('Imported as a module')

In a git project, you can try this to find all scripts made to be launched from command-line :

$ git grep "if __name__ ?== ?\W__main__\W"

Suggestion : 2

The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line.,Produce a set of annotated listing files upon program completion that shows how many times each statement was executed. See also --coverdir, --file and --no-report below.,The above will execute somefile.py and generate annotated listings of all Python modules imported during the execution into the current directory.,Do not generate annotated listings. This is useful if you intend to make several runs with --count, and then produce a single set of annotated listings at the end.

python - m trace--count - C.somefile.py...
import sys
import trace

# create a Trace object, telling it what to ignore, and whether to
# do tracing or line - counting or both.
tracer = trace.Trace(
   ignoredirs = [sys.prefix, sys.exec_prefix],
   trace = 0,
   count = 1)

# run the new command using the given tracer
tracer.run('main()')

# make a report, placing output in the current directory
r = tracer.results()
r.write_results(show_missing = True, coverdir = ".")

Suggestion : 3

Python Main Function is the beginning of any Python program. When we run a program, the interpreter runs the code sequentially and will not run the main function if imported as a module, but the Main Function gets executed only when it is run as a Python program.,It is important to understand that, if you are running something directly on the Python shell or terminal, this conditional statement, by default, happens to be True.,As a result, programmers write all the necessary functional definitions on the top, and finally write this statement at the end, to organize the code.,In most programming languages, there is a special function which is executed automatically every time the program is run. This is nothing but the main function, or main() as it is usually denoted. It essentially serves as a starting point for the execution of a program.

In most Python programs/scripts, you might see a function definition, followed by a conditional statement that looks like the example shown below:

def main():
   print("Hello, World!")
if __name__ == "__main__":
   main()

As a result, you end up writing the conditional if statement as follows:

if __name__ == "__main__":
   Logic Statements

Let us see the following piece of code:

def get_got():
   print("…Fetching GOT Data… n")
data = "Bran Stark wins the Iron Throne. n"
print("…GOT Data has been fetched…n")
return data

print("n Demo: Using Functions n")
got = get_got()
print(got)

By now, you know the various ways how a Python code can be executed. You also know why and when a main() function is used. It is time to apply it. Look at the following piece of code:

print("n Main Function Demo n")
def demo(got):
   print("…Beginning Game Of Thrones…n")
new_got = str.split(got)
print("…Game of Thrones has finished…n")
return new_got
def main():
   got = "n Bran Stark wins the Iron Throne n"
print(got)
new_got = demo(got)
print(new_got)
if __name__ == "__main__":
   main()

Let us look at below code fragment:

print("n Main Function Demo n")
def demo(got):
   print("…Beginning Game Of Thrones Demo1…n")
new_got = str.split(got)
print("…Game of Thrones has finished…n")
return new_got
def getgot():
   print("…Getting GOT Data…n")
got = "Bran Stark wins the Iron Throne n"
print("…GOT Data has been returned…n")
return got
def main():
   got = getgot()
print(got)
new_got = demo(got)
print(new_got)
if __name__ == "__main__":
   main()

Suggestion : 4

Last Updated : 13 Jul, 2022

Output:

The time of execution of above program is: 0.001995563507080078