Home > Software engineering >  How to fix import error with modules on python docker
How to fix import error with modules on python docker

Time:02-04

i'm creating an app and wanted to dockerize it. But after the build, when i do sudo docker-compose up i get the error module cogs not found at the line from cogs import FILES in my main file (josix.py). I already checked several topics on StackOverflow about import errors with docker but none of them got me the right answer so i try with this one.


The code looks like this :

/app

  • josix.py
  • cogs/
    • init.py (containing FILES variable)
    • others py files

(init.py and other py files are inside cogs directory)


Dockerfile :

FROM python:3

WORKDIR /app

COPY Josix/* ./

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

ENV PYTHONPATH "${PYTHONPATH}:/app/"

CMD [ "python3", "josix.py" ]

I tried to change the pythonpath, add several ones, change the import

CodePudding user response:

Remove the * from your copy command to copy sub-directories as well as files (and you don't need to add to PYTHONPATH /app is already in it).

FROM python:3
WORKDIR /app
COPY Josix/ ./
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python3", "josix.py" ]
  • Related