Deployment of a Django project on Gandi server - django

While trying to deploy my django project on my Gandy hosting server, while starting running uwsgi, the uwsgi.log shows an error
ModuleNotFoundError: No module named 'django'
while executing the following line from my wsgi.py file
from django.core.wsgi import get_wsgi_application
here is the full wsgi file:
import sys
import os
import os.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'projectname')))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'projectname.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from what I understand, django is not installed in my virtual environment, but it is listed in my requirements.txt file
Django==3.0.6
django-background-tasks==1.2.5
django-compat==1.0.15
django-extensions==3.0.3
django-mathfilters==1.0.0
Can anyone pin point where I am missing something?

I realise you've probably moved on from this now!
In case others hit the same issue, you need to deploy your code, not just upload it, for the Gandi Python environment to read requirements.txt and build your code.
See https://docs.gandi.net/en/web_hosting/connection/git.html?highlight=deploy#deploy-your-code

Related

Error running WSGI application, no module named django

I have some problem while deploying my website to Pythonanywhere.com
i've done all things step by step like it was in a guide-line
and at the end i catch an error:
error log
2022-03-27 20:07:23,699: Error running WSGI application 2022-03-27
20:07:23,700: ModuleNotFoundError: No module named 'django' 2022-03-27
20:07:23,700: File "/var/www/usitingshit_pythonanywhere_com_wsgi.py",
line 88, in <module> 2022-03-27 20:07:23,700: from django.core.wsgi
import get_wsgi_application 2022-03-27 20:07:23,700:
i've tried to change wsgi.py file
import os
import sys
path = '/home/usitingshit/deploy_course_work/'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] ='/home/usitingshit/deploy_course_work/vodprojectstroy_site/wsgi.py'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
structure of my project:
has anybody had the same problem? how can i fix that? sorry if this question is silly, coz i am a beginner in django
Django is preinstalled on PythonAnywhere, so it's basically available out of the box. However, if you want to use a virtual environment for the web app, you need to ensure that Django is available in it too. The error you're seeing is most probably indicating that you set a virtual environment for the web app, but did not install Django in it.

PythonAnywhere wsgi deployment error

I've followed the steps and everything works fine on a local server but when I try to deploy on PythonAnywhere I keep running into problems. I don't know what I'm doing with the WSGI file so I just copied and pasted a template with some adjustments:
import os
import sys
path = '/home/KTruong88/Kappa_Ranks/Kappa_Ranks/'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'Kappa_Ranks.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application
I try to run the thing, and it gives me in the error logs:
TypeError: get_wsgi_application() takes 0 positional arguments but 2 were given
I don't know how if I configured my wsgi file properly, and I don't know where I can even access the get_wsgi_application() function so I can adjust it, or if I could, what would I adjust it too. How can I fix this?
You did not need to "copy and paste" a WSGI file in the first place; it is included in the project that was created when you did django-admin.py startproject.
Nevertheless, the problem is that application should be the object returned from get_wsgi_application, not the function itself:
application = get_wsgi_application()

Do I need to edit the wsgi.py file to install the New Relic Agent using Gunicorn/Python?

I have a django project deployed through digitalocean. I am trying to install the New Relic Python agent and I have followed all of the instructions, but when I edit the wsgi.py, I receive a 502 error. My website is using Django and Unicorn, so I'm not sure if I should even be editing the wsgi.py file? The instructions are quite confusing. I currently have this wsgi.py file in my project directory, and everything works correctly.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Now when I change this to the following (following the New Relic quick start), I receive a 502 error, and nothing in my django/nginx/wsgi logs tells me why.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import newrelic.agent
newrelic.agent.initialize(os.path.join(os.path.dirname(os.path.dirname(__file__), 'newrelic.ini')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
My guess is that I'm not even supposed to edit the WSGI.py file. Another thing that confuses me is that New Relic has directions for installing the Python agent for both uwsgi and Unicorn.

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: Manage.py runserver no error, but mod_wsgi report cannot import name connection

When testing my django website by python manage.py runserver 0.0.0.0:8000, there is no error.
But if I deploy it to production with apache and mod_wsgi, it reports error cannot import name connection.
I find some other questions like django 1.4 database router - "cannot import name connection" suggesting adding from django.db import connections into settings.py.
But I found adding this import can prevent this error, but it also disables database router.
I think it may due to different running environment between manage.py runserver and WSGI.
This is how my wsgi.py looks like
import os
import sys
sys.path.insert(0,"/home/my/myweb")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
How can I fix this bug?
My python version is 2.7 and django version is 1.4.3
Edit 1:
My OS is CentOS 6.4, on this server:
If I run it using python manage.py runserver 0.0.0.0:8000, no error.
If I remove database router, it can work under WSGI.
But database router and WSGI cannot work together.
Any advice is appreciated.
Edit 2:
Thanks to #Graham Dumpleton, I figured it out by myself.
This is the wsgy.py that works for me.
import sys
sys.path.insert(0, '/home/my/myweb')
from mywebsite import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Usually caused by using a mix of imports where some imports are via the site package and some don't. You can add:
sys.path.insert(0,"/home/my/myweb/mywebsite")
and that may help.
For a bit of a discussion of the problem see:
http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html