The current working directory is not in the sys.path so it cannot find my settings file... now I'm wondering what would be the "clean" way to fix this.
I have no problem running other parts of py.test but the pytest_django module cannot find my settings.
My file structure is roughly like this:
project_directory/settings.py
project_directory/app_1/
project_directory/app_2/
project_directory/manage.py
Obviously manage.py has no problems importing the settings.
Py.test is being called like this:
py.test --ds settings
The exception:
_pytest.config.UsageError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings
What is the proper way to fix this? I know I can set the PYTHONPATH variable in my environment but that just seems hacky. Why won't pytest_django import from the current directory as you would expect?
For clarity, something like this works without a problem of course:
In a file called test_settings.py:
import settings
assert settings
Running like this: py.test test_settings.py
Nevermind... just found the FAQ item that says to install the package locally. Not the prettiest way imho but it's at least a clean workaround :)
It's in the FAQ (http://pytest-django.readthedocs.org/en/latest/faq.html#i-see-an-error-saying-could-not-import-myproject-settings) but I think it's not clear enough.
It's a bug (or maybe a feature?) in py.test. There are couple of ways to solve this:
Personally I think setup.py is the best option IF you are already using it for distributing your code. If not then I'd go with other options.
The setup.py is my current solution but it's not really the prettiest solution I think. It would be useful if pytest would give a somewhat clearer error in this regard.
Perhaps pytest_django should just append the current working directory (i.e. sys.path.append('.')) to sys.path. It's a bit scary imho but it's the only way pytest_django can work so there is no way around it anyhow. All of your proposed solutions effectively add the directory to the sys.path anyhow so why avoid it :)
Guys, this should be more visible. I was struggling with this a fair bit, when I had the idea to look up closed issues...
Because this issue does indeed show up in search results, I'd like to add the most simple work-around for afaik:
manage.py by adding django_find_project = false to pytest.inipytest-pythonpath to test requirements or pip install pytest-pythonpath. This seems to add cwd to path automatically, so I didn't have to configure anything further.http://pytest-django.readthedocs.io/en/latest/managing_python_path.html#managing-the-python-path-explicitly
I encountered this issue while having a bogus pytest.ini file:
[pytest]
DJANGO_SETTINGS_MODULE = "project.settings"
Removing the quotes fixed it:
[pytest]
DJANGO_SETTINGS_MODULE = project.settings
Most helpful comment
Guys, this should be more visible. I was struggling with this a fair bit, when I had the idea to look up closed issues...