Django deployment, cannot import settings - django

I'm trying to deploy an application with django.
I've put my django_project directory in /home/XXX/websites/YYY.
The web server root is in /srv/http/YYY and it only contains a directory named static and an apache.wsgi file.
The content of apache.wsgi is as follow:
import os, sys
sys.path.append('/home/XXX/websites/YYY')
os.environ['DJANGO_SETTINGS_MODULE'] = 'YYY.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Whenever I reload apache modules and try to load a page, I got an import error :
ImportError: No module named YYY.settings
I don't understand. Since the settings.py file is in /home/XXX/websites/YYY/YYY/settings.py, why this don't work ?
I've manage to make this work by putting the django project in the web server root directory, but that wasn't the place where I wanted to put it.
Thanks for your help.

Related

Error running WSGI application, ModuleNotFoundError: No module named 'mysite'. Also not able to find Static files

I'm trying to deploy my website using pythonanywhere. I've given the correct path in wsgi file which is as follows (see code below). Still I'm getting a ModuleNotFoundError.
Also, it's not able to find Static files, however, they are at correct location.
Wsgi file code:
{
path = '/home/divyanshu964/portfolio/Personal_portfolio/'
if path not in sys.path:
sys.path.insert(0, path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'Personal_portfolio.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
}
Can someone help? Thanks.
Hello #DivyanshuRaghuwanshi to load static files you can set DEBUG=True inside settings.py but it's not a good practice to set DEBUG on in production so another way is to set your DEBUG=False and create STATIC_ROOT and configure it inside project root urls.py and than run python manage.py collectstatic command and than give path of your static root directory in pythonanywhere configuration
Apart from settings directly related to Django, there are some extra steps on PythonAnywhere.
wsgi file: you need to edit the file that is linked on the Web app page, not a file inside your project (check the docs here)
static files: set up static files mappings on the Web app page as well (check the docs here).

Error while deploying Django app on cpanel(shared hosting)

I m new to django. I created a web with dajngo,and successfully deployed it in the server
The python app has been successfully setup and virtual environment has been setup.
but while running the web it gives me "Server Error (500)" I don't know whats the problem.
I think error is in "wsgi.py" file but i'm unable to idenify it.
My wsgi file:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'karan_web.settings')
application = get_wsgi_application()
my "passenger_wsgi.py" file is:
import imp
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
wsgi = imp.load_source('wsgi', 'karan_web/wsgi.py')
application = wsgi.application
can someone help me with it;
Sorry for the late answer, I had figured out the as to this just forgot to post it.
As I stated in my question, the actual problem was in passenger_wsgi.py, the django server starts with the wsgi.py and act as the gate way to the Django server.
So whenever a Django project is uploaded on the hosting server, It creates a passenger_wsgi.py file and one default wsgi.py, address of which is provided in passenger_wsgi.py by default.
So we just need to change that address and provide the address of our own wsgi.py in the project
In my case it was
import os
import sys
from karan_web import wsgi
application = wsgi.application
just edit in passenger_wsgi.py the following code.
from karan_web.wsgi import application
You need to check if your code syntax is correct and running properly. If its still doesn't work try to delete and recreate your database in cpanel and check if you have made all necessary migrations, don't forget to restart your python app. If after all these it still doesn't work check if all your files have the correct file permission(766).

what's the choice should be done with django_wsgi and wsgi

My current version of django is 1.6.3
And i want to deploy django on my centos server with uwsgi and nginx.
There's a tutorial posted by someone who use 1.3's django that says it should create a file named django_wsgi.py inside my django project. I was wondering if this step could be ignored because i already have wsgi.py in my project at beginning. Are django_wsgi.py and wsgi.py the same thing? Just change the name along with the version upgrade.
#django.py
mport os
import sys
sys.path.append("/opt/www")
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
#wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netmag.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The project structure generated by startproject changed since 1.3. Before 1.4 release, you had to manually create a wsgi file. Since 1.4, Django automatically creates wsgi.py file in the project root.
Continue using wsgi.py that Django created for you.
A Django 1.6 already has a default WSGI configuration as you can see in your wsgi.py file, nothing else is needed. The instructions you mention are for legacy Django projects.
See https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ for more information on the Django WSGI config.

Django Unable to Import Settings Module

I'm trying to host a simple Django app on AWS.
I can run it via the public DNS and runserver, but I can't seem to get the environment URL working.
I checked the log and it says "ImportError: Could not import settings 'AppName.settings'"
I've tried adding sys.path.append('/opt/python/current/app/ProjectName/AppName') to the WSGI file, but when I try to print out the sys.paths, it is not listed.
How can I fix this?
Thanks!
I think you need to import till project name like
sys.path.append('/opt/python/current/app/ProjectName')
That will ensure project directory is in pythonpath and you can import AppName from there.

django app module and relative import

Hey I want to use some python module in my app. The problem is that this module has to be "hosted" inside app folder. So I put module in myapp/packagaes folder (myapp/packages/modulename) and then I try to load it in django app from myapp.packages import modulename - this work fine until module consists from submodules which import some other submodules from this module - then the import path is wrong because there isn't myapp. prfix before import path.
For example:
myapp/packages/module/submodule1/...
myapp/packages/module/submodule2/...
and in myapp/packages/module/submodule1/__init__.py:
import module.submodule2 # this will fail because it should be import myapp.module.submodule2
So how I can overcome this situation without modifying module source?