473,804 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Package organization

I have a question about how to organize modules in a package. I think
there is something fundamental that I am missing. Say I'm creating a
package,
GreatPackage, which has three sub-packages, myUtilities, GoodStuff,
MoreGoodStuff.

The directories look like:

GreatPackage
|
|-- myUtilities
|
|-- GoodStuff
|
|-- MoreGoodStuff

What is the right way for modules in GoodStuff and MoreGoodStuff to
refer to modules etc... in myUtilities? I'd like to set PYTHONPATH to
just point to the directory containing GreatPackage. But, if put
something like:

from GreatPackage.my Utilities import aUtilityThingy

in a module in GoodStuff, then the whole package gets loaded,
including MoreGoodStuff. That's okay except when I'm developing and
MoreGoodStuff is broken.

What are guidelines for developing/deploying packages which contain
pacakges?

thanks,
-robert
Jul 18 '05 #1
4 4205
On Tue, 18 Nov 2003 16:38:15 -0800, Robert Ferrell wrote:
I have a question about how to organize modules in a package. I think
there is something fundamental that I am missing. Say I'm creating a
package,
GreatPackage, which has three sub-packages, myUtilities, GoodStuff,
MoreGoodStuff.

The directories look like:

GreatPackage
|
|-- myUtilities
|
|-- GoodStuff
|
|-- MoreGoodStuff

What is the right way for modules in GoodStuff and MoreGoodStuff to
refer to modules etc... in myUtilities?
There's a PEP that recommends that when importing things you refer to them
explicitly; i.e. inside GoodStuff you'd say

import GreatPackage.my Utilities

rather than just

import myUtilities

I've only benefitted from doing it that way myself in large apps that have
had many packages with sub packages. It keeps you safe from module name
clashes at different levels of the hierarchy (not that I think they're a
sensible thing to have). By name clashes I mean if you have modules
set up like this:

lib
shoe.lib
shoe.fruit

then you can't get at the top level lib module from inside shoe.fruit.
from GreatPackage.my Utilities import aUtilityThingy

in a module in GoodStuff, then the whole package gets loaded,
including MoreGoodStuff.
MoreGoodStuff won't get loaded if you import GreatPackage, unless
GreatPackage/__init__.py causes it to be loaded. Obviously, if something
in myUtilities imports something from MoreGoodStuff then python will
attempt to load MoreGoodStuff when importing myUtilities.

Also, because you're importing something from inside a module using the
"from" style imports you're risking running into circular reference
problems. In my experience from imports are best avoided, unless you're
importing a module from a package.
What are guidelines for developing/deploying packages which contain
pacakges?


For deploying, have you looked at distutils?

-- Graham
Jul 18 '05 #2
Graham Ashton <ga*****@cmedlt d.com> wrote in message news:<pa******* *************** ******@cmedltd. com>...
On Tue, 18 Nov 2003 16:38:15 -0800, Robert Ferrell wrote:
I have a question about how to organize modules in a package. I think
there is something fundamental that I am missing. Say I'm creating a
package,
GreatPackage, which has three sub-packages, myUtilities, GoodStuff,
MoreGoodStuff.

The directories look like:

GreatPackage
|
|-- myUtilities
|
|-- GoodStuff
|
|-- MoreGoodStuff

What is the right way for modules in GoodStuff and MoreGoodStuff to
refer to modules etc... in myUtilities?
There's a PEP that recommends that when importing things you refer to them
explicitly; i.e. inside GoodStuff you'd say

import GreatPackage.my Utilities

rather than just

import myUtilities

I've only benefitted from doing it that way myself in large apps that have
had many packages with sub packages. It keeps you safe from module name
clashes at different levels of the hierarchy (not that I think they're a
sensible thing to have). By name clashes I mean if you have modules
set up like this:

lib
shoe.lib
shoe.fruit

then you can't get at the top level lib module from inside shoe.fruit.
from GreatPackage.my Utilities import aUtilityThingy

in a module in GoodStuff, then the whole package gets loaded,
including MoreGoodStuff.


MoreGoodStuff won't get loaded if you import GreatPackage, unless
GreatPackage/__init__.py causes it to be loaded. Obviously, if something
in myUtilities imports something from MoreGoodStuff then python will
attempt to load MoreGoodStuff when importing myUtilities.

Ah Ha! Somehow I'd convinced myself that GreatPackage/__init__.py had
to import all the submodules. So that's what I was doing. That's
obviously wrong. Now that I've corrected that mistake I see that what
I was having trouble with is not an issue.
Also, because you're importing something from inside a module using the
"from" style imports you're risking running into circular reference
problems. In my experience from imports are best avoided, unless you're
importing a module from a package.


I don't understand this comment. How does "from" style importing
increase the risk of circular imports?

Thanks for the clarification!
-robert
Jul 18 '05 #3
On Wed, 19 Nov 2003 14:23:33 -0800, Robert Ferrell wrote:
Graham Ashton <ga*****@cmedlt d.com> wrote in message news:<pa******* *************** ******@cmedltd. com>...

Also, because you're importing something from inside a module using the
"from" style imports you're risking running into circular reference
problems. In my experience from imports are best avoided, unless you're
importing a module from a package.


I don't understand this comment. How does "from" style importing
increase the risk of circular imports?


Imagine you've got two modules, spam and eggs.

