I'm practising Django now, and I'm stuck. When I run:
>>> from django import template
>>> t = template.Template('hi')
Gives me:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Any idea what causes this? What should I do?
Django is designed to work within a project, which contains a file named settings.py. Rather than just importing it on the command line, you should work through the tutorial, which shows you how to create a project and its apps.
If you just want a quick start, you need to execute this on the command line:
django-admin.py startproject test_project
This will create a directory named test_project with the needed files. Within that directory, you can execute the command:
./manage.py shell
which will get you a Python shell with the settings module properly loaded.
As noted here, you have to do the following:
When you use Django, you have to tell it which settings you're using. Do this by using an environment variable, DJANGO_SETTINGS_MODULE.
The value of DJANGO_SETTINGS_MODULE should be in Python path syntax, e.g. mysite.settings. Note that the settings module should be on the Python import search path.
Related
I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar
Disclaimer: I am new to python and django but have Drupal programming experience. I'm using Windows 7 (same issues on Windows XP)
On python 2.7 and Django-1.3.1, I successfully created a default project
django-admin.py startproject djsite
Now, I need to "bootstrap" djsite.manage as explained here (http://www.pyinstaller.org/wiki/Recipe/DjangoApplication) in a file called bootstrap.py located in djsite's parent directory as follows:
import djsite.manage
djsite.manage.execute_manager(djsite.manage.settings,['manage.py', 'runserver'])
Yet, as soon as the compiler sees:
import djsite.manage
I get this:
"Error: Can't find the file 'settings.py' in the directory containing 'C:\Python27\Lib\site-packages\djsite\manage.pyc'. It appears you've customized things... You'll have to run django-admin.py, passing it your settings module." And, I don't know how to follow the error's advice in this situation.
However, if I instead issue the following in bootstrap.py:
import os, sys
sys.path.append(os.path.abspath('djsite'))
import djsite.manage
djsite.manage.execute_manager(djsite.manage.settings,['manage.py', 'runserver'])
the script works correctly, but it breaks Pyinstaller (I've already asked this question on that software's mailing list (http://groups.google.com/group/pyinstaller/browse_thread/thread/174a72e26c26a44c). Even if I add the path to the djsite in my PATH variable, I get the same error.
So my question here is this: Why does importing the manage.py module fail with this approach and how can I proceed? Thanks!
Try adding this to your bootstrap.py to inform it where your settings file lives:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'djsite.settings'
This will assume that djsite is in your pythonpath. That is, if its located here: /path/to/my/djsite, then this should be in your pythonpath: /path/to/my
Actually the best way to be doing this from the start is to being using virtualenv which will ensure that your environment is correct. I feel like that had to have been part of your tutorial if I remember bootstrap at all. If you are using virtualenv, make sure you remembered to source bin/activate
If that doesn't work, you can try altering the runserver command:
args = ['manage.py', 'runserver', '--settings=/path/to/my/djsite/settings.py']
djsite.manage.execute_manager(djsite.manage.settings, args)
I want to rename a project which I created with:
django-admin.py startproject
But after renaming the folder and all the references inside my project, I still can't get it to start. It says myproject.settings is not in the pythonpath. Since the old project name is neither in the pythonpath i figure that django must keep these names and paths somewhere else. Where does it store this information ?
I know I could just add the path to sys.path while execution, but i want to fix this completely.
If i run:
python manage.py runserver
I get:
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named settings
The name for 'mysite' is already the correct one and corrosponds with the folder name. But still it can't find it.
Any ideas?
PS: I'm running debian.
It doesn't store the project path anywhere. Everything is calculated relative to the path you specified for the settings module.
If you renamed your project folder and it's still trying to load the old settings, it's possible that you still have the old settings file configured somewhere, e.g. in the environment DJANGO_SETTINGS_MODULE is still pointing to oldproject.settings or your WSGI server is still configured to load oldproject.settings. Also check that you don't have any package weirdness in your interpreter's site-packages.
i'm sorry, but the solution was more simple in my case:
one of the apps had the same name i wanted to give to the project. this resulted in the described error message !
sorry for the trouble.
So, I'm fairly new to programming. I have relatively little background in django and don't understand much of its implementation. However I have read through django tutorials and have a basic understanding of the underlying structure.
So currently I am not actually modifying the django project being used by the server (lets call it /main), I think my coworker copied over the project to a new directory called /test. When I try to do anything with manage.py located in the /test directory by running
python2.4 manage.py runserver
I get an error saying
Django Error: Could not import settings 'interfaces.settings'
(Is it on sys.path? Does it have syntax errors?): No module named settings.
I've looked through the settings file and have tried changing main to test, but it hasn't changed anything.
Any guidance as to where I should look for a solution would be great.
Also?
It sounds like there is something hard-coded somewhere which expects the project to be in a directory called "interfaces", rather than "test". Try running manage.py shell --settings=settings, and see if that helps.
Actually, "also", you should not be editing code on the server, even if it's in a different directory to the production deployment. Install Django and the code on your local machine, and edit there.
The manage.py shell --settings=settings worked for me. I figured out from running django-admin.py runserver that settings could not be imported, because environment variable DJANGO_SETTINGS_MODULE was undefined.
Fix: export DJANGO_SETTINGS_MODULE=settings
I ran the command from the root of the project (same directory that had manage.py -- not sure if it matters). Then, running manage.py shell worked without the --settings=settings bit.
Cheers!
Try to undestand which versions of python you have installed.
Type python2 and press tab button in console.
It looks, that you are trying to run version of python, which has not django installed in libs.
Also try to run python2.4 in interactive mode from test folder and import settings.
I've made a separate directory for my django modules. I've added this directory to my PATH variable. I've also created a new PYTHONPATH variable, since it wasn't there. I've added modules to settings.py. But when im trying to run manage.py syncdb for the new module it still says
Error: No module named my_module
Why, oh why?
EDIT: I didn't created the app with manage.py startapp, but manually created the files. Can this cause the problem?
PATH tells your shell where to find executables; it has nothing to do with Python. PYTHONPATH is a list of directories to search for Python modules. It should be edited to include the directory with my_module.
Hard to tell you what the issue is with only that output, however this should solve the problem:
Inside bar.py or bar/__init__.py
import os,sys
sys.path.append(os.path.dirname(__file__))
Now, in other files you can import bar