Django mod_wsgi: ImportError: No module named 'home'

Django mod_wsgi: ImportError: No module named 'home'

我有一个 Django 项目,它使用开发服务器 (runserver) 运行没有问题,但在通过 Apache/WSGI 提供服务时失败了。

错误是 ImportError: No module named 'home',这是来自 Apache 错误日志的跟踪:

[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'

似乎错误出在 settings.INSTALLED_APPS,它正在寻找 home 应用程序。

我找不到 Django 文件的任何问题。我有一个带有 __init__.py 文件的 home 应用程序,一切都在开发服务器上运行。

这让我觉得这可能是一个 mod_wsgi 问题,所以我重新安装了 mod_wsgi 以确保我从我的 virtualenv 安装了正确的 python 版本,并且我更改了组所有权递归到 www-data 以确保我没有权限问题。

我确定我遗漏了一些简单的东西...但我没有看到它!

更新 1

这是 apache 虚拟主机设置:

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>

这里是apachemodwsgi.load:

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

这里是apachemodwsgi.conf:

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

根据要求,这是我手动导入时得到的 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'
>>>

这是 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#                                                                                                                                                      

更新 2

我按照指示添加了 Python-路径到 Damon 进程(以及 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

但我仍然遇到错误。这次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'

但是 is_spa module 在那里:

(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'
>>>

脚本是否必须位于 Django 项目中的特定位置?我的文件夹结构是/is_spa/is_spa/,其中第一个目录是项目,第二个是应用程序。我目前在 app 目录中有 WSGI 脚本,但我无法想象当我们明确设置 python-path 选项时这会很重要。

根本原因是 WSGIDamonProcess 需要在其选项中明确设置 python-path。它不继承全局值。

正如@GrahamDumpleton 指出的那样,改变

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

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

解决了问题。