Apache 2在Django项目目录下找不到python软件包

时间:2019-03-14 13:03:21

标签: django python-3.x apache docker

我正在Ubuntu Docker容器中运行Apache2和Django。 Apache可以找到并正在尝试运行Django项目,但找不到在项目目录下包含的python软件包。它返回一个导入错误。见附图。

Django error message

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 / 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')

1 个答案:

答案 0 :(得分:0)

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

我不想在虚拟环境下运行此程序,因为Web应用程序是在此Docker容器中唯一运行的程序。因此,我在基本映像中安装了所有必需的python库软件包。我的Dockerfile,Requirements.txt和Docker组成文件如下。

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模块的问题。为了使所有部分互相交谈,我们花费了大量的精力,因此希望这可以帮助其他有类似需求的人。