No module named 'django'

57 views
Skip to first unread message

Anibal Xochipa

unread,
Nov 30, 2020, 10:19:34 PM11/30/20
to modwsgi
I'm trying to deploy my Django app using Apache2 on a raspberry pi 4B running raspbian 10 
My project path looks like this 
/home/pi/Andola
Inside that folder I created a Virtual environment called MyDjangoEnv running python 3.8.2 and Django 3.0.3 so the path to the virtual environment looks like this
/home/pi/Andola/myDjangoEnv
My wsgi.py file lives inside this folder: /home/pi/Andola/AnDjo/wsgi.py , next to the settings.py  , __init__.py , etc  


I copied the file 000-default.conf on /etc/apache2/sites-available to another one named django_project.conf in order to configure my own site. I configured it according to Django documentation and my new file looks like this:

<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

Alias /static /home/pi/Andola/static
<Directory /home/pi/Andola/static>
Require all granted
</Directory>

        Alias /media /home/pi/Andola/media
        <Directory /home/pi/Andola/media>
                Require all granted
        </Directory>

<Directory /home/pi/Andola/AnDjo>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

WSGIScriptAlias / /home/pi/Andola/AnDjo/wsgi.py
WSGIDaemonProcess django_app python-path=/home/pi/Andola/ python-home=/home/pi/Andola/myDjangoEnv
WSGIProcessGroup django_app

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I think I gave the propper permissions to media folder and changed the ownership  of my project folder (Andola) to www-data.

My Django application runs totally fine when I use python manage.py runserver but whenever I try to run apache it gives me the 500 Internal Server Error and the next shows up in the error log

[Mon Nov 30 20:47:16.396524 2020] [mpm_event:notice] [pid 10724:tid 3069383184] AH00491: caught SIGTERM, shutting down
[Mon Nov 30 20:47:16.540219 2020] [mpm_event:notice] [pid 12812:tid 3069309456] AH00489: Apache/2.4.38 (Raspbian) mod_wsgi/4.6.5 Python/3.7 configured -- resuming normal operations
[Mon Nov 30 20:47:16.540623 2020] [core:notice] [pid 12812:tid 3069309456] AH00094: Command line: '/usr/sbin/apache2'
[Mon Nov 30 20:47:20.764437 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238] mod_wsgi (pid=12813): Failed to exec Python script file '/home/pi/Andola/AnDjo/wsgi.py'.
[Mon Nov 30 20:47:20.764696 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238] mod_wsgi (pid=12813): Exception occurred processing WSGI script '/home/pi/Andola/AnDjo/wsgi.py'.
[Mon Nov 30 20:47:20.765543 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238] Traceback (most recent call last):
[Mon Nov 30 20:47:20.767301 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238]   File "/home/pi/Andola/AnDjo/wsgi.py", line 12, in <module>
[Mon Nov 30 20:47:20.767372 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238]     from django.core.wsgi import get_wsgi_application
[Mon Nov 30 20:47:20.767491 2020] [wsgi:error] [pid 12813:tid 3038991392] [remote ::1:52238] ModuleNotFoundError: No module named 'django'


Python Installation in use:
sys.version = '3.7.3 (default, Jul 25 2020, 13:03:44) \n[GCC 8.3.0]' sys.prefix = '/home/pi/Andola/myDjangoEnv'

