Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DJANGO_SETTINGS_MODULE is undefined with pycharm

Tags:

django

pycharm

I have the DJANGO_SETTINGS_MODULE problem. I am using Pycharm and under Project Settings -> Django Support everything is set and enabled. Nevertheless I get following error while trying do an import in models.py:

from django.db import models

C:\Python27\python.exe C:/Users/Grimbo/PycharmProjects/Muspy/poll/models.py
Traceback (most recent call last):
  File "C:/Users/Grimbo/PycharmProjects/Mus/poll/models.py", line 1, in <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
    self._setup()
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

print(sys.path):

['C:\\Program Files (x86)\\JetBrains\\PyCharm 2.6.3\\helpers\\pydev', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Users\\Grimbo\\PycharmProjects\\Mus']

Does someone has an idea what's wrong?

like image 306
Grimbo Avatar asked Jan 20 '13 18:01

Grimbo


2 Answers

Check out this link. Specifically, you want to set the Environmental Variable in the configuration. By default, you will see PYTHONBUFFERED = 1, and you will want to add DJANGO_SETTINGS_MODULE = project.settings - obviously replacing project with the actual name of your project.

Another good thing to do is to go to File-> Settings -> Django Support -> and be sure that your Django Root, Settings.py, and Manage.py fields are correct.

like image 65
Dan Hoerst Avatar answered Nov 15 '22 03:11

Dan Hoerst


Following Dan Hoerst's answer worked for me, but it wasn't clear where to set the environment variable. You can find the setting under the menu option:

Run -> Edit Configurations...

Expand the Django Server option on the left hand side and then select your project. There you will find the Environment Variables: setting. Remember to use the dotted path like Muspy.settings and not the file path.

Dan, I'd reply to your answer, but don't have the rep.

like image 43
Fiver Avatar answered Nov 15 '22 04:11

Fiver