ModuleNotFoundError:Apache,Bitnami,Django应用无法从虚拟环境导入

时间:2020-06-19 18:20:14

标签: django apache mod-wsgi bitnami

我正在尝试第一次将非常简单的Django应用部署在AWS Lightsail实例上。我预配置了一个用bitnami和django设置的AWS Apache实例,并成功地遵循了tutorial,但没有虚拟环境,但是此后花了很多时间尝试用虚拟环境添加我自己的项目。

大多数事情似乎都正确设置,并且我可以很好地启动Apache,只能通过浏览器导航到我的URL,并在错误日志中找到ModuleNotFoundError: No module named 'requests'错误。这是我的代码中所有pip安装的模块的首次导入,因此建议我的虚拟环境尚未将其放入我的PYTHONPATH中。我只是通过硬编码站点软件包的路径并在我的wsgi.py文件中使用sys.path.append(...)附加路径来找到一种解决方案,但我认为这不是用Django应用程序配置am_wsgi的正确方法,并且担心以后会导致意外问题。

服务器上的所有django项目都位于/opt/bitnami/apps/django/django-projects/。在教程之后,该目录包含项目“ Project”和“ tutorial”。为了从这些项目中分离出requirements.txt文件和虚拟环境env,我决定在自己的文件夹中找到我的项目(一个非常简单的种族平等相关应用程序)。我怀疑此文件夹结构对我的问题至关重要:

django-projects/
     Project/
     tutorial/
     re_app/
          env/  # this is the virtual environmnent
          requirements.txt
          racial_equity_project/
               conf/
                    httpd-app.conf
                    httpd-prefix.conf
               db.sqlite3
               manage.py
               racial_equity_app/
                    urls.py
                    views.py
                    ...
               racial_equity_project/
                    settings.py
                    urls.py
                    wsgi.py
                    ...

我希望这个httpd-app.conf文件可以工作。 python-home声明似乎与docs一致。

下面的相关文件和堆栈跟踪:

httpd-app.conf

<IfDefine_DJANGOSTACK_LOADED>
      Define IS_DJANGOSTACK_LOADED
      WSGIDaemonProcess wsgi-djangostack   processes=2 threads=15    display-name=%{GROUP} python-home=/opt/bitnami/apps/django/django_projects/re_app/env
</IfDefine>

<Directory "/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/racial_equity_project">
    Options +MultiViews
    AllowOverride All
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>

    WSGIProcessGroup wsgi-djangostack

    WSGIApplicationGroup %{GLOBAL}
</Directory>

Alias /racial_equity_project/static "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static"
WSGIScriptAlias /racial_equity_project '/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/racial_equity_project/wsgi.py'

wsgi.py

import os
import sys
sys.path.append('/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project')
sys.path.append('/opt/bitnami/apps/django/django_projects/re_app/env/lib/python3.7/site-packages')  # this is what solved my problem, but is there a better solution?
os.environ.setdefault("PYTHON_EGG_CACHE", "/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/egg_cache")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "racial_equity_project.settings")
exec(open('/etc/racial_equity_project-production-envvars.py').read())
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

来自less /opt/bitnami/apache2/logs/error_log的跟踪

[Fri Jun 19 16:43:17.041048 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118] Traceback (most recent call last):, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041112 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/core/handlers/exception.py", line 34, in inner, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041117 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     response = get_response(request), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041123 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/utils/deprecation.py", line 94, in __call__, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041127 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     response = response or self.get_response(request), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041133 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/core/handlers/exception.py", line 36, in inner, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041137 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     response = response_for_exception(request, exc), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041143 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/core/handlers/exception.py", line 90, in response_for_exception, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041147 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info()), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041153 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/core/handlers/exception.py", line 128, in handle_uncaught_exception, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041158 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     callback, param_dict = resolver.resolve_error_handler(500), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041163 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/urls/resolvers.py", line 597, in resolve_error_handler, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041168 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     callback = getattr(self.urlconf_module, 'handler%s' % view_type, None), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041173 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/utils/functional.py", line 80, in __get__, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041177 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     res = instance.__dict__[self.name] = self.func(instance), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041183 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/urls/resolvers.py", line 577, in urlconf_module, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041187 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     return import_module(self.urlconf_name), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041193 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/python/lib/python3.7/importlib/__init__.py", line 127, in import_module, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041199 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     return _bootstrap._gcd_import(name[level:], package, level), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041205 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041210 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041216 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041222 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041227 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap_external>", line 728, in exec_module, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041233 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041239 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/racial_equity_project/urls.py", line 22, in <module>, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041243 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     path('', include('racial_equity_app.urls')),, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041248 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/urls/conf.py", line 34, in include, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041252 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     urlconf_module = import_module(urlconf_module), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041258 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/python/lib/python3.7/importlib/__init__.py", line 127, in import_module, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041262 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     return _bootstrap._gcd_import(name[level:], package, level), referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041267 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041273 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041278 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041284 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041290 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap_external>", line 728, in exec_module, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041295 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041301 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/racial_equity_app/urls.py", line 3, in <module>, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041305 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     from . import views, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041313 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]   File "/opt/bitnami/apps/django/django_projects/re_app/racial_equity_project/racial_equity_app/views.py", line 6, in <module>, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041317 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118]     import requests, referer: http://3.20.187.50/
[Fri Jun 19 16:43:17.041328 2020] [wsgi:error] [pid 18658] [client 73.222.180.141:55118] ModuleNotFoundError: No module named 'requests', referer: http://3.20.187.50/

您知道为什么此配置未按预期使用我的虚拟环境吗?

0 个答案:

没有答案