Django documentation: Models; error from line 1 of code - python

In the Django documentation on Models, the first command that I am asked to run and python's response was:
>>> from django.db import models
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from django.db import models
File "C:\Python26\lib\site-packages\django\db\__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "C:\Python26\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "C:\Python26\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.
As you can see from the stack trace, I am running a django server in python 2.6.6. Could anyone offer me a clue to start off on the right foot with this tutorial? Thanks in advance.

Are you running these commands from ./manage.py shell? You cannot run django commands from the regular python shell without specifying where your settings.py file is for the project. Django's ./manage.py shell command specifies that for you, making it easier to do django stuff on the command line.
If you don't see a file named manage.py in your current working directory, then that probably means you're not in the directory of your django application, or you haven't started one yet.
Edit: Also, that documentation is meant as an example of what to put in your application's models.py file, not something you should type in on the command line. That doesn't mean it won't work on the command line (if you use manage.py shell) but it isn't what the documentation is suggesting. Check the tutorial if you're unclear on how to start the shell and what files go where.

You're doing it wrong.
$ python manage.py shell

The answer here definitely works for the interactive prompt, which I was using, however I don't think that the intention of the first block of code was intended to actually be run! Immediately following that first code in the models documentation, you are expected to put the next codes into your models.py file created during the previous tutorial... I guess that's why they subtly labeled that section "Quick Example." What a headache!
Futhermore, that paragraph goes on to say "The above Person model would create a database table like..." suggesting that it was never intended to actually be run as-is.

Related

django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured

I'm trying to run a populate script which I put together from the tango_with_django tutorial (https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py) however I'm getting the below traceback and it seems to be related to changes made in Django 1.7? I'd appreciate if someone could explain what I'm doing wrong here.
(test_env) C:\Users\WriteCode\test_env\epl>python populate_clubs.py
Traceback (most recent call last):
File "populate_clubs.py", line 4, in <module>
django.setup()
File "c:\Python27\lib\site-packages\django\__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge
tattr__
self._setup(name)
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set
up
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, b
ut settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
(test_env) C:\Users\WriteCode\test_env\epl>
My populate_clubs.py script
import os
import sys
import django
django.setup()
def add_club(name, nickname, manager, established, stadium, active=True):
c = clubs.objects.get_or_create(name=name, nickname=nickname, manager=manager, established=established, stadium=stadium, active=active)
return c
def populate():
add_club(name = "Aston Villa",
nickname = "Villans",
manager = "Tim Sherwood",
established = "1897",
stadium = "Villa Park"
)
# Print out what was added
for c in clubs.objects.all():
print "The following has been added to clubs:" + c
# Start execution
if __name__ == '__main__':
print "Starting club population script..."
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
from teams.models import clubs
populate()
You can insert os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") before the django.setup() line.
Call to django.setup() should go after setting DJANGO_SETTINGS_MODULE environment variable. Just move it into your __main__ right after os.environ.setdefault().
If you are getting a similar error after initiating your interaction with Django by running python in a terminal, running python manage.py shell in the appropriate directory may solve your issue.
For development and debugging, you may use the standalone python package.
Install with pip
pip install standalone
Use standalone to use Django from the module.
# add the following to the top of the module.
import standalone
standalone.run('mysite.settings') # replace with your settings module.
# your code goes below the configuration
import os
import sys
# ... .. . whole module
Now you may run the module as a Python Django script.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings')
should be above
import django
which is you should call django_settings_module before importing and calling django setup.
Hope this is helpful.
If you're running PyCharm, what worked for me was to invalidate the cache and restart the app.
File => Invalidate Caches / Restart ...
My virtualenv had been recently updated from Python 3.6 or 3.7 to 3.8.
It happened to me when I used django related import statement in a non-django module.
i.e from index.views import view that did raise an error for me.
I met the same problem when setting the environment.
(I tried to add the:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
into the "Starting script window",which is in the Django Console or rewrite the .wsgi file, But all of them were failed)
My final solution: (open the terminal and set the environment manually)
Here are the steps:(for mac)
open the terminal and type vim ~/.bash_profile
add below lines:
PATH=${PATH}:/Users/work/workspace/[virtual_environment]/bin
PYTHONPATH=${PATH}:/Users/work/PycharmProjects/[project_name]
DJANGO_SETTINGS_MODULE=[project_name].settings
type :wq to save and exit terminal
type source ~/.bash_profile to reload it
it will work when you run python manage.py shell this time.
Make sure to activate your venv first by scripts/activate venv then in your populate_user.py right after import os run os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'epl.settings') this should work.

after formating my laptop, my Django projects doesn't work

