ImportError Environment variable DJANGO SETTINGS MODULE is undefined

0 votes

I'm running the server using python manage.py runserver. And I get this error that I'm not quite understanding how to sort out:

Traceback (most recent call last):
  File "manage.py", line 1, in <module>
    from polls.models import Poll
  File "/Users/davidhaddad/Desktop/mysite/polls/models.py", line 2, in <module>
    from django.db import models
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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.

settings.py was visible earlier and I started getting this message after I added some code to register a few app models with admin.

To give some more info: when I do export DJANGO_SETTINGS_MODULE=settings from inside the project directory and then i do python django-admin.py runserver the server doesn't run

Jun 12, 2020 in Python by kartik
• 37,530 points
434 views

1 answer to this question.

0 votes

Hello @kartik,

You just need to be able to import the value of DJANGO_SETTINGS_MODULE when you're in a location that isn't in your project root. One easy way of doing that is to (1) add the parent of your project folder to PYTHONPATH, (2) change DJANGO_SETTINGS_MODULE to projectfoldername.settings.

Hope this is helpful!!

Thank you!!

answered Jun 12, 2020 by Niroj
• 82,820 points

Related Questions In Python

0 votes
1 answer

How does Python know whether a variable in the class is a method or a variable?

In python objects/variables are wrapped into methods ...READ MORE

answered Sep 18, 2018 in Python by aryya
• 7,440 points
347 views
0 votes
1 answer

ImportError: openpyxl is required for loading excel format files

Forget the PsychoPy complications for the time ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 58,140 points
247 views
0 votes
0 answers

what is the scope of the variable in a conditional statement?

what are global and local variables in ...READ MORE

Apr 17, 2019 in Python by Waseem
• 4,540 points
302 views
0 votes
1 answer

Is it possible to print a string and a variable in a single print statement in Python?

This can be done using simple string ...READ MORE

answered Jul 15, 2019 in Python by Neel
• 3,020 points
288 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,423 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,824 views
0 votes
1 answer
0 votes
1 answer

How to access environment variable values?

Hello @kartik, To check if the key exists ...READ MORE

answered Nov 18, 2020 in Python by Niroj
• 82,820 points
67 views