Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - mysite.wsgi - file is not present but uWSGI works. How?

There is some magic around how uWSGI works (I'm using it with Django) which I just don't understand.

I have the following test Django project:

(uwsgi-tutorial)[root@localhost mysite]# tree
.
|-- db.sqlite3
|-- manage.py
|-- mysite
|   |-- __init__.py
|   |-- __pycache__
|   |   |-- __init__.cpython-34.pyc
|   |   |-- settings.cpython-34.pyc
|   |   |-- urls.cpython-34.pyc
|   |   `-- wsgi.cpython-34.pyc
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- test.py

In this folder I run uwsgi --http :8000 --module mysite.wsgi And it works.

How?

I mean, there is no such file as mysite.wsgi. However there is mysite/wsgi.py. Now if I run uwsgi --http :8000 --module mysite/wsgi.py, it doesn't work (HTTP 500).

If I change mysite.wsgi to anything else then uWSGI is unable to load the module, so it looks like module name must match Django project name and you have to add .wsgi to enable the "magic".

Most uWSGI+Django guides, including the official Django 1.8 documentation say something like:

module: The WSGI module to use – probably the mysite.wsgi module that startproject creates

or

module mysite.wsgi: load the specified wsgi module

I do not understand how it works and why it works this way. Can anyone explain? :) Thanks in advance!

like image 406
Dmitrii Sutiagin Avatar asked Sep 01 '15 01:09

Dmitrii Sutiagin


1 Answers

After playing around a bit I seem to have understood what's really happening - module is a Python module reference, like for an import statement in Python.

I thought that uWSGI is a universal thing and not tied to Python, so why would it expect a Python module reference? Looks like uWSGI was initially developed for Python. The rest of the options are available via --plugins and custom builds. So when I installed uWSGI via pip install uwsgi is was compiled with default settings, which means it expects a Python module reference. At least this is how I understand it now.

So I can, for example, rename wsgi.py inside mysite/ folder into socool.py and run my setup via uswgi --http :8000 --module mysite.socool.

If anyone can add clarity to my reply that would be great.

UPDATE-01:

At least with Arch Linux distribution there is a difference between uwsgi system package installed via pacman -S uwsgi and the Python package installed with pip install uwsgi.

If a system package is installed, it will not have Python capabilities out of the box. For example, running uwsgi --module ... will give an error that module option is not supported.

Using uWSGI system package to run Python module is possible only after installing uwsgi-plugin-python. Also it is necessary to specify the plugin to be used - uwsgi --plugins python ....

[root@localhost ~]# uwsgi --help | grep module | wc -l
0
[root@localhost ~]# uwsgi --plugins python --help | grep module | wc -l
23

Suddenly it turns out that extra options appear in help output, and --module is now supported.

Hope this helps those who are unfamiliar with uWSGI like myself.

like image 128
Dmitrii Sutiagin Avatar answered Nov 18 '22 07:11

Dmitrii Sutiagin