Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker image and ENV command for PYTHONPATH: module not found even when locally runs fine

I've got a Python job that I'm trying to ship in a Docker image. The code is structured in such a way that some modules get imported from a modules folder, so I've added to the Python path.

Specifically, the Dockerfile is

FROM python:3

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app

ENV PYTHONPATH "/usr/src/app"

RUN pip3 install -r requirements.txt

As you can see, I'm trying to set the environment variable for PYTHONPATH so it would find stuff in the same working directory.

The script to run is called main.py and when I run it locally (not from docker) as

PYTHONPATH=$PYTHONPATH:$HOME/job-path python3 main.py

it runs fine.

With that Dockerfile instesd, after building the image I get, from a docker inspect <ID>, that the Env field contains

"Env": [
            "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=C.UTF-8",
            "PYTHON_VERSION=3.6.4",
            "PYTHON_PIP_VERSION=9.0.1",
            "PYTHONPATH=/usr/src/app"
        ]

so it'd look like it's fine? But docker run gives me a

ModuleNotFoundError: No module named 'modules'
like image 774
mar tin Avatar asked Dec 29 '25 15:12

mar tin


1 Answers

The syntax is

ENV variable1[=value1] variable2[=value2] ...

Without an equals sign, you are creating two empty variables; the name of the second is /usr/src/app. You want

ENV PYTHONPATH="/usr/src/app"

with an equals sign between the variable's name and its value.

If you want to append to an existing value, you can:

ENV PYTHONPATH="/usr/src/app:${PYTHONPATH}"
like image 180
tripleee Avatar answered Jan 01 '26 05:01

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!