【问题标题】:Apache 2 can't find python packages under Django project directoryApache 2 在 Django 项目目录下找不到 python 包
【发布时间】: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
  • 顺便说一句,我会谨慎地将INDmainINDmain/main 添加到python 路径中。这意味着一些模块可以从两个不同的地方导入(例如main.foofoo),这可能会导致奇怪的行为。
  • 添加了守护进程模式的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


【解决方案1】:

所以回答我自己的问题...

我不想在虚拟环境下运行它,因为 Web 应用程序是唯一在这个 Docker 容器中运行的东西。因此,我在基础镜像中安装了所有需要的 python 库包。我的 Dockerfile、requirements.txt 和 Docker compose 文件如下。

Dockerfile

FROM ubuntu

RUN apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y && apt-get autoremove -y && apt-get autoclean -y
RUN apt-get install -y apt-utils vim curl apache2 apache2-utils
RUN apt-get -y install python3 libapache2-mod-wsgi-py3
RUN ln /usr/bin/python3 /usr/bin/python
RUN apt-get -y install python3-pip
RUN ln /usr/bin/pip3 /usr/bin/pip
ADD ./requirements.txt .
RUN pip install -r requirements.txt

ADD ./web_site.conf /etc/apache2/sites-enabled/000-default.conf
ADD ./INDmain /var/www/html/INDapp/INDmain
EXPOSE 80 3500
CMD ["apache2ctl", "-D", "FOREGROUND"]

requirements.txt

django
ptvsd
requests
pandas
openpyxl
xlrd

docker-compose.yml

version: "2"

services: 
  ind-web-app-ubuntu:
    image: ind-web-app-ubuntu
    container_name: ind-web-app-ubuntu
    ports:
      - '8005:80'
      - '3500:3500'
      - '8006:81'
    restart: always

我的结束项目布局。

INDmain
--> INDmain
--> main
db.sqlite3
manage.py

我的配置/设置文件。为简洁起见,未显示常用设置。

Apache .conf 文件

WSGIPythonPath /var/www/html/INDapp/INDmain

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/INDapp

    Alias /static "/var/www/html/INDapp/INDmain/main/static"

    WSGIScriptAlias / /var/www/html/INDapp/INDmain/INDmain/wsgi.py

</VirtualHost>

settings.py

import sys

sys.path.append('/var/www/html/INDapp/INDmain/main')

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 = 'INDmain.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()

在现有的 python 环境中安装 python 包解决了在 Django 和 Apache 的底层目录中找不到 python 模块的问题。让所有部分相互交流需要付出巨大的努力,因此希望这将有助于其他有类似需求的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 2012-03-20
    • 2021-01-09
    • 2019-07-02
    • 2018-10-26
    • 2013-03-14
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多