Problems testing modules in namespace package

Create issue
Issue #56 resolved
Chris Dent created an issue

When using namespace_packages if I am testing a new module in a namespace that is already installed into sys.path (e.g. an egg in that namespace is already on sys.path) then the new module fails to import when running the test file against py.test.

If I run the same test file with python (and have the test executed) all goes well and the correct code is imported, and the test passes as expected.

While narrowing this down I created a new namespace package and tested it in place (before installing it as an egg). That first test worked fine. I then created a second module in the same namespace and tested it before installing the first module. That tested fine.

I then installed the first module. Subsequent tests of the second module failed.

I'm in Python 2.6.1, py 1.0.2 on OS X 10.6.1

I have attached a tarball which ought to demonstrate this situation.

Inside the tarball are two directories: test.one and test.two. Both contain modules which are destined for the test1234 namespace. As they stand the single test in test dir in each should run just fine. However if 'python setup.py install' is run in the test.one directory, installing test1234.one as egg, the test in test.two will fail to 'from test1234.two import two' (assuming that things don't work).

At first glance this makes me think "oh namespace_packages" are yucky, but if you run 'python test/test_two.py' things work fine...

Happy to do any additional digging about as required. Thanks.

A little more cookbooky on the tarball:

  • tar zxvf testme.tar.gz
  • cd testme/test.one
  • py.test test # expect success
  • cd ../test.two
  • py.test test # expect success
  • cd ../test.one
  • sudo python setup.py install # install egg
  • cd ../test.two
  • py.test test/test_two.py # except failure "E ImportError: No module named two"
  • python test/test_two.py # expect success

Comments (4)

  1. Chris Dent reporter

    A workaround is to symlink the namespace directory to a different name (not used in any namespace) and import from that namespace in the tests.

    It's all a big hack, but so are namespaces...

  2. Holger Krekel repo owner
    • changed milestone to 1.1

    Hi,

    i reproduced your steps on Ubuntu, python 2.6.2, using a virtualenv. But i didn't get a failure.

    Here is an excerpt with more info after i have done "python setup.py install" in the test_one dir:

    >>> import test1234
    >>> test1234
    <module 'test1234' from '/home/hpk/venv/issue56/lib/python2.6/site-packages/test1234.one-0.0.0-py2.6.egg/test1234/__init__.pyc'>
    >>> import py
    >>> py
    <module 'py' from '/home/hpk/venv/issue56/lib/python2.6/site-packages/py-1.0.2-py2.6.egg/py/__init__.pyc'>
    >>>
    

    and here is the output of running "py.test test/test.two":

    ============================= test session starts ==============================
    python: platform linux2 -- Python 2.6.2 -- pytest-1.0.2 -- /home/hpk/venv/issue56/bin/python
    test object 1: /home/hpk/hg/py/trunk/issue56/testme/test.two/test/test_two.py
    
    test/test_two.py:6: test_two PASS
    
    =========================== 1 passed in 0.03 seconds ==========================
    
    

    I never have used the setuptools/namespace feature so am i bit unsure how to proceed/investigate. Could you maybe check if creating a virtualenv produces the same failure for you?

    best, holger

  3. Log in to comment