How to run Django app with Gunicorn/WSGI webserver? - django

I have my existing Django application running locally on my MacBook. It's directory structure looks something like this:
myproject/
mySite/
__init__.py
settings.py
urls.py
wsgi.py
myApp1/
__init__.py
models.py
views.py
manage.py
requirements.txt
Up until now, I have been using the Django toy webserver to run my app: ./manage.py runserver 0.0.0.0:8000. But now I want to use gunicorn instead. So I'm following the instructions here.
I do source myVirtualenv/bin/activate && cd myproject && gunicorn mySite.wsgi. I get the following error:
File "/usr/local/Cellar/python/2.7.12_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "myproject/MyApp2/models.py", line 11, in <module>
from caching.base import CachingManager, CachingMixin
ImportError: No module named caching.base
When I run ./manage.py runserver 0.0.0.0:8000 from the same location it works perfectly fine.
Why? Am I doing something wrong?
Does Django-Cache-Machine not work with Gunicorn/WSGI? How to work around this issue?

To run your project using gunicorn, try the following:
activate your virtualenv
go to your project's directory
run gunicorn mySite.wsgi:application --bind 127.0.0.1:8000
If the commands work fine, than my you are setup. Otherwise, try the following tutorial. I always use this tutorial myself, when setting up a new project for production. Try it. Setting up Django with Nginx, Gunicorn and Supervisor

You seem to have installed gunicorn globally rather than within the virtualenv, so the executable is pointing to the global Python and its site-packages directory rather than the one within the virtualenv. Reinstall gunicorn locally.

Related

How to specify correctly wsgi for Heroku deploy?

