Newbie: module structure and import question

Z

Ziong

hi all,
i have question on how to design a module structure.
for example, i have 3 files.
[somewhere]/main.py
[somewhere]/myLib/Base/BaseA.py
[somewhere]/myLib/ClassA.py

main.py
=======
from myLib.ClassA import ClassA

a = classA()
dir(a)

myLib/ClassA.py
===============
from myLib.Base.BaseA import BaseA

class ClassA(BaseA):
def __init__(self):
BaseA.__init__(self)
print "classA"

if (__name__ == "__main__"):
print "test class A"
a = ClassA()
print a


myLib/Base/BaseA.py
===================
class BaseA:
def __init__(self):
print "BaseA"


It's fine when i run main.py.
however when i run ClassA.py individually, it would fail in import
statment since the import path is incorrect.
I would like to know is something wrong in my design, or something i
missed.

Also, in practical usage, is that one class in one py file?

thx!

a python newbie
 
R

Rob Emmons

hi all,
i have question on how to design a module structure.
for example, i have 3 files.
[somewhere]/main.py
[somewhere]/myLib/Base/BaseA.py
[somewhere]/myLib/ClassA.py
....
.....
It's fine when i run main.py.
however when i run ClassA.py individually, it would fail in import
statment since the import path is incorrect.
I would like to know is something wrong in my design, or something i
missed.

I think your issue is your module search path. Take a look at the doc for
sys.path in the library reference. These are the directories that python
searchies for modules. Usually the "." directory is included in this
which makes python search the current working directory. Your example
fails because your working directories are probably different when you ran
the two modules. In any case always consider how you've setup sys.path
and your libraries and modules.
Also, in practical usage, is that one class in one py file?

I'm not exactly clear what your asking -- but I think yor asking if you'd
normally only put one class in one py file. My answer is no -- generally
you'd put many functions and classes in each py file. Modules are high
level and should be used to create libraries essentailly -- this means
many fucntions and classes per module.

Rob
 
Z

Ziong

Thx Rob.

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.
sometime like my example, even i don't know where the root directory of my
module will place, and i expect it can place in anywhere, how should i set
the sys.path?
i know that maybe a stupid question, please forgive me, i'm just a newbie.
i have read all online documents in python.org. but i wouldn't find the
answer.

yes, i'm asking is it normally only put one class in one py file.
thanks for your advice.
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. i'm
quite confuse in this, maybe because i learn Java before.

Thx again, Rob.

Rob Emmons said:
hi all,
i have question on how to design a module structure.
for example, i have 3 files.
[somewhere]/main.py
[somewhere]/myLib/Base/BaseA.py
[somewhere]/myLib/ClassA.py
....
.....
It's fine when i run main.py.
however when i run ClassA.py individually, it would fail in import
statment since the import path is incorrect.
I would like to know is something wrong in my design, or something i
missed.

I think your issue is your module search path. Take a look at the doc for
sys.path in the library reference. These are the directories that python
searchies for modules. Usually the "." directory is included in this
which makes python search the current working directory. Your example
fails because your working directories are probably different when you ran
the two modules. In any case always consider how you've setup sys.path
and your libraries and modules.
Also, in practical usage, is that one class in one py file?

I'm not exactly clear what your asking -- but I think yor asking if you'd
normally only put one class in one py file. My answer is no -- generally
you'd put many functions and classes in each py file. Modules are high
level and should be used to create libraries essentailly -- this means
many fucntions and classes per module.

Rob
 
R

Rob Emmons

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
need.

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
module.

Thanks,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top