9

When importing python modules locally, I am able to successfully do so, however I'm having difficulty doing so when dockerising the app. It seems as though I get the opposite behaviour locally to how I get in the docker app... any thoughts?

I have the following directory structure

| app
|      |api.py
|      |settings.py
| tests
|      |test_api.py

In api.py, I import settings by: from app import settings In test_api.py I import app.py by: from app import api.

Locally, everything works fine. When I try to dockerize this API using the following Dockerfile:

FROM python:3.8.5-alpine

RUN pip install pipenv
COPY Pipfile /usr/src/
WORKDIR /usr/src
RUN pipenv lock --requirements > requirements.txt
RUN pip install -r requirements.txt

COPY app /usr/src/app

WORKDIR /usr/src/app

CMD ["python", "api.py"]

The docker image builds successfully, however upon running the container, I get the following error:

File "api.py", line 4, in <module>
from app import settings
ModuleNotFoundError: No module named 'app'

If I change how I import in api.py, to import settings, I get errors locally, but the docker container works perfectly. I'm pretty sure it's something to do with the PythonPath, any thoughts on how I can resolve this?

2
  • i believe that if you try to use it as a module, i'd recommend you to create a file init.py on the app folder... it is required to make Python treat directories containing the file as packages.(can just be an empty file) Commented Nov 6, 2020 at 12:58
  • I've answered the question in one of the responses below after having seen some other posts online and combining them with the answer from @stefan below. Commented Nov 6, 2020 at 13:33

3 Answers 3

10

I've just come across another thread on StackOverflow which seems to have resolved my issue. I can leave the import statements as I indicated above in my question, and by setting the PYTHONPATH in the docker container correctly, I am able to get the imports working correctly in docker.

How do you add a path to PYTHONPATH in a Dockerfile

My updated (working) Dockerfile is as follows:

FROM python:3.8.5-alpine

RUN pip install pipenv
COPY Pipfile /usr/src/
WORKDIR /usr/src
RUN pipenv lock --requirements > requirements.txt
RUN pip install -r requirements.txt

COPY app /usr/src/app

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

CMD ["python", "app/api.py"]
Sign up to request clarification or add additional context in comments.

1 Comment

How to write when I have multiple such apps like app1, app2, app3, etc: ` | app1 | |api.py | |settings.py |app2 | |x.py | |y.py |app3 | |alpha.py | tests | |test_api.py `
1

Your working directory is /usr/src/app and the import statement tries to find the directory "app". If you change your working directory to /usr/src/.

Alternatively you can import the settings directly via:

import settings

instead of

from app import settings

3 Comments

Changing the WORKDIR to usr/src doesn't solve the problem as is I do that, it can't find api.py. If I then have the CMD["python", "app/api.py"], I get exactly the same problem. Using the import settings as opposed to from app import settings works in the docker container, but not locally.
Looks like I need the WORKDIR to /usr/src as well as some other bits which I'll add in a separate post.
@AnishPatel from where do you start your api.py locally? if its from outside app you could use "from . import settings"
-1

You should use just

import settings

since, "api.py" & "settings.py" both are in app folder.

4 Comments

This works for the docker container, but then I get local issues... perhaps some local python path stuff? I'm using Pipenv which potentially complicates matters.
I am not getting your question. Are you trying to say that you are doing the same project on docker & also locally?
what is file hierachy for local project?
The file hierarchy is the same locally as in the docker container. The solution is above, I've posted it. Thanks for looking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.