472,328 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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
1 1143
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a...
4
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that...
17
by: mohan | last post by:
Hi Guys, I've been using the following IDE, "Pythonwin - Python IDE and GUI Framework for Windows. Copyright 1994-2001 Mark Hammond " ...
3
by: Tinka | last post by:
Hi, I'm having trouble with my new directory hierarchy. My little python programme worked as long as I had all my modules in one big file. I have...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write...
7
by: tinnews | last post by:
I'm just beginning to create some python modules for my own use and I'm wondering where to put them. Initially I have put them in $HOME/bin and I...
5
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies...
16
by: ssecorp | last post by:
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also,...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.