Propagate import for all modules in a package.

Ben Finney ben+python at benfinney.id.au
Fri Jul 17 19:25:47 EDT 2009


Phil <phily05 at gmail.com> writes:

> I was thinking that I could just, for example, 'from datetime import
> datetime' in __init__.py and have the ability to use 'datetime'
> anywhere in any of the modules in 'package'.

That would break one of the very nice features of Python's namespace
system: that, because nothing[0] comes into a module without an explicit
‘import foo’, you can always trace where ‘foo’ came from in the source
code.

> This didn't work for me. Did I just do something wrong? Is what I am
> trying to do possible?

If it were possible, we'd have the nightmare of not knowing where the
names in a module were coming from, without having to search all over
the package just to find where these implicit imports were taking place.
No, thanks; I'm glad it's not possible.

> Thanks in advance.

[0] Two exceptions: There are a certain number of built-in names,
    strictly limited and well documented in the standard Python
    documentation. There are also those who use a namespace-clobbering
    ‘from foo import *’, to which the solution is a clue-bat applied to
    the back of the skull.

-- 
 \        “Pinky, are you pondering what I'm pondering?” “Wuh, I think |
  `\      so, Brain, but how will we get three pink flamingos into one |
_o__)                     pair of Capri pants?” —_Pinky and The Brain_ |
Ben Finney



More information about the Python-list mailing list