Need help debugging ModuleNotFoundError: No module named 'django'

1,317 views
Skip to first unread message

Tim Buckland

unread,
May 23, 2018, 4:36:14 PM5/23/18
to modwsgi
I'm trying to deploy a simple Django app to Ubuntu 18.04 using mod_wsgi but am struggling to get it all to work.

I'm able to run the app using the Django development server without issue.

I also got the hello_world wsgi app to run using Apache. The problem appears to be with the Django environment, but I've run out of ideas on things to try.

Note that I'm pretty new to both Linux and Apache so the chance I have made some basic mistake is very high!

This is approximately what I did (among much else):

cd /
sudo apt-get install python3-pip
sudo apt-get install python3-virtualenv
sudo apt install gnome-terminal
mkdir venv
cd venv
sudo virtualenv dash
. dash/bin/activate
pip3 install Django
cd /var/www
sudo mkdir dash
sudo chmod 777 dash
cd dash
cd test-results-dashboard
python3 linux_new_dashboard.py

sudo apt update
sudo apt install apache2
sudo apt install apache2-dev
pip3 install mod_wsgi
mod_wsgi-express module-config | sudo tee /etc/apache2/conf-enabled/wsgi.conf
sudo cp /var/www/dash/test-results-dashboard/dash/httpd-vhosts_linux.conf /etc/apache2/sites-enabled/test-results-dashboard.conf
sudo systemctl restart apache2

(note that I also deleted the default config file in /etc/apache2/conf-enabled)


[Wed May 23 12:05:46.517907 2018] [wsgi:error] [pid 21512:tid 139994546329344] [client 127.0.0.1:60226]   File "/var/www/dash/test-results-dashboard/dash/dash/wsgi.py", line 12, in <module>
[Wed May 23 12:05:46.517911 2018] [wsgi:error] [pid 21512:tid 139994546329344] [client 127.0.0.1:60226]     from django.core.wsgi import get_wsgi_application
[Wed May 23 12:05:46.517929 2018] [wsgi:error] [pid 21512:tid 139994546329344] [client 127.0.0.1:60226] ModuleNotFoundError: No module named 'django'

The contents of test-results-dashboard.conf:
# test-results-dashboard virtual host configs

WSGIPythonPath /var/www/dash/test-results-dashboard/dash
<VirtualHost *:80>

    #ServerName www.example.com
    #ServerAlias example.com
    #ServerAdmin webm...@example.com

    DocumentRoot /var/www/html

    WSGIScriptAlias /hello_world /var/www/dash/test-results-dashboard/tests/hello_world/wsgi-scripts/hello_world.wsgi
    <Directory /var/www/dash/test-results-dashboard/tests/hello_world/wsgi-scripts>
        AllowOverride None
        Require all granted
        <Files hello_world.wsgi>
            Require all granted
        </Files>
    </Directory>

    Alias /static /var/www/dash/test-results-dashboard/dash/results/static
    <Directory /var/www/dash/test-results-dashboard/dash/results/static>
        AllowOverride None
        Require all granted
    </Directory>

    WSGIScriptAlias /dash /var/www/dash/test-results-dashboard/dash/dash/wsgi.py
    <Directory /var/www/dash/test-results-dashboard/dash/dash>
        AllowOverride None
        Require all granted
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

</VirtualHost>

I'm wondering if things are mixed up between python 2 and python 3? I tried adding a python3 shebang to wsgi.py but that didn't help.

Thanks in advance to anyone that can provide some tips on getting to the bottom of this!

Graham Dumpleton

unread,
May 23, 2018, 4:39:27 PM5/23/18
to mod...@googlegroups.com
What do you get if you substitute in a WSGI hello world which does checks in:


This will tell you which Python installation/version is being used.

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 post to this group, send email to mod...@googlegroups.com.
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Tim Buckland

unread,
May 23, 2018, 4:40:51 PM5/23/18
to modwsgi
Ok, I've got this working now :)

It seems I needed to issue this command:
python3 -m venv dash