Python module search path:
sys.path = ['/home/pi/Andola', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload']

Embedded or Daemon Mode
mod_wsgi.process_group = 'django_app'

Subinterpreter Being Used
mod_wsgi.application_group = '192.168.1.70|'

I can see that wsgi might not be using myDjangoEnv and a bad configuration is happening but as this is the first time I'm using wsgi and also the first time trying to make a website I've been kind of lost for the last few days /::

Graham Dumpleton

unread,
Nov 30, 2020, 10:26:22 PM11/30/20
to mod...@googlegroups.com
Two possibility come to mind immediately.

The first is that the version of Python that mod_wsgi was compiled for is different to the version of Python your virtual environment was created using. You can't force mod_wsgi compiled for one Python version to use a virtual environment for a different Python version.

The second is that the permissions on the Python virtual environment root directory are such that the Apache user cannot access it, or that the Django package directory within the site-packages directory of the Python virtual environment doesn't have permissions such that Apache user can access it.

What do you get for:

    ls -lasd /home/pi/Andola
    ls -lasd /home/pi/Andola/myDjangoEnv
    ls -lasd /home/pi/Andola/myDjangoEnv/lib/python3.7/site-packages/django*

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/0ecac6e4-53ad-43ad-ab9e-b498775a3275n%40googlegroups.com.

Anibal Xochipa

unread,
Dec 1, 2020, 4:45:29 PM12/1/20
to modwsgi
This is what I get:

ls -lasd /home/pi/Andola
4 drwxr-xr-x 16 pi www-data 4096 nov 30 17:56 /home/pi/Andola

ls -lasd /home/pi/Andola/myDjangoEnv
4 drwxr-xr-x 5 pi pi 4096 nov 16 15:29 /home/pi/Andola/myDjangoEnv

ls -lasd /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django*
4 drwxr-xr-x 19 pi pi 4096 nov 30 15:06 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django
4 drwxr-xr-x  2 pi pi 4096 nov 17 18:24 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_allauth-0.42.0.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 17 11:35 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_appconf-1.0.4.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 17 18:15 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_crispy_forms-1.9.2.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 17 11:35 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_imagekit-4.0.2.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 16 16:42 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_phonenumber_field-4.0.0.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 17 18:10 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_tinymce-3.1.0.dist-info
4 drwxr-xr-x  2 pi pi 4096 nov 17 18:17 /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/django_widget_tweaks-1.4.8.dist-info

My virtual environment has python 3.8 as that's the one my application needs, however I think wsgi seems to be pulling python 3.7 out of somewhere.

How do I now whether I'm using the wrong version of wsgi and what can I do to switch for the right one?? I've been searching some tutorials but haven't had any success
I installed wsgi using this command while my virtual environment was activated:
sudo apt-get install libapache2-mod-wsgi-py3

Thnk you for your help 

Graham Dumpleton

unread,
Dec 1, 2020, 5:01:55 PM12/1/20
to mod...@googlegroups.com
You will need to uninstall the system package for mod_wsgi and compile mod_wsgi yourself against Python 3.8.

You can build/install it using 'pip install mod_wsgi' with Python 3.8 virtual environment activated. Then configure Apache to use it.

See:


Graham

Anibal Xochipa

unread,
Dec 2, 2020, 2:04:42 PM12/2/20
to modwsgi
I did as you said, reinstalled and followed the instructions on the link you provided. 
I also enabled the module but now apache2 wont restart

sudo systemctl status apache2.service
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-12-02 12:50:25 CST; 51s ago
  Process: 18421 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)

dic 02 12:50:25 andola-server systemd[1]: Starting The Apache HTTP Server...
dic 02 12:50:25 andola-server apachectl[18421]: apache2: Syntax error on line 146 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/wsgi.load: Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/apache2/modules/mod_wsgi.so: cannot open shared object file: No such file or directory
dic 02 12:50:25 andola-server apachectl[18421]: Action 'start' failed.
dic 02 12:50:25 andola-server apachectl[18421]: The Apache error log may have more information.
dic 02 12:50:25 andola-server systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
dic 02 12:50:25 andola-server systemd[1]: apache2.service: Failed with result 'exit-code'.
dic 02 12:50:25 andola-server systemd[1]: Failed to start The Apache HTTP Server.


I looked for it and /usr/lib/apache2/modules/mod_wsgi.so actually doesn't exists on that directory I can't find what's happening

Anibal

Graham Dumpleton

unread,
Dec 2, 2020, 2:09:12 PM12/2/20
to mod...@googlegroups.com
When uninstalling the system package for mod_wsgi, I would have expected the existing wsgi.load file which contained the LoadModule line for "modules/mod_wsgi.so" (or similar) to have been removed.

You would have needed to have replaced the whole wsgi.load file with your own which contained the output of running:

    mod_wsgi-express module-config

What do you currently have in wsgi.load file?

Where in the Apache configuration did you put the output of running the "mod_wsgi-express module-config" command?

Graham

Anibal Xochipa

unread,
Dec 2, 2020, 2:41:07 PM12/2/20
to modwsgi
The first time I putted the oputput from "mod_wsgi-express module-config" on 
/etc/apache2/httpd.conf 
Getting the result 
Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/apache2/modules/mod_wsgi.so: cannot open shared object file: No such file or directory

On wsgi.load file I have 
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

which gave me the last error

