I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe.
Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :
gunicorn wsgi:application
However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated.
I tried understanding the following method :
How to use Flask-Script and Gunicorn
But I can't figure out how to make it work with Django.
The following doesn't work:
from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)
Could someone tell me how to start the gunicorn server from my Python script ?
Related
I am trying to port a django web application from some server to cloudera "applications" (data science workbench) and I was trying to make it work. I managed to do so with flask and fastapi applications, just the django framework is missing. My problem, when trying the base setup (https://docs.djangoproject.com/en/3.2/intro/tutorial01/), locally works like a charm but when I try to start up the server from an instance in cloudera, the server does not start and, more curiosuly, I get a weird output related to some package of the image I am spinning. (you can see I am bypassing the traditional runserver command on django because in the cloudera applications side I cannot run shells directly and also due to the fact that I would to tie it up with some environmental variable)
below the manage.py
import os
import sys
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
port_to_pass = 8000
args = ['name', 'runserver', '127.0.0.1:%d' % port_to_pass]
execute_from_command_line(args)
weird "print" (the instance I am spinning has got the exact same package versions installed locally):
when I should be getting:
any idea what might the problem be?
You need an extra entry.py script:
!pip install django
!python manage.py runserver 8000
Heroku seems to have a dependency on Gunicorn when it comes to Python / Django apps. Gunicorn is not supported on Windows. Has anybody had success or know of a work around?
My app runs fine but not under Heroku or Heroku local
Error:
...site-packages\gunicorn\util.py", line 9, in <module>
import fcntl
ModuleNotFoundError: No module named 'fcntl'
Exited with exit code null
It seems unfair to blame Heroku for this. Gunicorn doesn't support Windows. Heroku has nothing to do with Windows.
There are other WSGI web servers that may work. For example, uWSGI has documentation for running on Heroku.
A quick summary:
Make sure that uwsgi and werkzeug are in your requirements.txt or Pipfile / Pipfile.lock and that these files are tracked by Git
Create and track a uwsgi.ini file containing something like
[uwsgi]
http-socket = :$(PORT)
master = true
processes = 4
die-on-term = true
module = werkzeug.testapp:test_app
memory-report = true
making sure to set the module appropriately for your application.
Update your Procfile to contain
web: uwsgi uwsgi.ini
Make sure it works with heroku local, then push to Heroku.
I'm working on a project which used to be run on Linux for test. It's an App Engine flex project, run with gunicorn. Gunicorn does not work on Windows if I understood well, so I've been adviced to use waitress.
I also use virtualenv in my project.
So when I'm in my virtualenv, I run waitress-serve main:app (the gunicorn cmd was gunicorn -b :8080 main:app). I get an error: It had these arguments:
1. No module named flask.
I use flask. I can see the flask folder in my virtualenv folder. And when I run python then from flask import Flask I have no error.
Is there compat issue between waitress and virtualenv ? Or I'm doing something else wrong ? (already tried to delete virtualenv folder and install all the things again)
Python modules are case sensitive
Try Flask not flask.
This is the Error logs I find in the heroku when deploying the python pyramid application. I have followed the each and every steps the python pyramid documentation has. Where have I exactly missed not able to figure out.
I doubt if my way of creating run file is incorrect. I have created a run.py
file and added the following code into it.
#!/bin/bash
set -e
python setup.py develop
python runapp.py
You cannot host a web app like that. You need a proper server, for example gunicorn - see the Pyramid docs on how to run with gunicorn, that is what needs to go in your Procfile. You don't need a run.py.
Try this:
Profile
web: ./run
run
#!/bin/bash
set -e
python setup.py develop
python runapp.py
runapp.py
#Heroku Startup
import os
from paste.deploy import loadapp
from waitress import serve
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app = loadapp('config:production.ini', relative_to='.')
serve(app, host='0.0.0.0', port=port)
requirements.txt
pyramid
pyramid_chameleon
pyramid_debugtoolbar
waitress
<add other dependencies here>
runtime.txt
python-3.3.0 #or whatever version you are running. Take this out
It appeared that I successfully deployed the application using this code
import sys, os
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "UR.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
However Django did not run properly because it could not get to the latest version of Django and ran an old version with Python 2.3. I installed the latest Django and Python 2.7 and I added those two lines and now I am getting an internal server error..
INTERP = '/home/username/opt/python-2.7.7/bin/python'
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
I am sure this is the right path to my Python 2.7 ... any help will be really appreciated
I finally solved it ,
I was deploying on dreamhost and their servers had django1.2 and python2.6, whenever i tried to make python2.7 and let it be the default passenger gets to python 2.6 and successfully executes python2.7 but for some reason it ran into django1.2 instead of the latest django i installed.
I installed python2.7 in /usr/ (using a sudoer account ofcourse) then i installed the new django into site packages and now I no longer need the lines that execute python2.7 as it is the default now along with django 1.7
I have also edited the passenger-wsgi file and this code worked very fine for me....
import sys, os
sys.path.append(os.path.join(os.getcwd(), 'projectname')) #I needed this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The only setback now is that the server is to slow in it's response but other than that everything went fine
And i found a great trick to get into the logs of django
ssh into your server
Execute python manage.py runserver
Open another ssh session at the same time
In the new ssh execute lynx localhost:8000
Then you will be able to see logs and debug information from django in the second ssh .