trouble with unittest and namespaces - can anyone help?

Peter Hansen peter at engcorp.com
Tue Nov 11 18:55:16 EST 2003


Lol McBride wrote:
> 
> I've hit a snag which I believe is to do with namespaces (specifically the
> os.path).

Probably more a problem with sys.path, but perhaps that's what you meant.

> The programs in analysis do not import classes or modules from anywhere
> else and the tests in the tests directory run fine.My problem is with the
> tests done for prog3 and prog4, both of which import from the analysis
> package e.g
> from analysis import prog1
> 
> All tests fail to run and a 'no such module as analysis message' is
> displayed.
> Could anyone give me an idea as to how to resolve this problem?

The simplest approach is to insert the following at the top of your
tests:

 import sys
 sys.path.append('../..')

Or something like that... basically if the directory containing
the files to be imported (or the one containing the package folder, 
in the case of a Python package) is not in sys.path, you won't be 
able to import the module or package properly.

Given that your tests are in a subfolder of the one where prog2.py
is found, I'm not sure how you are invoking the tests right now 
such that they are working at all.  How do they find "prog2"
when they import it right now?

-Peter




More information about the Python-list mailing list