How can I correctly set DJANGO_SETTINGS_MODULE for my Django project (I am using virtualenv)?

后端 未结 7 1660
误落风尘
误落风尘 2020-12-07 21:22

I am having some trouble setting the DJANGO_SETTINGS_MODULE for my Django project.

I have a directory at ~/dev/django-project. In this directory I have

7条回答
  •  情歌与酒
    2020-12-07 21:47

    I just encountered the same error, and eventually managed to work out what was going on (the big clue was (Is it on sys.path?) in the ImportError).

    You need add your project directory to PYTHONPATH — this is what the documentation means by

    Note that the settings module should be on the Python import search path.

    To do so, run

    $ export PYTHONPATH=$PYTHONPATH:$PWD
    

    from the ~/dev/django-project directory before you run django-admin.py.

    You can add this command (replacing $PWD with the actual path to your project, i.e. ~/dev/django-project) to your virtualenv's source script. If you choose to advance to virtualenvwrapper at some point (which is designed for this kind of situation), you can add the export PY... line to the auto-generated postactivate hook script.

    mkdjangovirtualenv automates this even further, adding the appropriate entry to the Python path for you, but I have not tested it myself.

提交回复
热议问题