problem with packages and path

Paul Boddie paul at boddie.org.uk
Wed Aug 27 19:00:34 CEST 2008


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?

Paul



More information about the Python-list mailing list