【发布时间】:2019-08-05 09:24:55
【问题描述】:
我在 Ubuntu Docker 容器中运行 Apache2 和 Django。 Apache 可以找到并尝试运行 Django 项目,但找不到项目目录下包含的 python 包。它返回一个导入错误。见附图。
ImportError at /
Missing required dependencies ['numpy']
Request Method: GET
Request URL: http://localhost:8005/
Django Version: 2.1.7
Exception Type: ImportError
Exception Value:
Missing required dependencies ['numpy']
Exception Location: /var/www/html/django_demo_app/INDmain/Lib/site-packages/pandas/__init__.py in <module>, line 19
Python Executable: /usr/bin/python3
Python Version: 3.6.7
Python Path:
['/var/www/html/django_demo_app/INDmain',
'/var/www/html/django_demo_app/INDmain/Lib/site-packages',
'/var/www/html/django_demo_app/INDmain/Scripts',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages',
'/var/www/html/django_demo_app/INDmain',
'/var/www/html/django_demo_app/INDmain',
'/var/www/html/django_demo_app/INDmain/main']
当 Apache/Django 试图在 /var/www/html/django_demo_app/INDmain/main/Lib/site-packages 下找到请求包时,我遇到了同样的问题,所以我将目录添加到路径中,发现 Apache/Django包裹。现在它正在查找 pandas 包,但找不到位于同一目录中的依赖 numpy 包。什么可能导致 Apache 和/或 Django 看不到这些包?
项目布局
INDmain
--> Include
--> INDmain
--> Lib
--> main
--> Scripts
--> tcl
db.sqlite3
manage.py
我的配置/设置文件如下。为简洁起见,未显示常用设置。
Apache .conf 文件
WSGIPythonPath /var/www/html/django_demo_app/INDmain
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/django_demo_app
Alias /static "/var/www/html/django_demo_app/INDmain/main/static"
WSGIDaemonProcess INDmain python-path=/var/www/html/django_demo_app/INDmain:/var/www/html/django_demo_app/INDmain/Lib/site-packages:/var/www/html/django_demo_app/INDmain/Scripts
WSGIProcessGroup INDmain
WSGIScriptAlias / /var/www/html/django_demo_app/INDmain/main/wsgi.py
</VirtualHost>
settings.py
import sys
sys.path.append('/var/www/html/django_demo_app/INDmain')
ALLOWED_HOSTS = ['localhost']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'main.apps.MainConfig',
]
ROOT_URLCONF = 'INDmain.urls'
WSGI_APPLICATION = 'main.wsgi.application'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.abspath(os.path.join(BASE_DIR, "static")),
]
wsgi.py
"""WSGI config for INDmain project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/"""
import os
from django.core.wsgi import get_wsgi_application
import sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'INDmain.settings')
application = get_wsgi_application()
sys.path.append('/var/www/html/django_demo_app/INDmain')
sys.path.append('/var/www/html/django_demo_app/INDmain/main')
【问题讨论】:
-
在 Django 的deployment docs 中,它建议使用
python-home来指定虚拟环境,而不是将其添加到python-path。 -
顺便说一句,我会谨慎地将
INDmain和INDmain/main添加到python 路径中。这意味着一些模块可以从两个不同的地方导入(例如main.foo和foo),这可能会导致奇怪的行为。 -
添加了守护进程模式的python路径,现在网站只是挂起......不发送响应。
WSGIDaemonProcess INDmain python-home=/var/www/html/django_demo_app/INDmain python-path=/var/www/html/django_demo_ap p/INDmain WSGIProcessGroup INDmain WSGIScriptAlias / /var/www/html/django_demo_app/INDmain/main/wsgi.py process-group=INDmain -
@Alasdair 我已经添加了上面的项目布局。
-
Lib是什么,你是如何创建它的?也许你想要python-home=/var/www/html/django_demo_app/INDmain/Lib。
标签: django python-3.x apache docker