[Tutor] Python OO

Peter Otten __peter__ at web.de
Sun Mar 29 16:42:33 CEST 2015


Dave Angel wrote:

> On 03/28/2015 09:16 PM, Juan C. wrote:
>> Ok, so, let me try to express what I think is 'right' here according to
>> what you said.
>>
>> My code structure needs to be something like that:
>>
>> pycinema
>> - package: pycinema
>> - - __init__.py
>> - - api.py
>> - - actor.py
>> - - movie.py
>> - - serie.py
>> - __main__.py
>>
> 
> I'd suggest that you NEVER call a module  __main__.py   The name
> "__main__" is reserved for identifying the script file, and is faked
> during the program initialization.
> 
> By using that name for an imported file, you could get some very
> confusing errors later.

No, the __main__.py module is used to invoke a package:

$ mkdir mypackage
$ echo 'print("Hi")' > mypackage/__main__.py
$ python3 -m mypackage
Hi

In Python 2 you need an __init__.py module to run the above example:

$ python -m mypackage
/usr/bin/python: No module named mypackage
$ touch mypackage/__init__.py
$ python -m mypackage
Hi

See also <https://docs.python.org/3/library/__main__.html> (there should be 
better documentation, but I didn't manage to find it...)



More information about the Tutor mailing list