... instead of using virtualenv which created a Python 2 virtual environment!

Tim Buckland

unread,
May 23, 2018, 4:51:04 PM5/23/18
to modwsgi
Thanks very much Graham,

I followed the links in your doco and eventually cottoned on that I'd created a Python 2 virtual environment and fooled myself into believing it was accepting my pip3 commands, but obviously it was just ignoring me and installing to the main environment.

Then once you have setup the virtual environment correctly and activated it you can switch to using python and pip instead of python3 and pip3.

It seems to be working smoothly now!

Regards,

Tim

Graham Dumpleton

unread,
May 24, 2018, 2:20:14 AM5/24/18
to mod...@googlegroups.com
Yeah, virtual environments tend not to have python/pip with a version suffix. So important to use just python/pip once the virtual environment activated else can fallback to system Python.

I would strongly recommend not creating the virtual environment as root. Use a normal user and then also install all Python packages into the virtual environment as the non root user.

Only use the root user when installing the system packages, not the subsequent virtual environment creation and Python package installation.

Graham

Tim Buckland

unread,
May 24, 2018, 6:16:15 AM5/24/18
to modwsgi
Thanks again, I'm going to update my installation instructions to include this and the explanation - it is all a lot clearer now!

Regards,

Tim 

Sujai Kumar

unread,
May 24, 2018, 6:41:53 AM5/24/18
to mod...@googlegroups.com
Can you please do this?

pip3 install django #Please note it is small 'd' and not capital 'D'.

If django is already installed you will get appropriate message. If already installed try importing the same from python prompt by doing 'import django'. If no error display then django is installed successfully already. Once you confirm this we can proceed with next steps. 

Thanks & regards,
Sujaikumar 

--

Prajwal Gautam

unread,
May 29, 2020, 12:47:46 AM5/29/20
to modwsgi
Hi there,
I encountered the same problem but I havent been able to fix this issue. Any form of assistance would be highly appreciated!

Graham Dumpleton

unread,
May 29, 2020, 12:51:15 AM5/29/20
to mod...@googlegroups.com
Can you explain your problem from the start. We don't necessarily see the full history of what the prior issue was about. Plus your issue could be entirely different.

So, error you are getting and what you expect, the mod_wsgi configuration you are using, platform, Python version, how mod_wsgi was installed, whether Python virtual environment used, where is Django installed (globally, virtual environment, per user site packages) etc etc. In other words, any information that might be useful to work out what your problem is.

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.

Prajwal Gautam

unread,
May 29, 2020, 6:47:57 AM5/29/20
to modwsgi
Hi,

Sorry about that. 
Here's the error.log file:

 mod_wsgi (pid=147546): Failed to exec Python script file '/home/prajwael/django_project/django_project>

mod_wsgi (pid=147546): Exception occurred processing WSGI script '/home/prajwael/django_project/django>

Traceback (most recent call last):

File "/home/prajwael/django_project/django_project/wsgi.py", line 12, in <module>

[wsgi:error] from django.core.wsgi import get_wsgi_application

[wsgi:error]  ModuleNotFoundError: No module named 'django'


As far as the packages used, I am using Python 3.8.2, Apache/2.4.41 (Ubuntu), pip 20.0.2, Django==3.0.6, virtual environment is used, django is installed globally as well as locally. mod_wsgi was installed using the command: 

sudo apt-get install libapache2-mod-wsgi-py3


Following is in the django_project.conf file:

WSGIScriptAlias / /home/prajwael/django_project/django_project/wsgi.py

        WSGIDaemonProcess django_app python-path=/home/prajwael/django_project python-home=/home/prajwael/django_project/venv

        WSGIProcessGroup django_app


wsgi.py file has the following content:

import os


from django.core.wsgi import get_wsgi_application


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")


application = get_wsgi_application()


Thank you


On Thursday, May 28, 2020 at 11:51:15 PM UTC-5, Graham Dumpleton wrote:
Can you explain your problem from the start. We don't necessarily see the full history of what the prior issue was about. Plus your issue could be entirely different.

