python packages and __name__ query.

Gabriel Genellina gagsl-py at yahoo.com.ar
Sat Jan 20 22:41:42 CET 2007


"krishnakant Mane" <researchbase at gmail.com> escribió en el mensaje 
news:f81479dd0701201112i7f443c17w588ea3f6f7fc7d42 at mail.gmail.com...

> I use cx freze for creating a python executable.
cx_freeze is almost irrelevant here; all the following considerations are 
about your application structure, it doesnt matter if you use cx_freeze 
later (or any other tool).

>  my software is
> essentially a package containing a few modules and one file where I
> created the main() function.
> if needed and suggested by experts I may as well put it in my
> __init__.py file of the package.
No, usually the main function is outside the package. The package is a 
library, and main() *uses* that library, as a client.

> coming back to the point.  where do I put the following code
> if __name__ == "__main__":
>   main()
I would say, "In the main script, obviously :)" But since you're asking it 
may not be so obvious...
It appears that your main script is exec.py - so there should be it.
I suppose you run your application this way:
exec.py -some -options file.ext
(perhaps a bare exec.py is enough) so using the idiom above, inside exec.py 
you can detect when someone is running it as a script.

> I have 5 modules in my package each doing a specific task.
> and a file that contains the main() function.  the 5 modules and the
> exec.py file all are in the same package.  if it is a wrong practice
> please correct me.
exec.py is not a good name, since this is the "public" entry point to your 
application, and should be more meaningful.
Usually it is not inside the package, just uses it.
I suggest you look into other Python applications, to see how they are 
layered.

-- 
Gabriel Genellina 





More information about the Python-list mailing list