Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the starting point of code execution in any Python project?

I have been looking at various open source GitHub Python projects like, http-prompt and Theano

What I am not able to figure out is where their starting points are, so that I can debug them gracefully. Do I need to look in each and every file for the __main__ method?

I am from Android background; so I was searching something related like AndroidManifest.xml from where I can get an idea as where the code is getting started, but I was unsuccessful in my attempts.

like image 844
Ali_Waris Avatar asked Sep 12 '25 20:09

Ali_Waris


1 Answers

There are two ways a Python script can be loaded:

  • By import : import mymodule
  • By commandline : $ python mymodule.py

In both cases all code inside the script is executed

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"

Note that the project you mentioned, doesn't contain any explicitly defined entry point, instead the entry point script is generated at packaging time for distribution (see setup.py for this purpose)

like image 132
Nicolas Cornette Avatar answered Sep 14 '25 09:09

Nicolas Cornette