【问题标题】:Django No module named 'PIL' only in productionDjango 仅在生产中没有名为“PIL”的模块
【发布时间】:2019-03-08 16:56:23
【问题描述】:

尝试使用 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 配置文件:

<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

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 可能不是问题,但你不应该用 sudo 做任何这些事情。
  • @Daniel 使用 sudo 实际上会阻止任何工作吗?
  • @DanielRoseman 我只运行 sudo 来访问我拥有的秘密数据文件,并在使用 runserver 时访问端口 80。如果您认为我应该在没有 sudo 的情况下运行它,我当然可以尝试。
  • 不得不说您的python-path 相对于python-home 很奇怪:通常virtualenv 不在项目目录中。你确定manage.py/django/env 在同一级别的 /django 目录中吗?
  • @dirkgroten 是的,env 文件夹与 manage.py 位于同一目录中

标签: python django apache


【解决方案1】:

您需要安装 Pillow 库。

pip install Pillow

更多关于Pillow的详情

【讨论】:

  • 正如我所说,当我运行python3sudo python3 manage.py shell 时,我可以很好地导入枕头。当我运行pip3 freeze 时,它显示枕头已安装。
  • 所以,如果你已经安装了 Pillow 库。还有一个问题。当您通过 Apache 运行代码库时,它找不到 Pillow 库。 Django 项目没有运行安装 Pillow 库的实际 virtualenv
  • 有什么想法可以解决问题吗?我在上面找不到任何东西。
  • 我对 Apache 没有任何概念。我一直使用 Gunicorn,Nginx。对不起。但是您可以跟踪调试问题。激活您的虚拟环境并运行项目。如果项目正在运行。那么当你要通过 apache 运行项目时它应该有问题。在这种情况下,我不知道 apache。
【解决方案2】:

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

有时,如果您仅为用户或​​在环境中安装软件包,当您从另一个用户或外部环境运行脚本时,您会收到此错误。据我所知,Apache 使用 root 以外的其他用户运行脚本,因此可能会导致问题。

【讨论】:

  • 我安装的唯一一个wsgi版本需要python3,所以我觉得不是这样。另外,如果是错误的版本,脚本中的某个地方不会有其他错误吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
相关资源
最近更新 更多