简体   繁体   中英

Django mod_wsgi: ImportError: No module named 'home'

I have a Django project that runs without issue using the dev server (runserver), but fails when serving through Apache/WSGI.

  • Ubuntu 16.04
  • Python 3.5.2
  • Django 2.0.4
  • mod_wsgi 4.6.4

The error is ImportError: No module named 'home' and here's the trace from Apache's error log:

[Thu May 03 19:01:11.010623 2018] [mpm_event:notice] [pid 8568:tid 139727981401984] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.6.4 Python/3.5 configured -- resuming normal operations
[Thu May 03 19:01:11.010742 2018] [mpm_event:info] [pid 8568:tid 139727981401984] AH00490: Server built: 2018-04-18T14:53:04
[Thu May 03 19:01:11.010763 2018] [core:notice] [pid 8568:tid 139727981401984] AH00094: Command line: '/usr/sbin/apache2'
[Thu May 03 19:01:11.011684 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Python home /home/xxx/is_websites/webenv/.
[Thu May 03 19:01:11.011766 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Initializing Python.
[Thu May 03 19:01:11.014159 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Python home /home/xxx/is_websites/webenv/.
[Thu May 03 19:01:11.014238 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Initializing Python.
[Thu May 03 19:01:11.052639 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Attach interpreter ''.
[Thu May 03 19:01:11.054513 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Attach interpreter ''.
[Thu May 03 19:01:11.073009 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:11.075298 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:23.330027 2018] [wsgi:info] [pid 8572:tid 139727768385280] mod_wsgi (pid=8572): Create interpreter '127.0.1.1|'.
[Thu May 03 19:01:23.346866 2018] [wsgi:info] [pid 8572:tid 139727768385280] mod_wsgi (pid=8572): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:23.347805 2018] [wsgi:info] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572, process='', application='127.0.1.1|'): Loading Python script file '/home/xxx/is_websi$
[Thu May 03 19:01:23.665257 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572): Failed to exec Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Thu May 03 19:01:23.665326 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572): Exception occurred processing WSGI script '/home/xxx/is_websites/is_spa/is_spa/wsgi$
[Thu May 03 19:01:23.667315 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Thu May 03 19:01:23.667375 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/is_spa/is_spa/wsgi.py", line 16, in <module>
[Thu May 03 19:01:23.667386 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     application = get_wsgi_application()
[Thu May 03 19:01:23.667399 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/core/wsgi.py", line 12, in get_w$
[Thu May 03 19:01:23.667405 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     django.setup(set_prefix=False)
[Thu May 03 19:01:23.667416 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
[Thu May 03 19:01:23.667422 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     apps.populate(settings.INSTALLED_APPS)
[Thu May 03 19:01:23.667432 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/apps/registry.py", line 89, in p$
[Thu May 03 19:01:23.667439 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     app_config = AppConfig.create(entry)
[Thu May 03 19:01:23.667449 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/apps/config.py", line 90, in cre$
[Thu May 03 19:01:23.667455 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     module = import_module(entry)
[Thu May 03 19:01:23.667465 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Thu May 03 19:01:23.667471 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]     return _bootstrap._gcd_import(name[level:], package, level)
[Thu May 03 19:01:23.667481 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Thu May 03 19:01:23.667493 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Thu May 03 19:01:23.667504 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Thu May 03 19:01:23.667539 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] ImportError: No module named 'home'

It seems like the error is in settings.INSTALLED_APPS , which looks for a home app.

I can't find any issue with the Django files. I have a home app with an __init__.py file, and everything works on the dev server.

This makes me think it might be a mod_wsgi issue, so I reinstalled mod_wsgi to make sure I installed against the correct python version from my virtualenv, and I changed group ownership recursively to www-data to make sure I don't have permission issues.

I'm sure I'm missing something simple... but I don't see it!

Update 1

Here's the apache virtual host setup:

LogLevel info

WSGIScriptAlias / /home/xxx/is_websites/is_spa/is_spa/wsgi.py

WSGIDaemonProcess xxx_website processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup xxx_website

<Directory /home/xxx/is_websites/is_spa/is_spa/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Here is apache mod wsgi.load :

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

Here is apache mod wsgi.conf :

WSGIPythonHome /home/xxx/is_websites/webenv/
WSGIPythonPath /home/xxx/is_websites/is_spa/

As requested, here's what I get when manually importing home :

(webenv) xxx@django-wagtail-postgres:~/is_websites/is_spa$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import home
>>> home.__file__
'/home/xxx/is_websites/is_spa/home/__init__.py'
>>>

And here's the output from wsgi make install :

(webenv) root@django-wagtail-postgres:/home/xxx/mod_wsgi-4.6.4# make install                                                                                                                                         
/usr/bin/apxs2 -i -S LIBEXECDIR=/usr/lib/apache2/modules -n 'mod_wsgi' src/server/mod_wsgi.la                                                                                                                        
/usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' src/server/mod_wsgi.la /usr/lib/apache2/modules                                                                                    
/usr/share/apr-1.0/build/libtool --mode=install install src/server/mod_wsgi.la /usr/lib/apache2/modules/                                                                                                             
libtool: install: install src/server/.libs/mod_wsgi.so /usr/lib/apache2/modules/mod_wsgi.so                                                                                                                          
libtool: install: install src/server/.libs/mod_wsgi.lai /usr/lib/apache2/modules/mod_wsgi.la                                                                                                                         
libtool: finish: PATH="/home/xxx/is_websites/webenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/sbin" ldconfig -n /usr/lib/apache2/modules                         
----------------------------------------------------------------------                                                                                                                                               
Libraries have been installed in:                                                                                                                                                                                    
   /usr/lib/apache2/modules                                                                                                                                                                                          

If you ever happen to want to link against installed libraries                                                                                                                                                       
in a given directory, LIBDIR, you must either use libtool, and                                                                                                                                                       
specify the full pathname of the library, or use the '-LLIBDIR'                                                                                                                                                      
flag during linking and do at least one of the following:                                                                                                                                                            
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable                                                                                                                                                        
     during execution                                                                                                                                                                                                
   - add LIBDIR to the 'LD_RUN_PATH' environment variable                                                                                                                                                            
     during linking                                                                                                                                                                                                  
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag                                                                                                                                                                     
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'                                                                                                                                                  

See any operating system documentation about shared libraries for                                                                                                                                                    
more information, such as the ld(1) and ld.so(8) manual pages.                                                                                                                                                       
----------------------------------------------------------------------                                                                                                                                               
chmod 644 /usr/lib/apache2/modules/mod_wsgi.so                                                                                                                                                                       
(webenv) root@django-wagtail-postgres:/home/xxx/mod_wsgi-4.6.4#                                                                                                                                                      

Update 2

I added Python-path to the Damon process as instructed (as well as python-home): WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP} python-home=/home/xxx/is_websites/webenv/ python-path=/home/xxx/is_websites/is_spa

But I still get an error. This time ImportError: No module named 'is_spa.is_spa'

[Mon May 07 14:39:15.769405 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Cleanup interpreter ''.
[Mon May 07 14:39:15.771668 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Cleanup interpreter ''.
[Mon May 07 14:39:15.785445 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Terminating Python.
[Mon May 07 14:39:15.786996 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Terminating Python.
[Mon May 07 14:39:15.801152 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Python has shutdown.
[Mon May 07 14:39:15.801201 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Exiting process 'website'.
[Mon May 07 14:39:15.802813 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Python has shutdown.
[Mon May 07 14:39:15.802837 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Exiting process 'website'.
[Mon May 07 14:39:16.736463 2018] [mpm_event:notice] [pid 3926:tid 140137508214656] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.6.4 Python/3.5 configured -- resuming normal operations
[Mon May 07 14:39:16.736597 2018] [core:notice] [pid 3926:tid 140137508214656] AH00094: Command line: '/usr/sbin/apache2'
[Mon May 07 14:39:16.827772 2018] [wsgi:info] [pid 3929:tid 140137508214656] mod_wsgi (pid=3929): Attach interpreter ''.
[Mon May 07 14:39:16.832887 2018] [wsgi:info] [pid 3928:tid 140137508214656] mod_wsgi (pid=3928): Attach interpreter ''.
[Mon May 07 14:39:16.871324 2018] [wsgi:info] [pid 3929:tid 140137508214656] mod_wsgi (pid=3929): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:16.876253 2018] [wsgi:info] [pid 3928:tid 140137508214656] mod_wsgi (pid=3928): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:19.891511 2018] [wsgi:info] [pid 3929:tid 140137402570496] mod_wsgi (pid=3929): Create interpreter '127.0.1.1|'.
[Mon May 07 14:39:19.908227 2018] [wsgi:info] [pid 3929:tid 140137402570496] mod_wsgi (pid=3929): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:19.909016 2018] [wsgi:info] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929, process='website', application='127.0.1.1|'): Loading Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.262585 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929): Failed to exec Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.262676 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929): Exception occurred processing WSGI script '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.263610 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Mon May 07 14:39:20.263692 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/is_spa/is_spa/wsgi.py", line 16, in <module>
[Mon May 07 14:39:20.263702 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     application = get_wsgi_application()
[Mon May 07 14:39:20.263715 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Mon May 07 14:39:20.263721 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     django.setup(set_prefix=False)
[Mon May 07 14:39:20.263732 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/__init__.py", line 19, in setup
[Mon May 07 14:39:20.263738 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon May 07 14:39:20.263749 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
[Mon May 07 14:39:20.263755 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     self._setup(name)
[Mon May 07 14:39:20.263765 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
[Mon May 07 14:39:20.263783 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     self._wrapped = Settings(settings_module)
[Mon May 07 14:39:20.263795 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 106, in __init__
[Mon May 07 14:39:20.263801 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon May 07 14:39:20.263811 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "/home/xxx/is_websites/webenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Mon May 07 14:39:20.263818 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]     return _bootstrap._gcd_import(name[level:], package, level)
[Mon May 07 14:39:20.263828 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263840 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263851 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon May 07 14:39:20.263863 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon May 07 14:39:20.263875 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263886 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263898 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon May 07 14:39:20.263909 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon May 07 14:39:20.263921 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263932 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263944 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Mon May 07 14:39:20.263970 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] ImportError: No module named 'is_spa.is_spa'

But the is_spa module is there:

(webenv) xxx@django-wagtail-postgres:~/is_websites/is_spa$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import is_spa
>>> is_spa.__file__
'/home/wag/is_websites/is_spa/is_spa/__init__.py'
>>>

Is there a particular location that the script must be located in a Django project? My folder structure is /is_spa/is_spa/ , where the first directory is the project, and the second is the app. I currently have the WSGI script in the app directory, but I can't imagine that would matter when we explicitly set the python-path option.

The root cause is that the WSGIDamonProcess needs the python-path to be set explicitly in it's options. It does not inherit the global value.

As @GrahamDumpleton pointed out, changing

WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP}

to

WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP} python-path=/home/xxx/is_websites/is_spa

solved the issue.

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