[Import-SIG] PEP 395 (Module aliasing) and the namespace PEPs
pje at telecommunity.com
Wed Nov 16 21:06:51 CET 2011
On Wed, Nov 16, 2011 at 1:21 PM, Eric Snow <ericsnowcurrently at gmail.com>wrote:
> But which is more astonishing (POLA and all that): running your module
> in Python, it behaves differently than when you import it (especially
> __name__); or you add an __init__.py to a directory and your *scripts*
> there start to behave differently?
To me it seems that the latter is more astonishing because there's less
connection between your action and the result. If you're running something
differently, it makes more sense that it acts differently, because you've
changed what you're *doing*. In the scripts case, you haven't changed how
you run the scripts, and you haven't changed the scripts, so the change in
behavior seems to appear out of nowhere.
When I was learning Python, it took quite a while before I realized
> that modules are imported and scripts are passed at the commandline;
> and to understand the whole __main__ thing.
It doesn't seem to me that PEP 395 fixes this problem. In order to
*actually* fix it, we'd need to have some sort of "package" statement like
in other languages - then you'd declare right there in the code what
package it's supposed to be part of.
> It has always been a pain, particularly when I wanted to
> just check a module really quickly for errors.
What, specifically, was a pain? That information might be of more use in
determining a solution.
If you mean that you had other modules importing the module that was also
__main__, then I agree that having a solution for __main__-aliasing is a
good idea. I just think it might be more cleanly fixed by checking whether
the __file__ of a to-be-imported module is going to end up matching
__main__.__file__, and if so, alias __main__ instead.
> However, lately I've actually taken to the idea that it's better to
> write a test script that imports the module and running that, rather
> than running the module itself. But that came with the understanding
> that the module doesn't go through the import machinery when you *run*
> it, which I don't think is obvious, particularly to beginners. So
> Nick's solution, to me, is an appropriate concession to the reality
> that most folks will expect Python to treat their modules like modules
> and their scripts like scripts.
You lost me there: if most people don't understand the difference, then why
are they expecting a difference?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Import-SIG