I'm trying to start a project in Django, but I've come up against a wall right at the start. Whenever I run django-admin with no commands, I get this notice after the help message:
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
And whenever I try to run it or manage.py with a command such as runserver, I get:
python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 62, in execute
super(Command, self).execute(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 73, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/castro/Code/django_tests/tutorial/lib/python3.6/site-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I don't know how to define this DJANGO_SETTINGS_MODULE nor where to call settings.configure(). I've tried what some other answers here suggested, but either it's a different problem, or I'm missing something that should be quite obvious.
Near the top of your manage.py file, you should see this line, where project is the name of your project which contains the settings file.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
If the project does not exist, or the path is wrong, python manage.py will fail.
The file tree should look something like this
.
├── manage.py
├── project
│ ├── settings.py
For context, a generated manage.py contains the following lines
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# ... other code ...
You may use the suggested solution of an env variable in the exception - DJANGO_SETTINGS_MODULE.
export DJANGO_SETTINGS_MODULE="project.settings"
Related
I've been learning Python for a few months and a couple days ago i decided to learn one of Web frameworks. I found some tutorials on Django and it was looking really cool until i tried to use "runserver" command. As you can see i used the basic line "python -m django runserver manage.py" and got a lot of calls. I've just installed Django and i wasn't changing anything in it's scripts. I only started by creating a new project that worked perfectly with "python -m django startproject djangoLearn". Do you have any thoughts on how can i fix this problem? This problem prevents me from further learning beacuse i can't see my own project :((
Thanks in advance. Hope y'all have a great day.
Calls that i get while trying to run server:
D:\Projects\djangoLearn>python -m django runserver manage.py
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\__main__.py", line 9, in <module>
management.execute_from_command_line()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 61, in execute
super().execute(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\base.py", line 371, in execute
output = self.handle(*args, **options)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\core\management\commands\runserver.py", line 68, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
File "C:\Users\norml\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I hope your virtualenv is activated ??
use this to create project django-admin startproject projectname . take note of the period it works without the period though.
use python manage.py startapp appname to create an app
don't forget to register the app in INSTALLED_APP
i think the code you want to use is python manage.py runserver
but if you use python3 use python3 manage.py runserver just use whatever you use to start your python before manage.py runserver
you should first activate your virtual environment. with a command like this in linux:
source "path-to-activate-file"
and command like this in windows:
\pathto\env\Scripts\activate
I keep getting an Improperly Configured Error when I try to run my server
Error message:
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I created a new django project,
I have django installed in my virtual environment,
I did not forget to activate the virtual environment,
And I triple checked my python path,
Here is the full traceback:
(venv3) C:\Users\user\Desktop\trialproject>django-admin.py runserver
Traceback (most recent call last):
File "C:\Users\user\Desktop\trialproject\venv3\Scripts\django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
super().execute(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\base.py", line 369, in execute
output = self.handle(*args, **options)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\core\management\commands\runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "c:\users\user\desktop\trialproject\venv3\lib\site-packages\django\conf\__init__.py", line 57, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Someone please help me figure out the problem. Thanks
that usually happen when you want to run your server with wrong virtual environment.
make sure you have the right virtual environment and try this :
python manage.py runserver
UPDATE : check if this is useful for you
(Django version 1.9, Python version 3.4.3, using virtual env made with mkvirtualenv --python=/usr/bin/python3.4 django19)
I've been following the Django tutorial very closely, without any problems up til part 5:
https://docs.djangoproject.com/en/1.9/intro/tutorial05/
although, strictly speaking, the problem isn't to do with part 5, but bear with me. I'm following the tutorial in the pythonanywhere framework, because eventually I would like to host a website there. Therefore, I am also following the 'Following the Django tutorial' offered by pythonanywhere:
https://help.pythonanywhere.com/pages/FollowingTheDjangoTutorial
although my problem isn't anticipated in the advice for part 5 in the tutorial. Now the problem- at the point in the Django tutorial which reads:
To check if the bug really exists, using the Admin create a question whose date lies in the future and check the method using the shell
Following the link to the shell, I try (as instructed in the link) to run:
django-admin shell --plain
However I get the following error message
(django19)19:11 ~/mysite $ django-admin shell --plain
Traceback (most recent call last):
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 391, in execute saved_locale = translation.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 176, in get_language
return _trans.get_language()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 57, in __getattr__
if settings.USE_I18N:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
During handling of the above exception, another exception occurred:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/base.py", line 360, in run_from_argv
connections.close_all()
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 230, in close_all
for alias in self:
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 224, in __iter__
return iter(self.databases)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/db/utils.py", line 157, in databases
self._databases = settings.DATABASES
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 55, in __getattr__
self._setup(name)
File "/home/me/.virtualenvs/django19/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error messages recommends setting DATABASES (which is set in mysite/settings.py), by defining the variable DJANGO_SETTINGS_MODULE. So, that's what I did:
export DJANGO_SETTINGS_MODULE="${PWD}/mysite/settings.py"
There are also the required __init__.py files in the mysite and mysite/mysite directories. However now when I run the django-admin command I get:
ImportError: No module named 'mysite/settings'
I can't find anymore documentation or help hereafter. What am I missing? Do I need to set something special somewhere else? Am I setting DJANGO_SETTINGS_MODULE correctly? The ImportError makes it sound like I should set DJANGO_SETTINGS_MODULE to mysite.settings or something similar, but this doesn't work either.
Once you have created your project, use manage.py instead of django-admin to run commands:
python manage.py shell
The manage.py command takes care of the settings for you. If you use django-admin, you have to set DJANGO_SETTINGS_MODULE manually. The DJANGO_SETTINGS_MODULE should be the Python module, e.g. mysite.settings, not a file like /path/to/mysite/settings.py.
Be sure to have your project in the python import search path. Try this
>>import sys
>>sys.path
if the path/directory for your project is not listed, then you need to do
>>sys.path.append('/path/to/my/django/project')
Then assign DJANGO_SETTINGS_MODULE like so:
$export DJANGO_SETTINGS_MODULE=mysite.settings
See Designating Django settings
I'm trying to deploy my Django project using Heroku, but now stuck with the empty SECRET_KEY problem when I run the heroku run python manage.py syncdb command:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 190, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/init.py", line 40, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 4, in
from django.contrib.auth import get_user_model
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/init.py", line 7, in
from django.middleware.csrf import rotate_token
File "/app/.heroku/python/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in
from django.utils.cache import patch_vary_headers
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/cache.py", line 26, in
from django.core.cache import caches
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/cache/init.py", line 34, in
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 48, in getattr
self._setup(name)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/init.py", line 113, in init
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
However, running python manage.py syncdb is totally fine. In my settings.py file, I even wrote the SECRET_EKY explicitly as:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '*$_99!t5u&#w&9boeq()=obq#rjdsfasdfa#8hp_5s%h-mh(hk'`
I still got the same problem. I wondered if the environment variable DJANGO_SETTINGS_MODULE was missing, but running heroku config gave me (I replaced the information with xxx):
DATABASE_URL: xxx
DJANGO_SETTINGS_MODULE: xxx
SECRET_KEY: xxx
It seems that the relevant environment variables do exist. I really hope you could give me some suggestions. Thank you very much in advance.
I faced this issues a while ago. The following command resolved the issue for me
command: heroku config:set SECRET_KEY="your_secret_key"
Might be a stupid question, but are you running heroku run python manage.py syncdb?
If so, I am very confident, that your settings module is not loaded.
This could potentially have one of the following reasons:
Your settings module is not where Django expects it
(but you are saying manage.py can find it)
You have another config, that overshadows your config settings
(for example a local.py settings module for development settings)
Did you do any modification to manage.py or wsgi.py?
I edit this answer if I can think of more.
You can as well go to your app on your heroku dashboard, click on settings, then, scroll downwards; you'll find the button Reveal Config Vars. Add the SECRET_KEY and it's value as instructed. You can as well, add other secret properties required by your application.
I decided to try developing on python with django, while python is new to me right now.
[I'm using Windows 7 64bit]
Then, i installed python 2.7, added C:\Python27\;C:\Python27\Scripts; to PATH variable.
Next, i installed Eclipse Classic (last version) and installed PyDev, and Django.
I created my first project on Eclipse as a Django project but couldn't run it.
I'm trying (Right click on my project)-> Run as -> PyDev: Django, and i have the following console log:
Validating models...
Traceback (most recent call last): File "C:\Users\Matan\workspace\FirstSample\manage.py", line 10, in <module>
execute_from_command_line(sys.argv) File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute() File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__) File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 69, in handle
self.run(*args, **options) File "C:\Python27\Lib\site-packages\django\core\management\commands\runserver.py", line 80, in run
and the list goes on...
So i tried to run the project with the python console, using:
import Django
manage.py runserver
and i have the following syntax error:
File "<stdin>", line 1
manage.py runserver
^ SyntaxError: invalid syntax
What is the problem with running my project ? How can i solve this?
Thanks.
Edit:
After trying to run the project with the following command:
python manage.py runserver on the project directory, i receive the following traceback log: Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <dja ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x000000 0002688748>> Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\core\management\commands\runserver. py", line 91, in inner_run
self.validate(display_num_errors=True) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app) File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin e 23, in get_validation_errors
from django.db import models, connection File "C:\Python27\lib\site-packages\django\db\__init__.py", line 40, in <modul e>
backend = load_backend(connection.settings_dict['ENGINE']) File "C:\Python27\lib\site-packages\django\db\__init__.py", line 34, in
__geta ttr__
return getattr(connections[DEFAULT_DB_ALIAS], item) File "C:\Python27\lib\site-packages\django\db\utils.py", line 92, in
__getitem
__
backend = load_backend(db['ENGINE']) File "C:\Python27\lib\site-packages\django\db\utils.py", line 24, in load_back end
return import_module('.base', backend_name) File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in im port_module
__import__(name) File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 16 , in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No mo dule named MySQLdb
Download the pre-compiled python-mysql package for Windows, install it, then run the command again.
While you are there, you might as well also install setuptools if you haven't already.
Error loading MySQLdb module: No mo dule named MySQLdb
You have configured your settings to you MySQL but you have not installed MySQL-python:
pip install MySQL-python
You must be in the project directory to run the python manage.py runserver command. You also need to (1) set the PYTHONPATH environment variable to include your project directory's parent and (2) set the DJANGO_SETTINGS_MODULE environment variable to yourproject.settings
If you've done it correctly, you should be able to go to an unrelated directory (i.e. not your project directory) and do the following import in a python shell:
c:\temp> python
...
>>> from django.conf import settings
the settings module that is imported should then be the one in your project directory...