problem with packages and path
daniel.watrous at gmail.com
Wed Aug 27 19:41:25 CEST 2008
On Aug 27, 11:00 am, Paul Boddie <p... at boddie.org.uk> wrote:
> On 27 Aug, 18:44, Daniel <daniel.watr... at gmail.com> wrote:
> > I'm writing some unit tests for my python software which uses
> > packages. Here is the basic structure:
> > mypackage
> > 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
> One thing to note: if you are running alltests.py directly (which you
> don't mention explicitly) then even if you do so from the directory
> containing the root of the package hierarchy (mypackage), it will be
> the directory containing alltests.py (unittests) which will appear on
> the PYTHONPATH/sys.path. Sometimes it's easy to take this behaviour
> for granted when running programs.
> A question: what happens if you prepend the directory containing the
> root of package hierarchy to sys.path using insert (at element 0)
> rather than append?
I changed it to 'sys.path.insert(0, newpath)', as you suggest, but it
doesn't fix the error.
I did notice that the directory containing the file alltests.py ends
up in the path. I've also noticed that (in WinXP at least) the
sys.path modification inserts a '..' in the path, rather than the full
path to 'mypackage'. I'm not sure if this makes a difference, but it
seems like I should be able to add either '..' or 'path/to/mypackage'
to the path and have it find my packages.
Any more suggestions?
More information about the Python-list