Home > front end >  How to import python file into another python file so that it works in Docker?
How to import python file into another python file so that it works in Docker?

Time:07-04

When I run my app.py file I can access it on localhost and it works.

After running (and having no issues): docker build -t flask-container .

When I run: docker run -p 5000:5000 flask-container

I get: from helpers import apology, login_required, usd

ModuleNotFoundError: No module named 'helpers'

In app.py I have: from helpers import apology, login_required, usd

I have tried putting in an empty __init__.py folder in the main folder, still doesn't work.

Question: How do I fix the Module Not Found Error when trying to run docker?

Dockerfile

FROM python:3.8-alpine

# By default, listen on port 5000
EXPOSE 5000/tcp

# Set the working directory in the container
WORKDIR /app

# Copy the dependencies file to the working directory
COPY requirements.txt .

# Install any dependencies
RUN pip install -r requirements.txt

# Copy the content of the local src directory to the working directory
COPY app.py .

# Specify the command to run on container start
CMD [ "python", "./app.py" ]

requirements.txt

flask===2.1.0
flask_session

Python Version: 3.10.5

CodePudding user response:

Please copy the helpers.py as well into the working directory.

COPY helpers.py .

OR

ADD . /app 
#This will add all files in the current local dir to /app
  • Related