1

My project has a base.py, a dev.py and a production.py (pretty self explanatory).

On my PC I only keep the dev.py and base.py and on the server I only keep production.py and base.py.

While the WSGI isn't an issue, I do find myself always having to go into manage.py after each deployment to change the os.environ.setdefault() setting.

I wanted to change this:

from __future__ import absolute_import, unicode_literals

import os
import sys

if __name__ == "__main__":
    try:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev") #or project.settings.production

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

to this:

from __future__ import absolute_import, unicode_literals

import os
import sys

if __name__ == "__main__":
    try:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.dev")
    except:
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings.production")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

However this still raises an ImportError. Any ideas why? Or do you know a better way of doing this?

DDiran
  • 533
  • 1
  • 6
  • 23

1 Answers1

2

A good way to control this is using the DJANGO_SETTINGS_MODULE environment variable in the shells where you want to use manage.py. The default manage.py should not have to change:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

As for the ImportError, I'm not sure we have enough information to debug it. But basically you need to make sure that the file is either reachable from the current working directory, or from a directory on PYTHONPATH. For example, if you run manage.py from the project's root, and project/settings/dev.py exists, then the value "project.settings.dev" should work.

janos
  • 120,954
  • 29
  • 226
  • 236