Nyxynyx
Nyxynyx

Reputation: 63687

Python ImportError: No module named path.X, but works with nosetests

In level1/level2/bar.py, when trying to import a variable foo from level1/foo.py, I'm getting an error:

ImportError: No module named level1.foo

level1/foo.py

bar = 123

level1/level2/bar.py

import level1.foo
print level1.foo.bar

However when running nosetests defined in level1/level2/test_bar.py, it is able to locate level1.foo.py!

level1/level2/test_bar.py

import level1.foo

def test_foobar():
    print level1.foo.bar

Why is it possible to import via level1.foo during a nosetest, but not able to do so with a regular python script?

Here's the file structures, there are __init__.py files in each directory as well.

enter image description here

Additionallly, my friend using PyCharm does not seem to face such a problem. Does PyCharm automatically handle these imports, but breaks outside of PyCharm unless the root of the project is added to PYTHONPATH?

Upvotes: 0

Views: 247

Answers (1)

dbn
dbn

Reputation: 13940

This sounds like a PYTHONPATH issue. If you are running nosetests from the app directory, the level1.foo import will be available. If you are running level1/level2/bar.py from the apps/level1/level2 directory, level1.foo would not (necessarily) be in your path.

Upvotes: 1

Related Questions