Search code examples
pythondockerpipwkhtmltopdf

Pip not found error when installing wkhtmltopdf while building Docker image


When I try to build the image I get:

Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip._internal.cli.main'

This is my dockerfile:

FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

RUN pip install python3-wkhtmltopdf
RUN apt-get update
RUN apt-get install -y wkhtmltopdf
COPY . .
CMD [ "python3", "app.py"]

What am I missing here?


Solution

  • It looks like you are actually trying to install py3-wkhtmltopdf. To do this, you can just leverage python -m pip and install the package directly:

    FROM python:3.8-slim-buster
    WORKDIR /app
    
    # following their linux installation instructions
    RUN apt-get update && apt-get install -y \
      xvfb \
      xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic \
      wkhtmltopdf
    
    COPY requirements.txt requirements.txt
    RUN python -m pip install -r requirements.txt
    COPY . .
    CMD [ "python", "app.py"]
    

    Where requirements.txt might look like:

    py3-wkhtmltopdf