Question about import

Frank Millman frank at chagford.com
Fri Sep 11 02:40:24 EDT 2015


"Ian Kelly"  wrote in message 
news:CALwzidm3khNaGTt0OhVEo5bhqk1tFEjBUUUinW9TNUxrpnr8cw at mail.gmail.com...

On Thu, Sep 10, 2015 at 1:12 AM, Frank Millman <frank at chagford.com> wrote:
> > That makes me wonder if, in my project, I can import all modules inside
> > 'start.py', and then just use 'import package_name' inside each module?
>
> You can, but for readability and reuse I think it's better to be
> explicit in each module and import the fully qualified module names
> that are needed, rather than relying on some other module to import
> them for you.

I don't disagree. However, I started this exercise because I found a 
situation in one of my modules where I was referring to another module 
without ever importing it, and it worked! It took me quite a while to track 
down what was happening. So there could be a case for being explicit in the 
other direction - import all modules up front, and explicitly state that all 
modules are now free to reference anything in any other module. I will give 
it more thought.

> > Another question - I thought that, because aa.py and bb.py are in 
> > different
> > sub-directories, I would have to set them up as packages by adding
> > '__init__.py' to each one, but it works fine without that. What am I
> > missing?

> That surprises me also, but I suspect it's because they're
> subdirectories of the current working directory rather than packages
> found on the sys.path.

I had a look at PEP 420, as mentioned by Peter. I did not understand much of 
it, but it did say that omitting __init__.py incurs an additional overhead, 
so I will stick with including it.

Many thanks for the useful input.

Frank





More information about the Python-list mailing list