So, error you are getting and what you expect, the mod_wsgi configuration you are using, platform, Python version, how mod_wsgi was installed, whether Python virtual environment used, where is Django installed (globally, virtual environment, per user site packages) etc etc. In other words, any information that might be useful to work out what your problem is.

Graham

On 29 May 2020, at 1:55 pm, Prajwal Gautam <gunn.pra...@gmail.com> wrote:

Hi there,
I encountered the same problem but I havent been able to fix this issue. Any form of assistance would be highly appreciated!

On Wednesday, May 23, 2018 at 3:40:51 PM UTC-5, Tim Buckland wrote:
Ok, I've got this working now :)

It seems I needed to issue this command:
python3 -m venv dash

... instead of using virtualenv which created a Python 2 virtual environment!


-- 
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 mod...@googlegroups.com.

Graham Dumpleton

unread,
May 29, 2020, 6:56:58 AM5/29/20
to mod...@googlegroups.com
If Django is definitely installed in the Python virtual environment, most likely the issue is that mod_wsgi was compiled for a different Python version that Python 3.8.

See:


You can use mod_wsgi compiled for Python 3.6 or 3.7 with a Python virtual environment created with Python 3.8.

Another cause is that permissions on installed Python packages (Django) are such that the Apache user cannot access them.

Confirm what version of Python mod_wsgi is compiled for.

Also run Python interpreter with activate Python virtual environment and do:

    import django
    print(django.__file__)

Check that the directory where it is installed doesn't have restrictive permissions.

Graham

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/d99d4604-9b28-402f-8377-ca1fc78bad1d%40googlegroups.com.

Graham Dumpleton

unread,
May 29, 2020, 6:58:25 AM5/29/20
to mod...@googlegroups.com

On 29 May 2020, at 8:56 pm, Graham Dumpleton <graham.d...@gmail.com> wrote:

If Django is definitely installed in the Python virtual environment, most likely the issue is that mod_wsgi was compiled for a different Python version that Python 3.8.

See:


You can use mod_wsgi compiled for Python 3.6 or 3.7 with a Python virtual environment created with Python 3.8.

I obviously meant to say:

You can NOT use mod_wsgi compiled for Python 3.6 or 3.7 with a Python virtual environment created with Python 3.8.

Prajwal Gautam

unread,
May 29, 2020, 7:46:11 PM5/29/20
to modwsgi
I fixed the problem. I had to modify the wsgi.py file and it worked.
Thank you though!


On Friday, May 29, 2020 at 5:56:58 AM UTC-5, Graham Dumpleton wrote:
If Django is definitely installed in the Python virtual environment, most likely the issue is that mod_wsgi was compiled for a different Python version that Python 3.8.

See:


You can use mod_wsgi compiled for Python 3.6 or 3.7 with a Python virtual environment created with Python 3.8.

Another cause is that permissions on installed Python packages (Django) are such that the Apache user cannot access them.

Confirm what version of Python mod_wsgi is compiled for.

Also run Python interpreter with activate Python virtual environment and do:

    import django
    print(django.__file__)

Check that the directory where it is installed doesn't have restrictive permissions.

Graham

To unsubscribe from this group and stop receiving emails from it, send an email to mod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/d99d4604-9b28-402f-8377-ca1fc78bad1d%40googlegroups.com.

Graham Dumpleton

unread,
May 29, 2020, 8:12:23 PM5/29/20
to mod...@googlegroups.com
How did you modify the wsgi.py file?

If this was an issue with not seeing the virtual environment correctly, you would usually adjust the python-home option, not make changes in the wsgi.py file. If it is a mismatch in Python versions, you will end up with problems still later if trying to force crossing of versions by adding site-packages directory explicitly.

If you say what the changes were, then can indicate whether it is the correct way of fixing things or whether should be solved a different better way.

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/0723fdda-4cfa-4281-9737-0f204a628993%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages