problem with packages and path

Daniel daniel.watrous at gmail.com
Wed Aug 27 12:44:53 EDT 2008


Hello,

I'm writing some unit tests for my python software which uses
packages.  Here is the basic structure:

mypackage
  __init__.py
  module1
    __init__.py
    mod1.py
  module2
    __init__.py
    mod2.py
  unittests
    __init__.py
    alltests.py
    test1.py
    test2.py

within alltests.py I would expect to be able to "import
mypackage.unittests.test1".  In fact within PyScripter this works as
expected.  However, when I execute the code from the command line, I
get the following error:

ImportError: No module named mypackage.unittests.test1

I've read that "When importing the package, Python searches through
the directories on sys.path looking for the package subdirectory." -
http://docs.python.org/tut/node8.html#SECTION008400000000000000000

So I tried adding the following code to my alltests.py, which should
add the directory for mypackage to the path (and it does).

import os, sys
newpath = os.path.normpath( os.path.join( __file__, "../../" ))
sys.path.append(newpath)

I still get the same error.  Can someone please point me in the right
direction?  Thanks in advance:

Daniel



More information about the Python-list mailing list