4

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ali_Waris
  • 1,944
  • 2
  • 28
  • 46
  • @jonrsharpe Can you please be a little more descriptive. I am a ""newbie"". – Ali_Waris Jun 16 '16 at 09:46
  • Can you please suggest some links for simpler projects? – Ali_Waris Jun 16 '16 at 09:52
  • @jonrsharpe Consider a standalone application which can be executed. Now, assume it has a complex directory structure. Now, can you please help me in locating the starting point of the Python application? – Ali_Waris Jun 16 '16 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114844/discussion-between-ali-waris-and-jonrsharpe). – Ali_Waris Jun 16 '16 at 12:46

2 Answers2

8

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)

Nicolas Cornette
  • 772
  • 7
  • 11
  • Where do I need to look for the code ? In each module?? Please forgive me for being silly if I seem to.. – Ali_Waris Jun 16 '16 at 09:50
  • Usually the project should be documented accordingly, because you may find several entry points in a project – Nicolas Cornette Jun 16 '16 at 09:52
  • The project http-prompt doesn't provide a standard way to execute from source, it uses function `cli(url, http_options)` in `http_prompt/cli.py`, this is defined in setup.py `entry_points` : `http-prompt=http_prompt.cli:cli`I recommend you install the project with `$ pip install http-prompt` – Nicolas Cornette Jun 16 '16 at 10:06
  • What if the code can be executed from source? How to get the start point of the code then? – Ali_Waris Jun 16 '16 at 12:42
  • Note that this refers to *scripts*, not *packages*, which may not have any `if __name__ == '__main__':` blocks. – jonrsharpe Jun 16 '16 at 12:57
  • If `if __name__ == '__main__':` is not present, any code outside a block is executed anyway. – Nicolas Cornette Jun 16 '16 at 13:28
3

The Python equivalent of a manifest file is generally setup.py, so this is a good place to start looking. A package can:

If neither of these is provided, the package is probably designed to be imported 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?)

Community
  • 1
  • 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437