pytest (error): Requested setting ROOT_URLCONF, but settings are not configured

Trying to run pytest or coverage run -m pytest to test your Django code?

django.core.exceptions.ImproperlyConfigured: Requested setting 
ROOT_URLCONF, but settings are not configured. You must either 
define the environment variable DJANGO_SETTINGS_MODULE or call 
settings.configure() before accessing settings.

The error is telling us that the code no longer knows how to find our settings module.

Note that we are no longer using the manage.py command to deal with Django code and if you look at this file, one of the first lines of code is this.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', <project>.settings')

Since we are working outside of [manage.py](<http://manage.py>) we will need to set this environment variable ourselves. Run your test by setting the variable first with these two options.

$ export DJANGO_SETTINGS_MODULE=test_settings
$ pytest

# or

$ DJANGO_SETTINGS_MODULE=test_settings pytest