sniffer can't find DJANGO_SETTINGS_MODULE

自作多情 提交于 2019-12-23 12:15:44

问题


I'm trying to automate the test rerun after a change while developing. After searching around a little sniffer seemed fine. But if I run it my tests fail with this error:

ERROR: Failure: ImportError (Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.)

if I run them manually they pass. Do you have a clue why sniffer won't work?


回答1:


Something like the following as your scent.py should work:

from subprocess import call
from sniffer.api import runnable

@runnable
def execute_tests(*args):
    fn = [ 'python', 'manage.py', 'test' ]
    fn += args[1:]
    return call(fn) == 0

Which you can then call as sniffer -x appName.




回答2:


You can get sniffer to read your settings by creating a scent.py file in the same directory as manage.py.

Here's what mine looks like:

import os
os.environ["DJANGO_SETTINGS_MODULE"] = 'myapp.settings'

Which will get you as far as sniffer reading your settings, but then you'll run into other problems — basically, sniffer just runs your tests using nose, which isn't the same thing that the manage.py test does when django-nose is installed.

Anybody know what else needs to be in scent.py for snigger to with with Django?




回答3:


Trying to guess where the problem may reside: it seems you need to explicitly set the position of your settings.py file.

if you're running your test from a subprocess' call you can use the following command:

call(["django-admin.py", "test --settings=your_project.settings"])

otherwise you can set environment variables with the following command:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'

(change your_project with the name of your django project)

if you're running a command like "./manage.py tests" you can add the former lines at the beginning of manage.py (there are other ways but I need to see the code to provide a more precise solution)



来源:https://stackoverflow.com/questions/13975014/sniffer-cant-find-django-settings-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!