0

I'm exploring Python through building its Docker image. I want to use the scipy library but I'm not able to import it for some reason. Here are the imports for my __init.py__ file:

import numpy as np
import scipy.stats as sp
from flask import Flask, g
from flask_restful import Resource, Api, reqparse 

My Dockerfile:

FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./run.py" ]

requirements.txt:

numpy==1.19.0
scipy==0.15.1
Flask==0.12.2
markdown==2.6.11
flask-restful==0.3.6

And finally my docker-compose.yml:

version: '3.4'

services:
        calculator:
                build: .
                volumes:
                        - .:/usr/src/app
                ports:
                        - 5000:80

Now when I do a docker-compose up this is the error:

calculator  | Traceback (most recent call last):
calculator  |   File "./run.py", line 1, in <module>
calculator  |     from probability_calculator import app
calculator  |   File "/usr/src/app/probability_calculator/__init__.py", line 2, in <module>
calculator  |     import scipy.stats as sp
calculator  | ModuleNotFoundError: No module named 'scipy'

I've had a look here but adding those lines to the Dockerfile still doesn't seem to work.

clattenburg cake
  • 1,096
  • 3
  • 19
  • 40
  • 1
    @Dan apologies, have just added that file. – clattenburg cake Jun 23 '20 at 14:56
  • What are the logs saying when this line runs `RUN pip install --no-cache-dir -r requirements.txt`? Seems like scipy isn't getting installed but numpy is, the logs should indicate this – Dan Jun 23 '20 at 14:58
  • 1
    btw that is a very old version of scipy, maybe use a newer one? – Dan Jun 23 '20 at 14:59
  • thanks @Dan that's actually worked after I upgraded it to 1.4.1, that'll teach me to straight copy and paste too often... – clattenburg cake Jun 23 '20 at 15:11
  • I will bet you that you could diagnose this from the logs of that pip install line. I would be that numpy 1.19 has a minimum requirement on scipy above 0.15 and that pip would have logged that it wasn't going to install scipy or that something went wrong at the point. – Dan Jun 23 '20 at 16:02

1 Answers1

0

Answer- requirements.txt had an old version of scipy so it wasn't working, I bumped it up to 1.4.1 and it seems to work now.

clattenburg cake
  • 1,096
  • 3
  • 19
  • 40