Django 没有命名的模块 'PIL' 仅在生产中



我在尝试使用django admin上传映像时会出现错误

modulenotfoundError at/admin/home/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 project/

中运行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

任何帮助都将得到批准,谢谢!

您需要安装枕头lib。

pip install Pillow

有关枕头的更多详细信息

如果已安装了Python的不同版本,请检查Apache是否使用Python3运行您的脚本。

有时,如果您仅为用户或环境安装软件包,则在运行脚本形成另一个用户或外部环境时,您会遇到此错误。据我所知,Apache与另一个用户一起运行脚本,这可能会导致问题。

最新更新