[英]Django Python mod_wsgi: ImportError: No module named 'django'
[英]Django mod_wsgi: ImportError: No module named 'home'
我有一個使用開發服務器(runserver)可以正常運行的Django項目,但是通過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
以確保沒有權限問題。
我確定我缺少一些簡單的東西...但是我看不到!
這是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>
這是apache mod wsgi.load
:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
這是apache mod wsgi.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#
我按照指示(以及python-home)將Python路徑添加到了Damon進程中: 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
模塊在那里:
(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/
,其中第一個目錄是項目,第二個目錄是應用程序。 我目前在應用程序目錄中有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
解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.