I get an error when trying to upload an image using the Django admin
ModuleNotFoundError at /admin/home/image/16/change/
No module named ‘PIL`
The strange thing is, this only appears when I run the Django site with apache, when I run the server using sudo python3 manage.py runserver 0.0.0.0:80
it works just fine. Additionally, when I activate the virtual environment I have for Django, I can run import PIL
just fine. I can also do this when running the Django shell with sudo python3 manage.py shell
. The package is also shown to be installed when I run pip3 freeze
and pip3 list
.
Another note, I can’t run the wsgi.py
file in my Django project/
I’m not sure what will help, but here are some files I think may be important.
apache conf file:
<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
Any help would be apprechiated, thank you!
2
Answers
You need to install Pillow lib.
More details about Pillow
if you have different versions of python installed, check that apache runs your script with python3.
sometimes if you installed a package only for a user or in an environment, when you run script form another user or outside environment you got this error. as far as I know, Apache runs the script with another user than root, so that can cause the problem.