0

I'm trying to run makemigrations using "docker-compose run makemigrations" after adding pillow to my project but it gives an error saying the "ModuleNotFoundError: No module named 'PIL'". I'm attempting to use pillow to compress uploaded images.

Pillow has been added to settings.py and requirements.txt. I have also tried rebuilding the docker image.

It also may be worth noting that the error doesn't appear when I run the server using "docker-compose up"

Below are snippets of code:

docker-compose.yml

version: "3.9"
   
services:
  db:
    image: postgres
    ports:
      - "5432"
    volumes:
      - ./data/db:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
  createsuperuser:
    build: .
    profiles: ["createsuperuser"]
    command: python manage.py createsuperuser
    volumes:
      - .:/code
    ports:
      - "8001"
    depends_on:
      - db    
  makemigrations:
    build: .
    profiles: ["makemigrations"]
    command: python manage.py makemigrations
    volumes:
     - .:/code
    ports:
      - "8002"
    depends_on:
      - db
  migrate:
    build: .
    profiles: ["migrate"]
    command: python manage.py migrate
    volumes:
      - .:/code
    ports:
      - "8003"
    depends_on:
      - db

models.py

from PIL import Image
...
class PropertyPhoto(models.Model):
    property = models.ForeignKey(Property, on_delete=models.CASCADE, null=True, related_name="photos")
    photo = models.ImageField(upload_to=f"static/properties/{property}/photos/", null=True, default=None)

    def save(self, *args, **kwargs):
        instance = super(PropertyPhoto, self).save(*args, **kwargs)
        image = Image.open(instance.photo.path)
        image.save(instance.photo.path, quality=80, optimize=True)
        return instance 

Error when running makemigrations

Traceback (most recent call last):
  File "/code/manage.py", line 22, in <module>
    main()
  File "/code/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/usr/local/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.10/site-packages/django/apps/config.py", line 228, in create
    import_module(entry)
  File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'PIL'

Dockerfile

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
BamBoo22
  • 1
  • 1
  • 2
    It's quite unusual that you are running all the management commends as a docker-compose service. But what would be very interesting here is your `Dockerfile`. – Klaus D. May 28 '22 at 11:45
  • @KlausD. I added the dockerfile. This is my first time using docker for a project, how would one usually run management commands? – BamBoo22 May 28 '22 at 12:02
  • Does this answer your question? [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose) – Ivan Starostin May 28 '22 at 15:42

0 Answers0