467,895 Members | 1,384 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

handling modules in packages

Hi,

I am working on a package that contains a number of
different modules:
ls pyAstro
__init__.py
constants.py
conversion.py
observation.py
orbit.py
transformation.py

however, I find that several of the modules have the
same import statements:

orbit.py:

import numpy
import constants
import conversion
import observations

observations.py:

import numpy
import constants
import conversions
import transformations

The modules themselves are not overly large, but it bugs
me to have to import numpy twice (or even more as the
number of modules grow). Is there a way to import numpy
once in the package (like in the __init__.py file) such that
it is accessible to all the modules? Or is the multiple imports
just something one has to live with?

Thanks for any help or direction to webpages discussing this
topic.

Cheers
Tommy

Sep 5 '07 #1
  • viewed: 1037
Share:
1 Reply
Tommy Grav wrote:
Hi,

I am working on a package that contains a number of
different modules:
ls pyAstro
__init__.py
constants.py
conversion.py
observation.py
orbit.py
transformation.py

however, I find that several of the modules have the
same import statements:

orbit.py:

import numpy
import constants
import conversion
import observations

observations.py:

import numpy
import constants
import conversions
import transformations

The modules themselves are not overly large, but it bugs
me to have to import numpy twice (or even more as the
number of modules grow). Is there a way to import numpy
once in the package (like in the __init__.py file) such that
it is accessible to all the modules? Or is the multiple imports
just something one has to live with?
Essentially, yes. That's the way it is, and it's better for understanding
how things work in the respective submodules.

However, you can stuff things into the __builtins__-module using setattr,
and thus make names known globally.

But it's a hack, and it means that you possibly create conflicts if
different modules have different ideas on what is supposed to live under
one key.

so - don't do it. And live with the imports. After all, that's only c'n'p,
and not of the bad kind.

Diez
Sep 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nick Coghlan | last post: by
15 posts views Thread by Nick Coghlan | last post: by
4 posts views Thread by Fabian Braennstroem | last post: by
7 posts views Thread by tinnews | last post: by
5 posts views Thread by John Ladasky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.