Now I changed it with the output from "mod_wsgi-express module-config" command getting the next result

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-12-02 13:14:12 CST; 25s ago
  Process: 18820 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)

dic 02 13:14:12 andola-server systemd[1]: Starting The Apache HTTP Server...
dic 02 13:14:12 andola-server apachectl[18820]: apache2: Syntax error on line 146 of /etc/apache2/apache2.conf: Syntax error on line 2 of /etc/apache2/mods-enabled/wsgi.load: Cannot load /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-arm-linux-gnueabihf.so into server: /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-arm-linux-gnueabihf.so: undefined symbol: forkpty
dic 02 13:14:12 andola-server apachectl[18820]: Action 'start' failed.
dic 02 13:14:12 andola-server apachectl[18820]: The Apache error log may have more information.
dic 02 13:14:12 andola-server systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
dic 02 13:14:12 andola-server systemd[1]: apache2.service: Failed with result 'exit-code'.
dic 02 13:14:12 andola-server systemd[1]: Failed to start The Apache HTTP Server.


Graham Dumpleton

unread,
Dec 2, 2020, 2:49:23 PM12/2/20
to mod...@googlegroups.com
Based on:


it is likely that the Python library on that system should have been linked with -lutil, but Apache (which is the main program in this case) isn't. Further, the Python distribution configuration is not properly setup to ensure that library is linked when building C extensions.

The quick solution may be to find where libutil.so is located (possibly /lib64/libutil.so), and with the correct path, add a line:

    LoadFile /lib64/libutil.so

just before the LoadModule line for mod_wsgi.

Graham

Anibal Xochipa

unread,
Dec 2, 2020, 7:10:11 PM12/2/20
to modwsgi
Now I'm getting:

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-12-02 17:49:33 CST; 37s ago
  Process: 21591 ExecStart=/usr/sbin/apachectl start (code=exited, status=1/FAILURE)

dic 02 17:49:33 andola-server systemd[1]: Starting The Apache HTTP Server...
dic 02 17:49:33 andola-server apachectl[21591]: apache2: Syntax error on line 146 of /etc/apache2/apache2.conf: Syntax error on line 3 of /etc/apache2/mods-enabled/wsgi.load: Cannot load /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-arm-linux-gnueabihf.so into server: /home/pi/Andola/myDjangoEnv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-arm-linux-gnueabihf.so: undefined symbol: sincos
dic 02 17:49:33 andola-server apachectl[21591]: Action 'start' failed.
dic 02 17:49:33 andola-server apachectl[21591]: The Apache error log may have more information.
dic 02 17:49:33 andola-server systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
dic 02 17:49:33 andola-server systemd[1]: apache2.service: Failed with result 'exit-code'.
dic 02 17:49:33 andola-server systemd[1]: Failed to start The Apache HTTP Server.

Do I have a missing library maybe?

Graham Dumpleton

unread,
Dec 2, 2020, 7:12:50 PM12/2/20
to mod...@googlegroups.com
Also add:

    LoadFile /lib64/libmath.so

Again, work out the correct path for the math library.

In other words, the shared Python library isn't itself linking -lm this time and neither is Apache.

Graham

Anibal Xochipa

unread,
Dec 2, 2020, 7:51:44 PM12/2/20
to modwsgi
I can't seem to find libmath.so anywhere on my device.

I also ran 
ldconfig -p | grep libmath  getting no output

Graham Dumpleton

unread,
Dec 2, 2020, 7:53:15 PM12/2/20
to mod...@googlegroups.com
Sorry, got the -lm bit right, but not the file path. Is:

    LoadFile /lib64/libm.so

So look for libm.so, not libmath.so.

Anibal Xochipa

unread,
Dec 2, 2020, 8:11:33 PM12/2/20
to modwsgi
It's working!!! (':::
I've been struggling with this since the weekend 
Thank you a lot for your help!

Graham Dumpleton

unread,
Dec 2, 2020, 8:16:51 PM12/2/20
to mod...@googlegroups.com
Thanks for letting me know.

Is a worry though that it seems like the Python configuration for building the Python shared library are suspect on Raspbian. They should be linking the Python shared library to "-lutil -lm" to ensure that when Python is used in embedded systems, that it has all its dependencies and isn't dependent on the application it is linked into linking those libraries. This has come up before in the past with -lm on one system I remember, but issue seemed to go away so they must have addressed it.

Graham

Reply all
Reply to author
Forward
0 new messages