When you import a module it's top level statements get evaluated. Imagine
that spam imports something from eggs. The interpreter (whilst importing
spam) pauses to import eggs. What if eggs tries to import something from
spam?

You might think, ah, that's a circular import. But it isn't necessarily.
You see, when it starts importing spam the interpreter stores an empty
module object in sys.modules and fills in the details once it's
finished importing spam. Consequently when it's trying to import spam
again from inside eggs it finds a module reference in sys.modules for spam
and can carry on happily.

Things only break on you if eggs is trying to refer to something inside
spam that the original import of spam hasn't filled in yet.

I'll try and give you an example. Here's the spam module:

import eggs

def count_spam():
return 1

And the eggs module:

from spam import count_spam

def count_eggs_and_ spam():
return count_spam() + 1

If your main script then tries to import spam it'll blow up:

% python
Python 2.3.2 (#1, Dec 8 2003, 10:49:17)
[GCC 3.3.2 20031022 (Gentoo Linux 3.3.2-r3, propolice)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
import spam

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "spam.py", line 1, in ?
import eggs
File "eggs.py", line 1, in ?
from spam import count_spam
ImportError: cannot import name count_spam

It's because the spam module in sys.modules hasn't been filled in yet, as
the interpreter put off importing the contents of spam until the "import
eggs" statement returned. Replace the from import with a normal import and
the problem goes away. Here's a replacement eggs module:

% cat eggs.py
import spam

def count_eggs_and_ spam():
return spam.count_spam () + 1

It's no longer an issue as the spam.count_spam () function doesn't get
evaluated until the count_eggs_and_ spam() function is called, which is
long after both the modules have been fully imported.

Stay away from from, it's dangerous. And in my opinion you get less clear
code.

Incidentally, I learnt the nitty gritty of this from Martelli's
Python in a Nutshell. Recommended.

-- Graham
Jul 18 '05 #4
Robert Ferrell wrote:
Also, because you're importing something from inside a module using the
"from" style imports you're risking running into circular reference
problems. In my experience from imports are best avoided, unless you're
importing a module from a package.


I don't understand this comment. How does "from" style importing
increase the risk of circular imports?


python has no problem doing circular (or recursive) imports, but you're likely
to run into problems if you're doing recursive imports and you're referring to
stuff from one module on the *module-level* in another module. from-import
does exactly that.

for a diskussion, see the section on "recursive imports" on this page:

http://www.effbot.org/zone/import-confusion.htm

</F>


Jul 18 '05 #5

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

Similar topics

10
12377
by: Amardeep Verma | last post by:
Hi, I would like to determine the Country and Organization from a given IP. I have to write a JSP, which when given the IP address gives the country and the organization to which the IP belongs. The organization may be the ISP, or a MNC or any other corporate/government body, or it may be of an individual. I have to do it progamitically from inside a JSP? Any suggestions anybody Thanks in Advance for your reply
4
495
by: Thomas Lotze | last post by:
Hi, I've two questions concerning organizing and naming things when writing a Python package. - Naming of classes: I'm writing a library that reads PDF files. I have a data structure that represents the large-scale structure of a PDF file (header, trailer, incremental updates etc), and I'll have others, e.g. one that represents the document as a collection of logical objects (page descriptions, images etc).
6
1390
by: twoeyedhuman1111 | last post by:
I was wondering, what group standardized c++?
1
10928
by: Steven T. Hatton | last post by:
I think Danny was one cup of coffee shy of full consciousness when he wrote this, but the gist of it makes sens to me: "C++ Project Organization Guidelines Last updated May 26, 2005. http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=175 Last week's article about inline functions subtly brought into the limelight another important issue, namely how to organize the files of a typical C++ program, or project. This week I...
3
1543
by: david.kao | last post by:
Hi All: I tried to uninstall a runtime security package from a user's machine. Based on document I have read, all I need to do is right click on MSI file, and choose uninstall from menu. After I chose uninstall, the package seemed to uninstall itself. But when open .NET configuration 1.1, I did not see the package got remove. Thanks in advance DK
11
3869
by: fortepianissimo | last post by:
Say I have the following package organization in a system I'm developing: A |----B |----C |----D I have a module, say 'foo', that both package D and B require. What is the best practice in terms of creating a 'common' package that hosts
15
2003
by: GS | last post by:
Hello, I am looking for any opensource package for "appointment system" for doctors, please let me know. This is just o take Name/Phone/time, incase if there is not any opensource, then is it hard to implement using web/php/mysql?. Please let me know. GS
3
1585
by: someone | last post by:
Hi all, I am using Sql Server 2000. I need to create a very simple app which when clicked would fire the execution of a dts package on the sql server. The app itself would sit on the client machine. Is this possible? If so, how? Thanks in advance.
3
4215
by: sulekha | last post by:
Hi all, I was reading the book "Write Great Code vol 1" by Ryndall hyde in this book chapter 11 is named as Memory architecture & Organization. in this chapter there is a section named "Run time memory organization" author goes on to say that the run time memory is divided into different sections namely
1
1545
by: mypetslug | last post by:
Hi, I'm sorry if this has been asked before, but I can't seem to find an answer to this anywhere and so far, trial and error hasn't gotten me far either. Using python 2.4, I've created a testing application. When the app starts up, I do a from tests import * and it looks at the /tests directory and loads up all the tests it finds at that time. This works fine when interpreting the python code directly. The problem I'm having is...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.