Newbie: module structure and import question

Rob Emmons rmemmons at member.fsf.org
Thu Jan 13 20:59:57 EST 2005


> 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





More information about the Python-list mailing list