Before I format my laptop, I cut & paste my Django project (folder that include mysite and myvenv folders) to another partition. It was built on python 2.7 and used virtual environment and Django 1.7.
Now I have python 3.4, and after activating virtual environment and run:
$ python manage.py makemigrations
I get this error:
Traceback (most recent call last):
File "manage.py", line 12, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
So I tried and installed python2.7 but still get the same error.
Is there a way to correct what happend to my project and if not I need your advice for best practise on how can I move Django project without break it?
In my Django project I followed the official tutorial on djangoproject.com
This is my first question on stackoverflow
I revoke my answer. I no longer want to support this site.
My guess would be that your manage.py is not configured properly. The first line must define the Python exec to run the script, and not another location like /user/bin/python. If it is set to /user/bin/python you will almost certainly have certain packages missing. Ensure that the path you specify is to your virtualenv python location.

Django: manage.py ImportError because environment variable is undefined

I've just cloned a Django project using Mercurial to a Windows machine that I have set up Python 2.7 on. When I try to run manage.py (with or without a command), I get the following error:
Traceback (most recent call last):
File "C:\Users\jes.000\Documents\project\manage.py", line 30, in <module>
import django.core.servers.basehttp
File "C:\Python27\lib\site-packages\django\core\servers\basehttp.py", line 26, in <module>
from django.views import static
File "C:\Python27\lib\site-packages\django\views\static.py", line 95, in <module>
template_translatable = ugettext_noop(u"Index of %(directory)s")
File "C:\Python27\lib\site-packages\django\utils\translation\__init__.py", line 75, in gettext_noop
return _trans.gettext_noop(message)
File "C:\Python27\lib\site-packages\django\utils\translation\__init__.py", line 48, in __getattr__
if settings.USE_I18N:
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.
I understand that manage.py is supposed to set that environment variable. I also understand that I might be getting this error because the project is not in the python path... but adding it manually seems like it shouldn't be necessary. Isn't manage.py supposed to resolve this issue as well?
Edit: I've just tested and found that it does the exact same thing under Linux.
Edit: Here's a useful discovery: I only get this error if I install Django via PIP, it works fine if I install the python-django package on Ubuntu. So there's something that the debian package is doing that PIP isn't. Maybe the debian package sets some environment variables? I need to figure that out, since I'm trying to develop on Windows.
Edit: I think I've found the problem, but not a solution. Working on an Ubuntu machine, when I install Django via PIP (pip install django), this project doesn't work with the error I gave. If I create a new project (django-admin startproject testproject) and try that it works, and it consists of manage.py in the created folder, with another folder that contains everything else. Like this:
+-testproject
+-manage.py
+-testproject
+-__init__.py
+-etc.
When I install the Debian package (apt-get install python-django) the project I'm trying to work on works, and if I create a test project (django-admin startproject testproject) it has a folder structure like this:
+-testproject
+-manage.py
+-__init__.py
+-etc.
Notice the difference: the PIP Django package seems to put the manage.py outside of the python module that is the app, while the Debian package puts manage.py inside the module. I assume that this is due to the two packages being of a different version of Django, in between which they changed the structure, because the two packages doing this differently doesn't make any sense.
So my problem is that the project I'm trying to develop was originally generated using the version that's been Debian packaged, while on my Windows machine I'm trying to use the version of Django from pip.
The trouble is that I'm not certain how to fix the problem. Naively moving the manage.py up a directory so it's above the project folder doesn't work, because this 'debian-package version' manage.py tries to just import settings, which fails if it's not in the same folder as settings.py. But somewhere else in Django, something seems to be expecting the project itself to be at //settings.py (as would be the case if the pip version of django-admin was used) rather than at /settings.py (as is the case if the debian-package version is used).
If I look it up, the version in the Ubuntu repositories is 1.3.1, while the version in PyPI is 1.4. Look at the release notes for 1.4, and hey:
An updated default project layout and manage.py that removes the
“magic” from prior versions. And for those who don’t like the new
layout, you can use custom project and app templates instead!
But... shouldn't projects generated with the 1.3 layout still work under 1.4..?
I'm not sure if this has changed in more recent version of django, but the default manage.py simply tries to import the settings.py file and start the manager. You would be expected to put the project in your PYTHONPATH, or to put the settings.py into your DJANGO_SETTINGS_MODULE variable, by default.
You can modify the manage.py to be a bit smarter with this snippet:
manage.py
#!/usr/bin/env python
from django.core.management import execute_manager
...
import settings
import os
project = os.path.basename(os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project
if __name__ == "__main__":
execute_manager(settings)
Now you can run manage.py from any location, since it will always explicitly set up the path to the settings.py file.
You may add the following code to the file manage.py ,like this:
from django.conf import settings
settings.configure()
This is indeed due to the difference in default layouts between 1.3 and 1.4. Normally Django 1.3 projects will work under 1.4, so something else is going on here that I can't quite figure out. I have established that the problem isn't platform-dependent.
I couldn't figure out the exact problem but I did figure out a fix. That was:
Create a new project under Django 1.4 and poach its manage.py file. After changing the project name, this file works as a manage.py. I named it 'manage14.py' so that the original manage.py would be left for my colleagues using Django 1.3.
Add the directory above the one that the project was in to the global PYTHONPATH. This is not an ideal solution, but it allowed an unmodified Django 1.4 manage.py to load the project without having to convert the project to Django 1.4's organization.
As soon a practical, I plan to convert the project to the Django 1.4 format since it seems to be better anyway, particularly from the perspective of avoiding this kind of problem. Unfortunately I'll have to wait until the Ubuntu repo's get 1.4 so that my colleagues will be up to the same version I am on Windows using PyPI.

Running Django custom manage.py task on Heroku - Importing Issues

I'm trying to run a custom django command as a scheduled task on Heroku. I am able to execute the custom command locally via: python manage.py send_daily_email. (note: I do NOT have any problems with the custom management command itself)
However, Heroku is giving me the following exception when trying to "Run" the task through Heroku Scheduler addon:
Traceback (most recent call last):
File "bin/send_daily_visit_email.py", line 2, in <module>
from django.conf import settings
ImportError: No module named django.conf
I placed a python script in /bin/send_daily_email.py, and it is the following:
#! /usr/bin/python
from django.conf import settings
settings.configure()
from django.core import management
management.call_command('send_daily_email') #delegates off to custom command
Within Heroku, however, I am able to run heroku run bin/python - launch the python shell - and successfully import settings from django.conf
I am pretty sure it has something to do with my PYTHON_PATH or visibility to Django's SETTINGS_MODULE, but I'm unsure how to resolve the issue. Could someone point me in the right direction? Is there an easier way to accomplish what I'm trying to do here?
Thank you so much for your tips and advice in advance! New to Heroku! :)
EDIT:
Per Nix's comment, I made some adjustments, and did discover that specifying my exact python path, I did get past the Django setup.
I now receive:
File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 155, in call_command
raise CommandError("Unknown command: %r" % name)
django.core.management.base.CommandError: Unknown command: 'send_daily_email'
Although, I can see 'send_daily_email' when I run ``heroku run bin/python app/manage.py```.
I'll keep an update if I come across the answer.
You are probably using a different interpreter.
Check to make sure shell python is the same as the one you reference in your script /usr/bin/python . It could be that there is a different one in your path, which would explain why it works when you run python manage.py but not your shell scrip which you explicitly reference /usr/bin/python.
Typing which python will tell you what interpreter is being found on your path.
In addition, this can also be resolved by adding your home directory to your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).
Add it via the heroku config command:
$ heroku config:add PYTHONPATH=/app
That should do it! For more details: http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/

Why am I getting this error in Django?

I have a script that imports a models.py from an app, but it will not import!
I don't believe I am supposed to manually create an "export DJANGO..." environment variable...I'm doing something else wrong.
Traceback (most recent call last):
File "parse.py", line 8, in ?
from butterfly.flower.models import Channel, Item
File "/home/user/shri/butterfly/flower/models.py", line 1, in ?
from django.db import models
File "/usr/lib/python2.4/site-packages/django/db/__init__.py", line 10, in ?
if not settings.DATABASE_ENGINE:
File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 269, in __getattr__
self._setup()
File "/usr/lib/python2.4/site-packages/django/conf/__init__.py", line 38, 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.
You need to properly import your settings file. If you don't import the django environment variables first, your import will fail as you've noticed. You need to code something like:
import os
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"
#do your stuff
I don't believe I am supposed to
manually create an "export DJANGO..."
environment variable...
Manually or otherwise, you are supposed to ensure that variable is in the environment before you import a Django models file -- not sure what the causes are for your disbelief, but, whatever they may be, that disbelief is ill-founded.
from command line ran "python manage.py shell" should include the right settings for you and give you the python prompt.
One alternative to messing about with the environment is to just write your script as a Django custom management command, which takes care of setting up the environment for you (along with other things like option parsing, etc).
I had this similar error, and I realized that I forgot to create the file "init.py" in to the app folder:
myapp:
manage.py
myapp:
__init__.py
settings.py
....
From command line, please execute this command. This may help you:
export DJANGO_SETTINGS_MODULE=projectname.settings

Categories

Resources