I receive the following error when trying to run a shell command using Python Tool for Visual Studio. I have added the database to the settings file, and have been able to run the django app without errors, but when I try to add data using the shell, it throws this error:
>>> from ProjectTrackerServer.projects.models import Project
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\djangoapps\ProjectTrackerServer\ProjectTrackerServer\projects\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.
The error states the issue. It occurred because you forgot to supply the project setting DJANGO_SETTINGS_MODULE. You can fix it by supplying it. You need to set the DJANGO_SETTINGS_MODULE environment variable.
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.
https://docs.djangoproject.com/en/dev/topics/settings/
Related
Using Anaconda 2.5.0 (python 2.7) on a relatively clean Ubuntu 15.04 desktop system, I have the following unusual behavior:
If I package my application with cx_Freeze 4.3.4 and add the bin directory to the PYTHONPATH environment variable:
export PYTHONPATH=~/frozen-app/bin
Then, when I mistype a bash command:
sl
Instead of the expected:
The program 'sl' is currently not installed. You can install it by typing:
sudo apt-get install sl
I get a python stack trace:
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 23, in <module>
import gettext
File "/usr/lib/python3.4/gettext.py", line 49, in <module>
import locale, copy, io, os, re, struct, sys
File "/usr/lib/python3.4/locale.py", line 18, in <module>
import collections
File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
import re, traceback
File "/usr/lib/python3.4/traceback.py", line 3, in <module>
import linecache
File "/usr/lib/python3.4/linecache.py", line 10, in <module>
import tokenize
File "/usr/lib/python3.4/tokenize.py", line 29, in <module>
import collections
File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)
Original exception was:
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 23, in <module>
import gettext
File "/usr/lib/python3.4/gettext.py", line 49, in <module>
import locale, copy, io, os, re, struct, sys
File "/usr/lib/python3.4/locale.py", line 18, in <module>
import collections
File "/usr/lib/python3.4/collections/__init__.py", line 11, in <module>
from operator import itemgetter as _itemgetter, eq as _eq
ImportError: dynamic module does not define init function (PyInit_operator)
It seems that the "command-not-found" command gets confused by my PYTHONPATH environment, but I can't figure out exactly why. I don't think any of the mentioned modules are defined in my application (I don't define an "re" module in my app for example).
If I run "strace sl", then I just get "can't stat sl". Is there another way to figure out exactly which file in my application is causing this?
Using the suggestion by Charles Duffy above, and looking more closely at the python stack trace, I was able to reproduce the error with:
python3.4 -m operator
Just this replicates the stack trace. Since my PYTHONPATH points to a bin directory from from an Anaconda2.5 (python 2.7), it seems that the python3.4 tries to load operator.so from the the anaconda distribution which has been frozen in my bin directory. This particular import fails. Deleting operator.so from my bin fixes this issue. It seems that using the system python3.4 (which is used by command-not-found) and the Anaconda 2.7 combined with explicitly setting PYTHONPATH can cause trouble.
I've created a completely empty PyDev project (NOT "PyDev Django", just "PyDev"), with this single file in it:
import unittest
class Test(unittest.TestCase):
def testName(self):
print "hello world"
And when I either right-click on the file and select "Run As"->"Python unit test", or press CTRL+F9 and click on "testName", I get this error:
Traceback (most recent call last):
File "C:\Program Files\Brainwy\LiClipse 1.2.0\plugins\org.python.pydev_3.8.0.201409251617\pysrc\runfiles.py", line 201, in <module>
main()
File "C:\Program Files\Brainwy\LiClipse 1.2.0\plugins\org.python.pydev_3.8.0.201409251617\pysrc\runfiles.py", line 26, in main
import pydev_runfiles
File "C:\Program Files\Brainwy\LiClipse 1.2.0\plugins\org.python.pydev_3.8.0.201409251617\pysrc\pydev_runfiles.py", line 5, 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 __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
If I put this in the file:
if __name__ == "__main__":
unittest.main()
and execute it using "Run As"->"Python Run", it works fine, but of course it's inconvenient in case there are many tests in the file.
How do I get PyDev to execute the unit tests without Django?
Solved by updating LiClipse from 1.2.0 to 1.4.0 :)
I am working through the PYcharm tutorial, which sets up a polling site, as my first Python/Django project. I am new to this so if I am asking silly questions I apologize in advance. In the admin.py file the following import is not working:
from MyDjangoApp.polls.models import Poll, Choice
I have specified the environment variable as I found in other posts to
DJANGO_SETTINGS_MODULE MyDjangoApp.settings
with MyDjangoApp being my project name and MyDjangoApp.settings as the value in the Edit Configurations tab.
I also tried to follow the lightbulb suggestion to install package models but when I do that I get the following error:
Error occurred when installing packages.
The following command was executed:
packaging_tool.py install --build-dir /tmp/pycharm packaging1986982253689628475.tmp
models
The error output of the command:
Downloading/unpacking models
Downloading models-0.9.3.tar.gz
Running setup.py egg_info for package models
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/tmp/pycharm-packaging1986982253689628475.tmp/models/setup.py", line 25, in
<module>
import models
File "models/__init__.py", line 24, in <module>
from props import *
File "models/props.py", line 23, in <module>
import yaml
ImportError: No module named yaml
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 14, in <module>
File "/tmp/pycharm-packaging1986982253689628475.tmp/models/setup.py", line 25, in
<module>
import models
File "models/__init__.py", line 24, in <module>
from props import *
File "models/props.py", line 23, in <module>
import yaml
ImportError: No module named yaml
----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pycharm
-packaging1986982253689628475.tmp/models
Storing complete log in /home/andrew/.pip/pip.log
I am not sure how to proceed. I have seen a decent amount of suggestions but they either don't work or I'm not doing it correctly. If anyone has any suggestions I would greatly appreciate it. I have been banging my head against this wall most of the day today and I would really like to be able to move forward. Thanks is advance for anyone willing to help out a newbie.
In polls/admin.py do from models import Poll, Choice - this is presuming you have a models.py file next to admin.py.
I'd like to put a flickr feed on my homepage using django-syncr; I followed the instructions on the homepage (http://code.google.com/p/django-syncr/), and the installation process was pretty smooth. Steps 1-3 work just fine, but Step 4 is where I run into problems.
When I try to run the commands shown:
from syncr.app.flickr import FlickrSyncr
I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django_syncr-0.50-py2.7.egg/sync/app/flickr.py", line 11, in <module>
from syncr.flickr.models import *
File "/usr/local/lib/python2.7/dist-packages/django_syncr-0.50-py2.7.egg/syncr/flickr/models.py", line 1, in <module>
from django.db import models
File "/usr/lib/python2.7/dist-packages/django/db/__init__.py", line 14, in <module>
if not settings.DATABASES:
File "/usr/lib/python2.7/dist-packages/django/utils/functional.py", line 276, in __getattr__self._setup()
File "/usr/lib/python2.7/dist-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.
And I'm not really sure what to do. These files are run in my django project folder, but including the settings file (via import settings) doesn't seem to fix this. What files should be included in order to import the right variables?
thanks!
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?
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.
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.