0

I know Python imports are a perennial headache, but I can't see why this should be broken:

I have the following file structure:

webapp/
    README.md
    ...
    webapp/
        __init__.py
        ...
        tests/
            __init__.py
            test_integration.py

webapp/init.py is initializing a Flask project, and webapp/tests/test_integration.py is trying to test the homepage.

When I cd into webapp/webapp and run python tests/test_integration.py (which includes the line import webapp), I get the error ImportError: No module named webapp. I tried cding up one directory, into the upper webapp, as well as down into tests, but same deal. I tried adding the absolute path to the inner webapp directory to PYTHONPATH, but no avail.

This is strange, since I'm using a similar structure in another project and it seems to be working fine.

Also, if I move test_integration.py all the way up into the outer webapp directory, it runs just fine. Any reason why the test can't import webapp from any further inside the file tree?

Any insight? Thanks!~

kronosapiens
  • 1,333
  • 1
  • 10
  • 19
  • to avoid this issue altogether I usually install the library into the virtual environment, that way I can import from anywhere. – notorious.no Jul 25 '14 at 14:55

1 Answers1

0

"I tried adding the absolute path to the inner webapp directory to PYTHONPATH, but no avail."

You need to add your outer webapp directory to PYTHONPATH.

export PYTHONPATH=/path/to/webapp:$PYTHONPATH

Then your inner webapp directory will be visible, and you can import it.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159