I have following project structure
In Procfile i difine web as web: gunicorn george_paintings.wsgi
When i deploy project on Heroku i see that Heroku identified my Procfile
Starting process with command `gunicorn george_paintings.wsgi`
But when i got an error ModuleNotFoundError: No module named 'george_paintings'
`
How to properly set up wsgi in Proclife in context of my project structure?
Your ModuleNotFoundError: No module named ... error is generally fixed by adding an empty __init__.py file to the folder. See python docs
The init.py files are required to make Python treat directories containing the file as packages
What I would suggest is to remove the application folder and move Procfile, README.md and requirements.txt to the outer george_paintings and use that as your root folder.
Then web: gunicorn george_paintings.wsgi should work as expected.

Django heroku : Unable to deploy application (ModuleNotFoundError)

When I am deploying my Django application on Heroku, the application crash and Heroku's logs notify me about this error :
2020-12-07T12:55:55.982458+00:00 app[web.1]: ModuleNotFoundError: No module named 'WebSite'
The thing is, WebSite is not a Python module, but a folder in my Django application (Achievement Comparer being my main application name).
AchievementComparer/
AchievementComparer/ ← My Django project
static/
staticfiles/
WebSite/ ← A Django app
__init__.py
manage.py
wsgi.py
Procfile
requirements.txt
runtime.txt
Of course, if I start my application locally, everything works just fine (running py manage.py runserver on Windows, as gunicorn is only available on unix).
The current issue is that you have an extra directory layer:
AchievementComparer/ ← This shouldn't be here
AchievementComparer/
WebSite/
manage.py
…
Procfile
requirements.txt
runtime.txt
Move everything that's currently in the top-level AchievementComparer/ directory up a level and delete the now-empty old AchievementComparer/ directory:
AchievementComparer/
asgi.py
settings.py
wsgi.py
WebSite/
manage.py
Procfile
requirements.txt
runtime.txt
A few other issues that are likely to bite you next:
The only wsgi.py file you should need is the one that's currently in your AchievementComparer/ project directory, beside your settings.py. Delete the other one.
Your Procfile is hard-coding a port. You'll need to bind to whatever port Heroku assigns you via the PORT environment variable.
You don't need --pythonpath in your Procfile, but you should update the module to use the full module name AchievementComparer.wsgi.
All in all, try changing your Procfile to something like this:
web: gunicorn AchievementComparer.wsgi:application --bind=0.0.0.0:$PORT --log-file -

Django Heroku : python: can't open file 'manage.py': [Errno 2] No such file or directory

I am deploying a Django website on Heroku. My project is called mysite-project which contains at its root manage.py and Procfile
I can visit the heroku website after I run git push heroku master.
And the website shows:
I am assuming I do not see anything (navbar, initial page, etc.) because I did not run migrate.
If I do:
heroku run python manage.py migrate
I get the error:
python: can't open file 'manage.py': [Errno 2] No such file or directory
Which makes no sense since I am in the right directory.
In fact:
I can run python manage.py runserver and locahost works
git ls-files manage.py --outputs--> manage.py
BUT
If I do:
heroku run bash
ls manage.py
I get:
ls: cannot access 'manage.py': No such file or directory
It seems that manage.py is in my local but not in my heroku.
Procfile
web: gunicorn mysite-project.wsgi
First use heroku logs --source app --tail to make sure that the Build succeeded.
If the build is successful, make sure you have your project at the root of the git repo.
I had the same problem because my Django project was not at the root of the git repo.
My file hierarchy was first like (assuming the name of the project prototype and it has one app named app)
├──git_repo
        ├──Procfile
        ├──requirements.txt
        ├──new_folder
                ├──prototype (folder)
                ├──app (folder)
                ├──.gitignore
                ├──manage.py
                ├──db.sqlite3
I changed the hierarchy to the following and it worked
├──git_repo
        ├──Procfile
        ├──requirements.txt
        ├──prototype (folder)
        ├──app (folder)
        ├──.gitignore
        ├──manage.py
        ├──db.sqlite3
The welcome you see when you visit the site (as well as the fact, that you can't find manage.py with ls on the server) tells you, that you haven't successfully pushed your django project to Heroku yet. Try and run git push heroku master.
It may be that the push has failed, in which case you should consult the documentation. Specifically, run
$ pip install gunicorn django-heroku
$ pip freeze > requirements.txt
...and then add
import django_heroku
django_heroku.settings(locals())
...to the bottom of settings.py. Also don't forget to set STATIC_ROOT='staticfiles' in settings.pyaswell.
That screenshot attached indicates you have just created app in heroku but not pushed your local repository code to heroku app.
git push heroku master
I was getting this error too!
I solved it by specifying the path:
python <your_project_name>/manage.py migrate
This worked for me.
In my case, it was because I was using a branch name other than master. My branch name was heroku_branch, so to make things work properly, I pushed it this way
git push heroku heroku_branch:master
It was pushed this time and worked fine.
For django + heroku
web: python manage.py runserver 0.0.0.0:\$PORT

Running Django projects from virtualenv with Gunicorn >=19

I saw news on docs.gunicorn.org for gunicorn v.19:
Deprecations
run_gunicorn, gunicorn_django and gunicorn_paster are now completely
deprecated and will be removed in the next release. Use the gunicorn
command instead.
I run my applications from virtual environments, created with virtualenv with this command in supervisor:
[program:my_app]
command=/var/www/.virtualenvs/my_app/bin/gunicorn_django -c /var/www/my_app/conf/gunicorn.conf.py
user=www-data
group=www-data
daemon=false
debug=false
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/my_app/log/supervisor.log
How should I change my settings to run my projects with the new version gunicorn?
The command line should be changed to the following
command=/var/www/.virtualenvs/my_app/bin/gunicorn my_app.wsgi:application -c /var/www/my_app/conf/gunicorn.conf.py
This is assuming that you have the file my_app/wsgi.py. Since Django 1.4, startproject has generated a wsgi.py file for you as part of your project. I'd assume you have this, but if not you can use the following snippet to create that file.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
See https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
You may need to ensure that the my_app directory is in your PYTHONPATH if it is not already, or you will get errors.
To test this out standalone on the command line with a new django project, the following should work assuming you have django and gunicorn already installed in you current environment.
django-admin.py startproject myproject
cd myproject
export PYTHONPATH=$PYTHONPATH:.
gunicorn myproject.wsgi:application -b localhost:8008

Django and gunicorn: Specify path to django.wsgi file in environment specific folders for Heroku

I have my project based on the django startproject: https://github.com/lincolnloop/django-startproject/
Here is my procfile:
web: gunicorn <myproject>.wsgi:application
Right now I have my set up like this:
[myproject]
---[conf]
------[local]
---------settings.py
------[qa]
---------settings.py
---[server_configs]
------[local]
---------django.wsgi
------[qa]
---------django.wsgi
My django.wsgi looks like this:
import os
import sys
import django.core.handlers.wsgi
sys.path.append('/app')
sys.path.append('/app/myproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.conf.qa.settings'
application = django.core.handlers.wsgi.WSGIHandler()
I want to use the qa/django.wsgi for Heroku, but I am getting an error saying:
ImportError: No module named [myproject].wsgi
I have already gone through and tried solutions in this post: Configuring gunicorn for Django on Heroku but with no luck on my end.
Right now my PYTHONPATH is /app and have already tried Gunicorn+Django+Heroku. Python Path Issue with no luck either.
I had a similar problem once. Basically, you're running gunicorn from your root directory, but your wsgi is in [my_project]/[server_configs]/[qa]/.
There are two ways to go about it. If each of those directories has an init.py file, you can call it like a normal python module. Use:
web: gunicorn [myproject].[server_configs].[qa].django.wsgi:application
If they don't have init.py files (I didn't), you'll need your Procfile to switch into the proper directory. Try:
web: sh -c 'cd [server_configs]/[qa] && gunicorn django.wsgi:application'
Basically this is running a command in your shell to (1) change directories and (2) run gunicorn.
Heroku allows you to use environment variables in your Procfile. If you define an environment variable on Heroku, like so: heroku config:set WSGI_PATH=path/to/my/file/wsgi.py, then in your Procfile do: gunicorn $WSGI_PATH:application you should be good to go!