简体   繁体   中英

mod_wsgi error: ModuleNotFoundError: No module named 'django'

I am trying to deploy a very basic Django application with Apache 2.4 on Ubuntu 18.04 without using a virtual environment. When wsgi.py executes, it cannot find django module.

I have tried setting sys.path in wsgi, various solutions that define different configuration settings for 000-default.conf. Changing ownership of the site-packages folder to www-data for Apache, but nothing seems to work. I could make it work using virtualenv but for a production server, I do not want to use virtualenv. I can import django in Python's command line without an issue.

Following is my sample.tst.conf, if have already activated it using a2ensite command.

<code>
<VirtualHost *:80>

    ServerName sample.tst
    ServerAdmin webmaster@sample.tst
    DocumentRoot /var/www/html

    <Directory /home/raza/projects/sample/sample>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess sample python-path=/home/raza/projects/sample
    WSGIProcessGroup sample

    WSGIScriptAlias / /home/raza/projects/sample/sample/wsgi.py

    <Location />
    WSGIProcessGroup sample
    </Location>

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

</VirtualHost>
<code>

How can I fix this problem? I am struggling with this issue for more than a week.

I am a very experienced programmer but very new to Linux, python, and Apache platform, so I may be making some obvious mistake.

I get following error in Apache log file:

[Tue Jul 02 18:05:22.458785 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.458854 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.458916 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] Traceback (most recent call last):
[Tue Jul 02 18:05:22.459009 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170]   File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.459037 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170]     from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.459072 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51170] ModuleNotFoundError: No module named 'django'
[Tue Jul 02 18:05:22.490159 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Target WSGI script '/home/raza/projects/sample/sample/wsgi.py' cannot be loaded as Python module.
[Tue Jul 02 18:05:22.490240 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] mod_wsgi (pid=12490): Exception occurred processing WSGI script '/home/raza/projects/sample/sample/wsgi.py'.
[Tue Jul 02 18:05:22.490297 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] Traceback (most recent call last):
[Tue Jul 02 18:05:22.490314 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172]   File "/home/raza/projects/sample/sample/wsgi.py", line 12, in <module>
[Tue Jul 02 18:05:22.490318 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172]     from django.core.wsgi import get_wsgi_application
[Tue Jul 02 18:05:22.490330 2019] [wsgi:error] [pid 12490] [remote 10.10.10.99:51172] ModuleNotFoundError: No module named 'django'

Looks like django is installed into python2 (which is used by your OS). python3 comes with pre-installed pip3 package installer. So use "sudo pip3 install django" command to install django into python3 environment. pip will only install the packages into python2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM