Reputation: 2382
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.
Upvotes: 4
Views: 7863
Reputation: 782
There are two ways a Python script can be loaded:
import mymodule
$ 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)
Upvotes: 9
Reputation: 122007
The Python equivalent of a manifest file is generally setup.py
, so this is a good place to start looking. A package can:
scripts
keyword (as Theano does); or entry_points
keyword to define a function that should be exposed to the command line (as http-prompt
does, pointing to the cli
function).If neither of these is provided, the package is probably designed to be import
ed rather than executed, in which case take a look at the usage examples along with the root __init__.py
(e.g. Theano's), which will probably tell you what objects are exposed to the outside world. See the Python docs for more information on module structure.
However, Python is a dynamic, flexible language so there's no "magic bullet" to tell you where to look; there isn't e.g. a specific main.py
file that must be defined, for example (although there is a __main__.py
that can be defined, see What is __main__.py?)
Upvotes: 3