Django仅在生产中没有名为“ PIL”的模块

时间:2019-03-08 16:56:23

标签: python django apache

尝试使用Django管理员上传图像时出现错误

  

ModuleNotFoundError,位于/ admin / home / image / 16 / change /

     

没有名为“ PIL”的模块

奇怪的是,仅当我使用apache运行Django站点时,当我使用sudo python3 manage.py runserver 0.0.0.0:80运行服务器时,它才可以正常工作。另外,当我为Django激活虚拟环境时,就可以运行import PIL了。当使用sudo python3 manage.py shell运行Django shell时,我也可以这样做。当我运行pip3 freezepip3 list时,该软件包也显示为已安装。

另一个说明,我无法在Django项目中运行wsgi.py文件/

我不确定会有什么帮助,但是这里有一些我认为可能很重要的文件。

apache conf文件:

<VirtualHost *:80>
  Servername www.ubspy.org
  Servername ubspy.org

  DocumentRoot /home/ubspy/django/

  WSGIScriptAlias / /home/ubspy/django/ubspy/wsgi.py

  ErrorLog /home/ubspy/django/error.log

  Alias /static/ /home/ubspy/django/static/
  Alias /media/ /home/ubspy/django/media/
  Alias /php/ /var/www/html/

  <Directory /home/ubspy/django/static/>
    Require all granted
  </Directory>

  <Directory /home/ubspy/django/media/>
    Require all granted
  </Directory>

  WSGIDaemonProcess ubspy.org python-home=/home/ubspy/django/env python-path=/home/ubspy/django/
  WSGIProcessGroup ubspy.org

  <Directory /home/ubspy/django/ubspy/>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

  <IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault "access plus 1 month"
  </IfModule>

</VirtualHost>

settings.py:

"""
Django settings for ubspy project.

Generated by 'django-admin startproject' using Django 2.0.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Static files (CSS, JavaScipt, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubspy/django/static/'
STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'favicon/'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
secretFile = open(os.path.join(BASE_DIR, 'ubspy/secret.dat'), 'r')
SECRET_KEY = secretFile.read()
secretFile.close()

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['192.168.1.16', 'ubspy.org', 'www.ubspy.org']

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home.apps.HomeConfig',
    'poem.apps.PoemConfig',
    'mewsicgen.apps.MewsicgenConfig',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'ubspy.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'ubspy.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

感谢您的帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

您需要安装Pillow lib。

pip install Pillow

有关Pillow

的更多详细信息

答案 1 :(得分:0)

如果您安装了不同版本的python,请检查apache是​​否使用python3运行脚本。

有时,如果仅为用户或​​环境安装了软件包,则在以其他用户或外部环境运行脚本时会出现此错误。据我所知,Apache是​​使用root用户以外的其他用户运行脚本的,因此可能会引起问题。