472,969 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Imports in Packages

While working within a package...what is the 'best practice' way to do
your imports.

a/__init__.py
a/one.py
a/two.py
a/b/__init__.py
a/b/cat.py
a/b/dog.py
a/c/cow.py
Suppose I am working in a/c/cow.py and I need something from a/b/
dog.py. If a/b/__init__.py contains what I need from dog.py, should I
do:

"from a.b import desiredfunction"

or

"from a.b.dog import desiredfunction"

What are your reasons for preferring one over the other (performance,
readability, portability)? Also, the same can be said of functions
from other packages...

I know that
>>from math import cos
x = cos(3)
is preferred for performance reasons over
>>import math
x = math.cos(3)
because of the required lookup. Does this mean that that I should
import as far down as possible as well? For example, "from a.b.c.mod
import func" versus "from a import fun".
Dec 17 '07 #1
1 1175
tj*****@gmail.com a écrit :
While working within a package...what is the 'best practice' way to do
your imports.

a/__init__.py
a/one.py
a/two.py
a/b/__init__.py
a/b/cat.py
a/b/dog.py
a/c/cow.py
Suppose I am working in a/c/cow.py and I need something from a/b/
dog.py. If a/b/__init__.py contains what I need from dog.py, should I
do:

"from a.b import desiredfunction"

or

"from a.b.dog import desiredfunction"
What would be the point of exposing desiredfunction in a/b/__init__ if
you still import it from a/b/dog ?-)
What are your reasons for preferring one over the other (performance,
readability, portability)?
What about encapsulation of the package's internal organisation ?

(snip)
I know that

>>>>from math import cos
x = cos(3)


is preferred for performance reasons over

>>>>import math
x = math.cos(3)

because of the required lookup.
If you only use it once, it won't make such a difference wrt/ lookup
time. The "make names local" trick is mostly useful for tight loops in
functions. Else, better to go for readability and favor the first form
IMHO - at least you don't wonder where this 'cos' stuff come from !-)
Does this mean that that I should
import as far down as possible as well? For example, "from a.b.c.mod
import func" versus "from a import fun".
This won't save anything wrt/ lookup. And I stronly suggest that you
read more about namespaces and lookup rules in Python.
Dec 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Randall Smith | last post by:
I've noticed the push by Guido and others to use absolute imports instead of relative imports. I've always enjoyed the ease of relative imports, but am starting to understand that "explicit is...
3
by: Ron_Adam | last post by:
In looking at ways to reduce the size of exe's created with py2exe, I've noticed that it will include a whole library or module even if I only need one function or value from it. What I would...
9
by: chapolim-colorado | last post by:
I like to keep my classes each in a separate file with the same name of the class. The problem with that is that I end up with multiple imports in the beginning of each file, like this: from...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
4
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
3
by: Joshua J. Kugler | last post by:
Yes, I've read this: http://mail.python.org/pipermail/python-list/2006-August/395943.html That's not my problem. I installed PlanetPlanet <http://www.planetplanet.org/via the package's...
3
by: Juha S. | last post by:
I'm getting a "AttributeError: 'module' object has no attribute 'clock'" when importing a module from within two packages related to the line: "self.lastTime = time.clock()" in the __init__() of...
3
by: Jugdish | last post by:
Why doesn't the following work? $HOME/pkg/__init__.py $HOME/pkg/subpkg/__init__.py $HOME/pkg/subpkg/a.py $HOME/pkg/subpkg/b.py # empty import a
0
by: Terry Reedy | last post by:
Stef Mientki wrote: One straightforward way is plain old absolute imports. Assume root is in the import path (for instance, in site packages) from root.dir1 import general from root.dir2...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.