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
Related
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"
I´m making a web app with django, but when I run the application, I´m getting this error:
I have uploaded my client_secrets.json file in the project path and I´m sure I have no typos
Settings.py
GOOGLE_OAUTH2_CLIENT_SECRETS_JSON = 'client_secrets.json'
WSGI.py
# This file contains the WSGI configuration required to serve up your
# web application at http://bohosul02.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Django project
import os
import sys
# add your project directory to the sys.path
project_home = '/home/bohosul02/misitio'
if project_home not in sys.path:
sys.path.insert(0, project_home)
# set environment variable to tell django where your settings.py is
os.environ['DJANGO_SETTINGS_MODULE'] = 'gfglogin.settings'
# serve django via WSGI
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
You're using a relative path name (client_secrets.json) without paying attention to what your working directory is. If you use the full path to the file, then it will be able to find it.
I'm having trouble setting up my django on dreamhost shared hosting using python passenger_wsgi.py and virtual env. When I run passenger_wsgi.py no error is returned and the shell prints my project path. My website shows a 500 International server error. I have cleared my cache so there is no cache error. How do I set this up properly?
django 1.9
python 2.7
apache
My site structure is:
/home/myuser/mydomain.com/
env/
myApp/
passenger_wsgi.py
public/
passenger_wsgi.py
import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
project_location = cwd + '/myApp'
print (project_location)
sys.path.insert(0, project_location)
#Switch to new python
if sys.version < "2.7.3": os.execl("/home/myuser/mydomain.com/env/bin/python",
"python2.7.3", *sys.argv)
sys.path.insert(0,'/home/myuser/mydomain.com/env/bin')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = "myApp.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I had issues with Dreamhost specifically when upgrading from Django 1.6 to 1.8. One of the issues was with the WSGIHandler(). I can't say this is your problem specifically, but you can try setting application like this:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Finally - make sure you restart passenger. There are docs here about how to do it: http://wiki.dreamhost.com/Passenger
From that page:
Whenever the code or configuration files for your application are modified, you must create or update the modification date of the file "tmp/restart.txt" in the application's root directory tree in order to trigger Passenger to reinitialize the application. Passenger caches many resources so changes are not recognized unless the modification date of "tmp/restart.txt" is changed.
The most common method to make this change is to run "touch tmp/restart.txt" via SSH. (Ruby on Rails automatically creates a directory named "tmp". If you are creating non-RoR application, you may need to create the "tmp" directory manually.
I have a project called "myplanet" and my manage.py file looks like:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myplanet.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
I know that I have to set a system variable DJANGO_SETTINGS_MODULE:myplanet.settings and also set the PYTHONPATH to my settings.py file. I was wondering what I should do in the case of another project that is called gMaps ? I tried to do the same but it does not simply work. My OS is Windows 7 x64
You can set up two independent virtual environments for each project. It's best practice I think. In that case you can even install different versions of python packages for each project as it required.
You can read about using virtualenv and it's wrapper here:
http://virtualenvwrapper.readthedocs.org/en/latest/index.html
You don't need to set PYTHONPATH variable environment to your django project for running that, when your settings.py is inside a package or is a module that is importable with manage.py.
The PYTHONPATH is a list of directories Python goes through to search for modules and files.
If you need to add a path of one library or your root path of project to it, you can do that in your code.
For example in manage.py with:
import sys
sys.path.append("/home/my/project")
Or:
import os
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(BASE_PATH + '/src/folder/of/my/project/')
And each django project has own manage.py file that set DJANGO_SETTINGS_MODULE enivoronment itself, and you don't need to set that.
I think if your manage.py have this line:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myplanet.settings")
Then, you just need to have this structure for your project:
myplanet # It's a project folder
myplanet # It's a package
__init__.py
settings.py
manage.py
And you can run your project with python manage.py runserver without set PYTHONPATH environment variable.
I created a sub-directory of my Django project called bin where I want to put all command-line run Python scripts. Some of these scripts need to import my Django project settings.py file that is in a parent directory of bin.
How can I import the settings.py file from a sub-directory of the project?
The code that I use in my command-line script to set into the "Django context" of the project is:
from django.core.management import setup_environ
import settings
setup_environ(settings)
This works fine if the script is in the root directory of my project.
I tried the following two hacks to import the settings.py file and then setup the project:
import os
os.chdir("..")
import sys
sys.path = [str(sys.path[0]) + "/../"] + sys.path
The cruel hack can import settings.py, but then I get the error:
project_module = __import__(project_name, {}, {}, [''])
ValueError: Empty module name
I think your approach may be over-complicating something that Django 1.x provides for you. As long as your project is in your python path, you can set the environment variable DJANGO_SETTINGS_MODULE at the top of your script like so:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
In your command line script where you need to read your settings, simply import the settings module from 'django.conf' as you would do in your application code:
from django.conf import settings
And presto, you have your settings and a Django-enabled environment for your script.
I personally prefer to set my DJANGO_SETTINGS_MODULE using '/usr/bin/env' in a bash script called 'proj_env' so I don't have to repeat it
#!/bin/bash
proj_env="DJANGO_SETTINGS_MODULE=myproject.settings"
/usr/bin/env $proj_env ${*}
With this, now I can run any python script with my Django application in context:
proj_env python -m 'myproject.bin.myscript'
If you use virtualenv, this also gives you a good place to source the activate script.
etc. etc.
This is going one level up from your question, but probably the best solution here is to implement your scripts as custom manage.py (django-admin.py) commands. This gives you all of Django's functionality (including settings) for free with no ugly path-hacking, as well as command-line niceties like options parsing. I've never seen a good reason to write Django-related command-line scripts any other way.
Add the parent directory to your path:
import sys
sys.path.append('../')
import settings
Update from comments:
Don't forget the __init__.py file in
the directory that has your
settings.py – S.Lott
Let's say your project directory is /opt/someProject/`
This has files like:
manage.py
Now you have you may have your sub directory anywhere, does not matter.
Eg. subdirectory could be like:
/opt/someproject/dir1/dir2
Now for you to import your project settings inside /opt/someProject/dir1/dir2
You need to set your PYTHONPATH variable
export PYTHONPATH=/opt/someproject/
Now to import modules from bin
from someproject import bin