0

I'm executing a python file inside a docker container, and need to import all the functions that I put into a separate python file called base_functions. However, writing from base_functions import * throws the error, that ModuleNotFoundError: No module named 'base_functions', even though base_functions.py is in the same directory as the main python file. How can I do this? Do I need to specify which python scripts I want to import beforehand, in the settings.ini or something?

This is the content of the Dockerfile:

FROM amancevice/pandas:0.24.1-alpine

RUN apk update
RUN apk add build-base
RUN apk add gcc musl-dev libc-dev util-linux-dev linux-headers python3-dev postgresql-libs postgresql-dev git libffi-dev libmemcached-dev zlib-dev \
            ca-certificates zlib-dev jpeg-dev freetype-dev libpng

RUN pip3 install --upgrade pip
COPY requirements.txt .
RUN pip3 install -r requirements.txt

COPY src /vdp

WORKDIR /

ENTRYPOINT ["python3", "-m", "vdp"]

These are all the files in the directory of the project:

/home/cr/docker/71119/.

├── docker-compose.yaml
├── Dockerfile
├── Makefile
├── README.md
├── requirements.txt
├── settings.ini
└── src
    ├── base_functions.py
    ├── influx.py
    ├── __init__.py
    └── __main__.py
4
  • Have you tried using from .base_functions import *? Commented Nov 11, 2019 at 16:06
  • yes, it returns the same error :( Commented Nov 11, 2019 at 16:13
  • Then maybe the files are not in the same directory, if will be helpful if can you share your Dockerfile and your folders structure to know a bit more about your issue Commented Nov 11, 2019 at 16:15
  • ive edited the post Commented Nov 11, 2019 at 16:55

3 Answers 3

3

You're practically renaming your package (it smells like a package, having __init__.py and __main__.py) from "src" to "vdp" when you're creating the dockerfile.

I'd recommend:

  • Rename src/... to src/vdp/... (or vdp/...) – at this point you should be able to run python -m vdp within the src directory on your host machine and things should work.
  • Change the Dockerfile stanzas to
    COPY src /src
    WORKDIR /src
    ENTRYPOINT ["python3", "-m", "vdp"]
    – this'd be equivalent to how you're running it on the host machine.

Another option would be to drop the /src in the repository and change things to COPY vdp /src/vdp.

The third option, of course, would be to set up proper Python packaging, have your setup.py build a proper wheel, then simply install that in the Docker container.

Sign up to request clarification or add additional context in comments.

4 Comments

after renaming the folders I am able to run python -m vdp within the src directory. However, If I exchange the copy, workdir and entrypoint lines for the ones you posted, I still get the same error.
Might be a silly typo on my part. You can enter the container interactively using docker run -it image-id-here bash and examine the situation in /src.
You need to change ENTRYPOINT to CMD for that to work. (You should, there’s not really a downside.)
@AKX Wouldn't the COPY command copy the entire directory? Why aren't the import locations preserved?
1

Python is not able to find the file base_functions because it was run from other folder.

You may want to change the working directory of in the Dockerfile:

WORKDIR /path/to/workdir

1 Comment

this leads to the error "| /usr/bin/python3: No module named vdp"
1

You need to mention the PYTHONPATH. In your Dockerfile, I'm gonna assume your root dir is /home/cr/docker/71119/src/

env PYTHONPATH /home/cr/docker/71119/src/

Either that, or if you wanna see it working (my preference for debugging)

docker run -it <img-name> /bin/bash

export PYTHONPATH=/home/cr/docker/71119/src/ && python3 <your-script>.py And it should work.

Another tip is that if you want to show the file structure, install tree by: apt-get install tree -y && cd <your-dir> && tree And then post that to SO. It's just cleaner!

1 Comment

Modifying PYTHONPATH is generally not a good idea and can lead to confusion such as the same file being accessible with two different imports.

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.