Import Django settings from external script - python

I have a python script within my Django project designed to run seperate from the Django app. I want to use the settings.py on my Django App how can I do that.
When I try to import
from django.conf import settings
i get
ImportError: No module named DjangoTastypie.settings
My project Structure
I am running using eclipse-> Run as python

Based on #Sardorbek Imomaliev, you should also make your DjangoTastypie in your PYTHONPATH, you can do this in your script.
import os
import sys
import django
from django.conf import settings
sys.path.append("path/to/DjangoTastypie") # path to the parent dir of DjangoTastypie
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()

Read https://docs.djangoproject.com/en/1.9/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
So you basically will need to put this at the beginning of your script
import os
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()

Related

Django Installed app and ImproperlyConfigured Error: django.core.exceptions.ImproperlyConfigured

django.core.exceptions.ImproperlyConfigured: 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.
Actually, I am maiking a RabbitMQ consumer in Dango Project in the same directory as the manage.py file, i am facing the above error.
I also added
import os
import pika, sys, os, json
from matcher.views import GenerateOutput
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
These lines i got are from the stackoverflow related questions
So, you are making 2 mistakes.
First: In the Code, you have to write those os.enoviron line above to the code line where you are importing the views.py function GenerateOuput
Second: You have to import the django in the code and write the code line below
django.setup()
So the Complete Code will be :
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
django.setup()
import pika, sys, os, json
from matcher.views import GenerateOuput
This should work.
os.environ.setdefault() only takes action when the environment variable isn't already set. That is, if DJANGO_SETTINGS_MODULE is already set in your environment, and has a different value than what you need, this would explain your issue. Try using
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"

Import module that depends on module with same name

My project is based on django and in one app I want to import another module. I installed the module with pip and I'm importing it:
from django.conf import settings
from problematic_package.app.templatetags import test
The issue is that problematic_package is also django based and in test.py they have:
from django.conf import settings
...
Here python will think settings is actually the object from my django project, instead of the imported one.
I can't change the project installed with pip. What other solutions do I have?

Django's "check" cannot import settings

Since the project is evolving I would like to start executing the system check framework of Django on dev environment. The technology stack is Ubuntu, PostgreSQL, Django1.9 + UWSGI. But...
django-admin check
outputs the following error:
ImportError: No module named my_project.settings
The wsgi.py file contains:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('~/virtenv/my_site')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
The env variable DJANGO_SETTINGS_MODULE echoes my_site.settings
It is important to mention that the settings.py file is in virtenv/my_site/my_site/ . Please also note that the entire web application is running fine, I am also using the features of manage.py. Its just the django-admin check that is getting on my nerves.
you seem to append to the sys path in the wsgi but the admin check won't be using that.
Make sure you add the project path to the sys path for the environment you're using

How to create a scratch file in pycharm for Django

I can create a scratch file from menu "Tools" -> "New Scratch File...".
But Django settings and environment variables are not imported automatically. Is there a way to do that now or this feature is just meant for pure python code testing.
I want to be able to
from website.models import *
but I get this error
File "/Users/user1/.virtualenvs/test-env/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
% (desc, ENVIRONMENT_VARIABLE))
Thanks
You should not need to add anything to sys.path. This works for Python 3.7+, Django 2.1+ and PyCharm 2019+:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "APPNAMEHERE.settings")
import django
django.setup()
Add this at the top of your scratch-file to have access to a Django app. Futher info on ImproperlyConfigured-error.
Thanks to #Leistungsabfall, here is a working solution :-)
import os, sys
sys.path.append(os.path.join('/Users/user1/gitroot/website1/web', 'myproject'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.conf import settings
from website.models import *

Error when trying import Bottle-powered web app on Apache/mod_wsgi

I am created an api for OpenERP using bottle and when i try to configure wsgi with apache.
While importing in apache server log it shows
ImportError: No module named api
I checked for current directory it prints cwd and the import file is in same directory still it shows error
Here i uploaded my code for wsgi
import os
os.chdir(os.path.dirname(__file__))
import bottle
print os.getcwd()
import api as application
application = bottle.defaut_app()
What is your sys.path?
It must include the directory of your .py file, not the file name itself. E.g.,
sys.path.append('/var/www/api')
# and then, to see its new value:
print sys.path.append
Also note how I printed the value of sys.path there; the way you did it in one of your comments (printing the return value from append) is incorrect.
I think i had a similar problem and I ended up doing this in my wsgi:
import sys
import os
import bottle
sys.path.append('%path to module%')
import %modulename%
application = bottle.default_app()
In your py you have to import:
from bottle import default_app
for this to work.

Categories