[Django]-Django py.test does not find settings module

24👍

Because django.conf.settings is lazy it will attempt to import settings module only when you try to access it. That’s why your test doesn’t fail when you simply import settings object.

Your problem is already discussed here: https://github.com/pelme/pytest_django/issues/23

This is an issue with pytest and not with pytest-django itself. Pytest for some reason removes current directory from sys.path. It should be easy to work around it.

Solution 1:

PYTHONPATH=`pwd` py.test

Solution 2:

add this to your conftest.py (I assumed that conftest.py is in the same directory your apps are):

import os
import sys

sys.path.append(os.path.dirname(__file__))

Solution 3 (if you’re using virtualenv wrapper):

When you start a new project just add project’s root directory to virtualenv’s PYTHONPATH by
executing this line in your project’s directory:

add2virtualenv .

1👍

From the docs, it seems that base is already in your path – so maybe you want to be using

py.test --ds=settings.settings

0👍

Why are there 3 “t” ‘s in your settings?
it should be settings as opposed to setttings

👤random

0👍

You run the second program without the --ds=base.settings.settings argument and not the first. This is where the error appears to be coming from.

Also when importing code of the same name settings I would be tempted to do something like:

from base.settings import settings as foo
from django.conf import settings as bar

0👍

I had the same issue. pytest -s <project_dir> works fine for me.

👤Andrew

Leave a comment