JAOdev
JAOdev

Reputation: 341

Running Gunicorn and Django

I don't know how to use gunicorn with django. Could you give me some help?

This is how I run the server with django. It is https

python3 manage.py runsslserver xx.8x.x3.x4:443 --certificate /etc/letsencrypt/live/callservicesvps.online/fullchain.pem --key /etc/letsencrypt/live/callserv.com/privkey.pem

And in the gunicorn documentation it is mentioned that it must be executed as follows:

gunicorn myproject.wsgi

And here I have 2 questions. What is myproject.wsgi? Where Can I find it? Because if I look in the directory where the django project is, the only thing I find with wsgi is a file called wsgi.py

Running the server as follows gives me an error

gunicorn /home/proyectdirectory/wsgi.py

It also gives me an error if I put:

gunicorn /home/proyectdirectory/wsgi:Some_directory_where_the_proyec_is

Upvotes: 1

Views: 3000

Answers (1)

Daniel Oram
Daniel Oram

Reputation: 8411

What is myproject.wsgi?

myproject.wsgi IS the wsgi.py file you have located inside your project.

[End of answer]


But a further explanation will clear up why this is..

I imagine most people will look atmyproject.wsgi and see a file with an extension file type .wsgi but this is just because of the way importing of modules is written in python.

I want to clarify what a module and a package before continuing an explanation.

What is a Module

From the jargon heavy python docs

A module is a file containing Python definitions and statements.

Put simply, a module in python is just a python file containing any sort of functions, variables, or classes etc.

What is a Package

A package is just a collection of modules. The most simplest example, a special directory containing python files. In order to tell python that a directory is a package it must have a file named __init__.py inside of it. You will find a few of these inside different directories inside your django project. This is why they are there.




Now, I can say what I want to say which is the structure of the module namespace in python.

package.subpackage.module

If you look inside your wsgi.py file you'll see a good example of importing from django's own wsgi module.

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<project>.settings')

application = get_wsgi_application()

More specifically

from django.core.wsgi import get_wsgi_application

If you look inside the django package you can see how this works. django (package) core (subpackage) wsgi (module)

location of wsgi application import



Remember, the command gunicorn myproject.wsgi should be run inside the base directory of your django project. I always remember this as the directory containing the manage.py file. That is how gunicorn can find the wsgi.py file using the module namespace in this way. gunicorn is written in python afterall.

gunicorn /home/proyectdirectory/wsgi.py will error because python module imports don't contain / and even if you tried gunicorn home.proyectdirectory.wsgi home and proyectdirectory are not python packages.




Now hopefully this makes sense:

"If gunicorn myproject.wsgi is referring to the wsgi.py file why not just put gunicorn myproject.wsgi.py?"

You can't put a .py extension because this will refer to a module inside the wsgi subpackage with a filename py.py!

Upvotes: 4

Related Questions