DJANGO_SETTINGS_MODULE is undefined with pycharm - django

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.

Related

Django, url configuration reverse question. Gives ImproperConfigured

I tried to use this 2 lines to check if url reverse works...:
from django.urls import reverse
reverse('country-autocomplete')
It suppose to give me:
u'/country-autocomplete/'
I did this earlier and it worked perfectly, since I had couple of problems and did couple of changes in environment(?).
Now I'm trying to run these commands in cmd->python with swthed on environment and it gives me this fault:
>>> from django.urls import reverse
>>> reverse('raildict-autocomplete')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\me\Envs\sc_project\lib\site-packages\django\urls\base.py", line 31, in reverse
resolver = get_resolver(urlconf)
File "C:\Users\me\Envs\sc_project\lib\site-packages\django\urls\resolvers.py", line 69, in get_resolver
urlconf = settings.ROOT_URLCONF
File "C:\Users\me\Envs\sc_project\lib\site-packages\django\conf\__init__.py", line 76, in __getattr__
self._setup(name)
File "C:\Users\me\Envs\sc_project\lib\site-packages\django\conf\__init__.py", line 57, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting ROOT_URLCONF, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
What have I done to break it, and probably the most important how to fix it? Thank you!
def get_absolute_url(self):
return reverse('src:post')
Was having this kind of error it tired this and my error solved
This provides default revers after a operations performed
or you are reversing to path that is having an issue or that doesn't exist.
or use reverse_lazy.
That is kind of dumb...
I fired up: 'python' from cmd
and then:
>>>from django.urls import reverse
>>>reverse('sch-autocomplete')
But, it is not supposed to be done that way and sure it gave me the fault message. Oh! I was so sad! But it is only my mistake, thanks goodness... Environment is good, paths are good and etc. Just have to do it right way:
python manage.py shell
>>>from django.urls import reverse
>>>reverse('sch-autocomplete')
And after it gave me right reverse path. I will leave it here... Because I saw very in deep, complicated answers on: Requested setting ROOT_URLCONF, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
But the problem in my code was on the suface and only because I did wrong coding in cmd. May be it helps to somebody and saves the time...

ModuleNotFoundError: No module named 'django', when I run tests

When I run the tests (in the pycharm environment) I get an error ModuleNotFoundError: No module named 'django'. Django installed (2.2.5)
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/home/m0nte-cr1st0/projects/slack_bot/tests.py", line 4, in <module>
from app.models import *
File "/home/m0nte-cr1st0/projects/slack_bot/app/models.py", line 1, in <module>
from django.db import models
ModuleNotFoundError: No module named 'django'
You probably have not configured your PyCharm Project to use your virtualenv for tests.
In your Run/Debug configurations (the dropdown next to the run button), select "Edit Configurations".
Find your test configuration and check the value for "Python Interpreter". If should be something like /home/m0nte-cr1st0/.virtualenvs/slack_bot/bin/python, but probably it is something like /usr/bin/python3.6
If it is in the list, select /home/m0nte-cr1st0/.virtualenvs/slack_bot/bin/python. Otherwise, you have to add a new interpreter.
To add an interpreter, look at the status bar on the bottom right. It should say "Python 3.6". If you click on it, there should be an option to add an interpreter.

Waitress on Heroku giving error

I am trying to switch from Gunicorn to Waitress on Heroku. In the logs, I keep getting an error from Waitress:
Error: Bad module 'cardisle'
In my procfile, I have:
web: waitress-serve --port=$PORT cardisle.wsgi:application
If I remove the .wsgi extension, I get a different error:
Error: Bad object name 'application'
I have tried changint the object name to wsgifunc as well since it's in the Waitress doc, but no luck.
Any help would be appreciated. I have a wsgi.py file with the following:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardisle.settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Here's an awful fact about waitress: it is hiding info from you.
If you look at the source, "Bad Module" is code for "There was a failure importing your application from the wsgi module."
To see the error, try:
logging into a dyno with heroku run bash
navigating to the directory with wsgi.py in it (with cd)
opening a shell with python
running import wsgi
When I hit this error and did this, I got:
~/proj/proj $ python
Python 2.7.9 (default, Dec 11 2014, 17:18:51)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wsgi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "wsgi.py", line 36, in <module>
application = get_wsgi_application()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/config.py", line 86, in create
module = import_module(entry)
File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named debug_toolbar
Which is a much more helpful error. In my case, I had set DJANGO_SETTINGS_MODULE to 'local' in production, (which did not have the appropriate requirements,) and so the import failed.
The exact nature of your problem will vary, but I'll mention a case that frustrated me when I started out:
If you are running web: waitress-serve --port=$PORT cardisle.wsgi:application, you may need to change your PYTHONPATH environment variable so that PYTHONPATH+cardisle.wsgi is a fully-formed extant path on the machine in question.
I'll open a PR for waitress this evening that tries to bubble up the import error. Best of luck otherwise!
The wsgi.py file should be in the cardisle directory. Waitress is trying to import cardisle.wsgi.
I think I found the answer. If your project is laid out like this (as they are by default):
cardisle/
cardisle/
wsgi.py
app1/
app2/
app3/
Try this Procfile instead:
web: waitress-serve --port=$PORT cardisle.cardisle.wsgi:application
I thought of this because Dave Hall's sample project has a different layout:
projectname/
wsgi.py
apps/
app1/
app2/
app3/
Which has the wsgi.py file at a higher level. You probably followed his tutorial, but as far as I can tell the default Django layout doesn't quite work like that.
Try modifying your WSGI file like this(For Django 1.7):
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I had the same error because before, my Procfile was configured as explained on the Heroku guide:
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
And this was causing an issue for me.
Try to use:
cardisle:wsgi.application
Not carlisle"."wsgi":"application

Error trying to add data to database

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/

Using django-syncr to create flickr feed

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!