Python unittest import issues Python unittest import issues flask flask

Python unittest import issues


Think about what is on your PYTHONPATH. The toplevel package for your project is my_project, so that must be the start of any import for something in your project.

from my_project.foo import bar

You could also use a relative import, although this isn't as clear, and would break if you ever changed the relative location of the module you were performing this import from.

from ..foo import bar

Ideally, the test folder is not a package at all, and is not part of your application package. See pytests's page on good practices. This requires that you add a setup.py to your package and install it to your virtualenv in develop mode.

pip install -e .

Don't run the tests by pointing directly at a file within your application. After structuring/installing your project correctly, use the discovery mechanism for whatever framework you're using to run the tests for you. For example, with pytest, just point at the test folder:

pytest tests

Or for the built-in unittest module:

python -m unittest discover -s tests


import syssys.path.append('/path/to/my_project/')

Now you can import

from foo import bar


You can use relative imports:

from ..foo import bar

https://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports

but i think too that using absolute paths by installing your project in venv is better way.