[Tutor] Possible to import imports?

Kent Johnson kent37 at tds.net
Mon Jun 23 21:03:01 CEST 2008


On Mon, Jun 23, 2008 at 1:27 PM, Rob Kirkpatrick
<robert.d.kirkpatrick at gmail.com> wrote:
> I've googled a bit and tried some things on my own, but can't seem to
> determine if it's possible to declare some standard library imports in one
> package/module then import that package/module somewhere else and reap the
> benefit of those initial imports.
>
> For example, if I have a setup like this:
>
> foo
>   __init__.py
>   bar
>     __init__.py
>     hello.py
>
> Could I put "import datetime" in foo's __init__.py and do an import of foo
> from hello.py and start using datetime in hello.py without importing
> datetime specifically in hello.py?

Yes but it will be foo.datetime. The 'import datetime' in
foo.__init__.py binds the name datetime in foo's module namespace.

>  All the examples I see import all the
> necessary standard library mods in the script/module that specifically uses
> them so either what I want to do is not possible or a bad idea.  Thoughts?

Why do this? For clarity, import the modules you need in the modules
that need them.
  import foo
  foo.datetime...
is just obscure IMO.

Kent


More information about the Tutor mailing list