Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest cannot find settings #58

Closed
wolph opened this issue Nov 20, 2013 · 7 comments
Closed

Pytest cannot find settings #58

wolph opened this issue Nov 20, 2013 · 7 comments

Comments

@wolph
Copy link

wolph commented Nov 20, 2013

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?

@wolph
Copy link
Author

wolph commented Nov 20, 2013

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

@wolph
Copy link
Author

wolph commented Nov 20, 2013

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 :)

@wolph wolph closed this as completed Nov 20, 2013
@rafales
Copy link
Contributor

rafales commented Nov 20, 2013

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:

  • just adjust your PYTHONPATH environment variable to include project directory
  • insert path to your project into sys.path in your conftest.py file
  • if you're using virtualenvwrapper then use add2virtualenv script
  • you can write a proper setup.py script, which will adjust python path automatically (via .pth files) when installed in development mode

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.

@wolph
Copy link
Author

wolph commented Nov 20, 2013

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 :)

@kissgyorgy
Copy link

Guys, this should be more visible. I was struggling with this a fair bit, when I had the idea to look up closed issues...

@benjaoming
Copy link

Because this issue does indeed show up in search results, I'd like to add the most simple work-around for afaik:

  • Disable searching for manage.py by adding django_find_project = false to pytest.ini
  • Add pytest-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

@Natim
Copy link

Natim commented May 1, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants