Newbie: module structure and import question

Rob Emmons rmemmons at
Sun Jan 16 14:33:05 EST 2005

> yes i know it's related to search path, but i don't know how to set it in a
> practical way (beside hard coding).
> my concern is, if i want to create a custom module/library, i don't know
> what py file will import it and where the working directory should be.

Regarding where the current working directory is -- on Linux your right,
you probably don't know where it is going to be.  On Windows -- you can
pretty much demand that users set up the launcher so that your python
program is opened in a specific location -- typically the root directory
of the installed package.  So in the windows situation -- if all your
components are in that package directory you can access everything via
relative roots.  Linux is not so easy -- I'll get to that.

On the other hand, if your installing a general directory library module
that your going to use from a variety of other programs, then you need to
have the installer setup things so other applications can find it.  The
standard way to do this is to use the distutils package that is part of the
python distribution to install these modules in a standard location which
should already be in the search path.  If you don't want to do that you
have to do something special:  on windows create a registry entry that
can be accessed and looked up probably, or setup the default python path
for your system (or user) to include the extra directories, or like GNOME
does on Linux -- create a script in the executable search path
that your program or installer can run -- and have it return the path you 

So on the application side, you'd find out the location of the library as
described above and set sys.path.  You can either do this at launch time
in your main program -- or you can have the installer hard code it at the
top of the main program.

This is not really any different then for example using shared libraries. 
You either need to install all of your modules in the search path to start
with or you need to modify the search path by some method.

There is one more thought -- if you want to find out where the python
module is in the file system -- there are sometimes ways to do this --
I've never done this but I have some notes somewhere about this.  This
might also be helpful in finding directories for example.  If your
interested in this -- maybe someone else here can remember?
> But if i put a set of classes in a py file as a module,  will it increase
> the dependency of each class?
> back to my example, of course, i can put BaseA and ClassA together in one py
> file. what should i do when i need to add one more class later, "ClassB",
> which also extend BaseA. Put it into the same file or in a new file? if put
> in in the same file, i think it should difficult to maintain versioning.

Versioning -- hmm.  Well I think it depends.  If ClassB is intended to be
a general capability that you want to add to to moduleA (the module with
BaseA and ClassA), then add it to moduleA but make the module upwardly
compatible.  If you want to track versions, then just keep old version
snapshots if you need to or use something like CVS to do it for you.  I'm
pretty basic so I keep old copies but try to make my general libraries
upwardly compatible if possible.

Of course on the other hand, if the new class classB has a special function
then I'd either create a new module, or I'd just include it into my main
program module, or some other application module.  To merge the functions
just import what you need into that module.  I for example often use "from
mymodule import *" which imports everything rather than the limited
imports you used -- nothing wrong with either, just a matter of taste. I
also often use "import" and write the full module name when I reference
things too. 

The point is -- I personally think one should look at modules as being
collections of functions, classes, and other constructs that form
librararies rather than putting each class and function in a separate


More information about the Python-list mailing list