1

I'm writing a docker script for running a python program from bitbucket, say myprogram.py. The python program uses specific functions from other files in other directories, and calls them like this:

from mydirectory.myfunction import MyFunction

Normally, if I just clone the bitbucket repository and run it, no errors occur.

My repo in bitbucket consists of the program, a docker file and some directories with scripts of functions. So I clone the repo, and then build.

When I tried to build it, it was succesful. I build it with this command: docker build -t myprogram .

However, when I use the command (the -h is the "help" option made with argparse just to see if the program is able to run with an option):

docker run --rm -it myprogram -h it gives me the error:

Traceback (most recent call last):
File "/usr/src/myprogram.py", line 8, in <module>
  from mydirectory.myfunction import MyFunction
ImportError: No module named 'mydirectory' 

What is the best way to implement in the docker file, that the program uses functions from other scripts in other directories in the same repo in bitbucket? I'm new to docker, so any help would be appreciated! Please let me know if you need any additional information.

My dockerfile looks like this:

FROM debian:stretch

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update -qq; \
    apt-get install -y -qq git \
    apt-utils \
    wget \
    python3-pip \
    ncbi-blast+ \
    libz-dev \
    ; \
    rm -rf /var/cache/apt/* /var/lib/apt/lists/*;

ENV DEBIAN_FRONTEND Teletype

# Install python dependencies
RUN pip3 install -U numpy

COPY myprogram.py /usr/src/myprogram.py

RUN mkdir /mydirectory

COPY mydirectory/* /mydirectory/

RUN chmod 755 /usr/src/myprogram.py; \

WORKDIR /workdir

# Execute program when running the container
ENTRYPOINT ["/usr/src/myprogram.py"]
2
  • Can you provide the dockerfile that is being built? My guess is that you are not copying the mydirectory into the container. Maybe that's why python is unable to find it. Commented Jan 12, 2021 at 11:49
  • I have provided the code now. I did copy mydirectory into the container - but many that's not the correct way? Thanks in advance Commented Jan 12, 2021 at 12:34

1 Answer 1

1

The problem with this is that you are copying mydirectory into / while your script is located in /usr/src. By default, python only looks for packages in the site-packages directory and the directory where the script is located (the current working directory is used as a fallback in case location of script is not available). So, you should copy your mydirectory in the same directory as your script (which is /usr/src). So, you have two ways to move ahead:

  1. Copy mydirectory into /usr/src (and you may make it the working directory).
  2. Manipulate the sys.path in your script to include the absolute path to mydirectory.

To do this, simple put this snippet in your script before importing mydirectory

import sys
sys.path.append('/')
del sys # If you don't need it anywhere else
from mydirectory.myfunction import MyFunction

You may want to take a look at sys.path documentation.

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

1 Comment

Thank you very much, option no. 1 solved my problem (I did not try option 2). Appreciate it :-)

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.