簡體   English   中英

Apache2 和 Django - [wsgi:error] ImportError: cannot import name 'get_version' -> No module named 'django'

[英]Apache2 and Django - [wsgi:error] ImportError: cannot import name 'get_version' -> No module named 'django'

我已經在 Django 中建立了一個基本站點,並嘗試使用 Apache 2 為其提供服務,但是在嘗試時出現錯誤:

[Tue Jul 07 08:03:50.274206 2020] [mpm_event:notice] [pid 21614:tid 140492525521856] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Tue Jul 07 08:03:50.274292 2020] [core:notice] [pid 21614:tid 140492525521856] AH00094: Command line: '/usr/sbin/apache2'
[Tue Jul 07 08:03:55.841352 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Target WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py' cannot be loaded as Python module.
[Tue Jul 07 08:03:55.841401 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] mod_wsgi (pid=21616): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 08:03:55.841626 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 08:03:55.841655 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]   File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 08:03:55.841661 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]     from django.core.wsgi import get_wsgi_application
[Tue Jul 07 08:03:55.841669 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]   File "/home/ubuntu/Django/env/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
[Tue Jul 07 08:03:55.841745 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport]     from django.utils.version import get_version
[Tue Jul 07 08:03:55.841768 2020] [wsgi:error] [pid 21616:tid 140492373808896] [remote myip:myport] ImportError: cannot import name 'get_version'

我安裝的版本是:Apache/2.4.29 (Ubuntu)、mod_wsgi/4.5.17、Python/3.6、Django/2.2.10

我的 apache2 的 djangoproject.conf 是:

<VirtualHost *:80> 
 ServerAdmin admin@customspirit.co
 ServerName customspirit.co 
 DocumentRoot /home/ubuntu/Django 

 <Directory /home/ubuntu/Django>
   Require all granted
 </Directory>  

 Alias /static /home/ubuntu/Django/static_in_env
 
 <Directory /home/ubuntu/Django/static_in_env> 
  Options FollowSymLinks
  Order allow,deny
  Allow from all
  Require all granted 
 </Directory> 

 Alias /media /home/ubuntu/Django/media_root
 
 <Directory /home/ubuntu/Django/media_root> 
  Options FollowSymLinks
  Order allow,deny
  Allow from all
  Require all granted 
 </Directory> 

 
 # adjust the following line to match your Python path 
 WSGIDaemonProcess customspiritco python-home=/home/ubuntu/Django/env python-path=/home/ubuntu/Django
 WSGIProcessGroup customspiritco
 WSGIScriptAlias / /home/ubuntu/Django/customspiritco/wsgi.py 
 
 <Directory /home/ubuntu/Django/custompspiritco>
  <Files wsgi.py>
   Require all granted
  </Files>
 </Directory>

</VirtualHost>

錯誤所在的 init.py 文件如下所示:

from django.utils.version import get_version

VERSION = (2, 2, 10, 'final', 0)

__version__ = get_version(VERSION)


def setup(set_prefix=True):
    """
    Configure the settings (this happens as a side effect of accessing the
    first setting), configure logging and populate the app registry.
    Set the thread-local urlresolvers script prefix if `set_prefix` is True.
    """
    from django.apps import apps
    from django.conf import settings
    from django.urls import set_script_prefix
    from django.utils.log import configure_logging

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    if set_prefix:
        set_script_prefix(
            '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME
        )
    apps.populate(settings.INSTALLED_APPS)

wgsi.py 文件如下所示:

 import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'customspiritco.settings.production')

application = get_wsgi_application()

每次我嘗試加載任何內容時都會收到內部服務器錯誤(500),並且錯誤在日志中重復出現。

我現在已經將 Django 重新安裝到 2.2.14 版本,這已經解決了這個問題並帶來了一個新問題

[Tue Jul 07 09:03:28.291015 2020] [wsgi:error] [pid 25208:tid 139979472283392] [remote myip:myport] ModuleNotFoundError: No module named 'django'

在為我的 python 版本編譯 mod_wgsi 后,此錯誤現在已替換為:

[Tue Jul 07 10:43:39.699154 2020] [wsgi:info] [pid 1305:tid 140703057624000] mod_wsgi (pid=1305): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:43:39.699699 2020] [mpm_event:debug] [pid 1305:tid 140702956263168] event.c(2218): AH02471: start_threads: Using epoll (wakeable)
[Tue Jul 07 10:44:08.714222 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714265 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.714312 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of Require all granted: granted
[Tue Jul 07 10:44:08.714319 2020] [authz_core:debug] [pid 1304:tid 140702826608384] mod_authz_core.c(809): [client myip:myport] AH01626: authorization result of <RequireAny>: granted
[Tue Jul 07 10:44:08.727681 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Create interpreter 'customspirit.co|'.
[Tue Jul 07 10:44:08.740572 2020] [wsgi:info] [pid 1303:tid 140702905882368] mod_wsgi (pid=1303): Adding '/home/ubuntu/Django' to path.
[Tue Jul 07 10:44:08.740929 2020] [wsgi:info] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303, process='customspiritco', application='customspirit.co|'): Loading Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741202 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Failed to exec Python script file '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741229 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] mod_wsgi (pid=1303): Exception occurred processing WSGI script '/home/ubuntu/Django/customspiritco/wsgi.py'.
[Tue Jul 07 10:44:08.741396 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] Traceback (most recent call last):
[Tue Jul 07 10:44:08.741421 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport]   File "/home/ubuntu/Django/customspiritco/wsgi.py", line 3, in <module>
[Tue Jul 07 10:44:08.741427 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport]     from django.core.wsgi import get_wsgi_application
[Tue Jul 07 10:44:08.741441 2020] [wsgi:error] [pid 1303:tid 140702905882368] [remote myip:myport] ModuleNotFoundError: No module named 'django'

經過數小時的挫折后,通過創建用戶 ubuntu 擁有的新虛擬環境,一切都得到了解決。 以前我一定是使用 sudo 完成的,這導致找不到 Django 模塊的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM