Organizing code - import question

Brian Blais bblais at bryant.edu
Thu May 3 11:41:00 EDT 2007


Hello,

I am trying to organize some of my code, and am having a little trouble with the 
import logic.  I find I often have something like:

MyPackage/
    Part1/ # wants to use functions in Common/
      __init__.py  # does "from MyClass1 import MyClass1", etc,...
      MyClass1.py
      MyClass1a.py  # depends on MyClass1
      MyClass1b.py  # depends on MyClass1

    Part2/  # wants to use functions in Common/
      __init__.py  # does "from MyClass2 import MyClass2", etc,...
      MyClass2.py   # depends on MyClass1 also, such as containing a list of MyClass1
      MyClass2a.py  # depends on MyClass2
      MyClass2b.py  # depends on MyClass2

    Common/
      __init__.py  # does "import fun1,fun2", etc,...
      fun1.py
      fun2.py



So I have some common utilities that both classes want to access, and I have two 
separate class definitions, of which one depends on the other.  In MyClass2.py, I 
can't seem to do:

import Common.fun1

or

from Part1.MyClass1 import MyClass1


I think I am either missing some syntax/path thing, or I am thinking about the 
organization in entirely the wrong way.  Currently, as a hack, I am simply copying 
the code from Common into the other two directories, and making a link to the Part1 
directory in the Part2 so I can import it.  There must be a better way, yes?


			thanks,

				Brian Blais

-- 
-----------------

              bblais at bryant.edu
              http://web.bryant.edu/~bblais



More information about the Python-list mailing list