[Tutor] Fwd: Project tree

Danny Yoo dyoo at hashcollision.org
Tue Nov 11 20:00:19 CET 2014


---------- Forwarded message ---------
From: Wiktor Matuszewski <pywkm at wukaem.pl>
Date: Tue Nov 11 2014 at 4:47:10 AM
Subject: Re: [Tutor] Project tree
To: Danny Yoo <dyoo at hashcollision.org>


W dniu 2014-11-11 o 03:02, Danny Yoo pisze:

> See: https://docs.python.org/3/tutorial/modules.html#intra-
package-references
> for more details.
>
> If you have more questions, please feel free to ask!

Hi Danny, thanks.
I've seen this part of tutorial earlier, but obviously I misunderstood
it. This part especially: "Note that relative imports are based on the
name of the current module".

When I execute moda.py separately - there's no problem with importing
modb relatively from it. And I thought, that during importing moda from
start.py, for short moment I'm "locally" in moda (that this is current
module) and every import statements in it works relatively to moda.py
not to start.py.

Can you please explain what is happening step by step when I'm executing
only moda.py, and what is happening when I'm executing start.py? And
what is different when it's executed in Py2?
Or even better, maybe you can write short snippet of code, which I could
paste to all three files, and it will print out all this differences
during execution (you know - so I could be able to track current_module,
path_where_i_am, and path_of_relative_import). I tried to put
"print(__name__)" and "print(os.getcwd())" in every file in this tree,
but it's not quite that. Output looks the same in Py2 and Py3, so it
gives me no explanation.


Is there a simple way of importing modb from moda (in Py3), that will
work when moda.py is executed directly (you know - for unit testing),
but also when start.py is executed (for testing whole "package"). For
now I use this try/except block:

try:
     import modb
except ImportError:
     from . import modb

But I'm not sure if it's Pythonic, and if there's no simpler solution.

--
Best regards,
Wiktor Matuszewski
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141111/208f2dd6/attachment.html>


More information about the Tutor mailing list