1

I am trying to create a docker container to always run mypy in the same environment. The library I want to run mypy on has multiple dependencies, so I have to install those first and have access to them as I am evaluating the library that was passed. This is what it currently looks like, in this example I am only installing scipy as an external dependency, later I would install a regular requirements.txt file instead:

FROM ubuntu:22.04 as builder

RUN apt-get update && apt-get install -y \
        bc \
        gcc \
        musl-dev \
        python3-pip \
        python3 \
        python3-dev

RUN python3.10 -m pip install --no-cache-dir --no-compile scipy && \
    python3.10 -m pip install --no-cache-dir --no-compile mypy


FROM ubuntu:22.04 as production

RUN apt-get update && apt-get install -y \
        python3 \

COPY --from=builder /usr/local/lib/python3.10/dist-packages /usr/local/lib/python3.10/dist-packages
COPY --from=builder /usr/local/bin/mypy /usr/local/bin/mypy

WORKDIR /data
ENTRYPOINT ["python3.10", "-m", "mypy"]

I install and run my container with

docker build -t my-package-mypy . && docker run -v $(pwd):/data my-package-mypy main.py

Where main.py is a simple one line script that only imports scipy.

This returns the following output:

main.py:1: error: Cannot find implementation or library stub for module named "scipy"  [import]
main.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.10/dist-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/usr/local/lib/python3.10/dist-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 95, in main
  File "mypy/main.py", line 174, in run_build
  File "mypy/build.py", line 193, in build
  File "mypy/build.py", line 302, in _build
  File "mypy/build.py", line 3579, in record_missing_stub_packages
PermissionError: [Errno 13] Permission denied: '.mypy_cache/missing_stubs'

Where most importantly, the first line says that it cannot find the installation for scipy even though it was installed alongside mypy. How can I adjust my dockerfile to get it to work as described?

Yes
  • 339
  • 3
  • 19
  • It may not be safe to try to copy individual packages between build stages like that. [Activate python virtualenv in Dockerfile](https://stackoverflow.com/questions/48561981/activate-python-virtualenv-in-dockerfile) includes a [recipe](/a/48562835) for copying an entire virtual environment between build stages (requires that the two have _identical_ Python installations); is this a better approach for you? – David Maze Feb 02 '23 at 15:21
  • why you dont just intall the package? – Lucas M. Uriarte Feb 02 '23 at 15:40
  • @LucasM.Uriarte you mean at https://hub.docker.com/r/cytopia/mypy? This only gives me the "Cannot find implementation or library stub for module" error for every external library I am using – Yes Feb 02 '23 at 15:42
  • @DavidMaze I tried to rewrite my dockerfile according to the recipe you posted, but I get the error message `No module named mypy`, do you have an idea why this problem occurs? https://pastebin.com/hUbdWid5 – Yes Feb 02 '23 at 15:46
  • I mean why you don't you install the library instead of copying ? – Lucas M. Uriarte Feb 02 '23 at 15:48

0 Answers0