ahupp / python-magic

A python wrapper for libmagic
Other
2.6k stars 280 forks source link

No module named 'magic' #264

Closed kaikiat closed 2 years ago

kaikiat commented 2 years ago

I am getting this error when I try to use Docker + python-magic in my project.

Screenshot 2022-04-08 at 7 25 51 PM

This is my Dockerfile. I have tried installing libmagic-dev and libmagic1libmagic1 and python3-magic but it does not seem to work.

FROM python:3.9

ADD . /

ENV TZ=Asia/Singapore

ARG CHROME_VERSION="99.0.4844.84-1"

RUN apt-get -y update
RUN wget --no-verbose -O /tmp/chrome.deb https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
  && apt install -y /tmp/chrome.deb \
  && rm /tmp/chrome.deb
RUN apt-get install -y tzdata 

RUN apt-get install -y libmagic-dev
RUN apt-get install -y libmagic1
RUN apt-get install python3-magic

WORKDIR /code

COPY . /code
RUN pip install -r /code/requirements.txt

CMD exec gunicorn --bind 0.0.0.0:5000 --workers 1 --worker-class uvicorn.workers.UvicornWorker --threads 2 app.main:app

And this is a portion of my requirements.txt

Screenshot 2022-04-08 at 7 29 11 PM

I am not sure if you have knowledge on this and any help will be greatly appreciated. Thank you!

ahupp commented 2 years ago

The python3.9 docker image ships its build of python in /usr/local/bin/python3. This is independent of the underlying distribution's copy of python in /usr/bin/python. When you install python3-magic it goes into /usr/lib/python3/dist-packages/ and is only visible to the distribution install. e.g, you can run /usr/bin/python3 and then import magic works.

The second issue is that your requirements.txt is specifying platform_system requirements that will exclude this docker image (which is linux). The third issue is that python-magic-bin is a fork of python-magic that includes binaries, but it's not the one in this repo.

So tl;dr you should:

  1. Only apt install libmagic1 in the container, you don't need libmagic1-dev or python3-magic
  2. Remove the platform_system requirements in your requirements.txt
  3. Put python-magic, not python-magic-bin in your requirements.txt
kaikiat commented 2 years ago

Thanks it works as expected !