如何将Python文件导入另一个Python文件中,以便在Docker中起作用?

发布于 2025-02-12 16:58:09 字数 1022 浏览 0 评论 0 原文

当我运行我的app.py文件时,我可以在localhost上访问它,并且可以访问它。

运行后(没有问题): docker build -t flask -container。

当我运行时: docker run -p 5000:5000 flask -container

我得到:来自助手导入道歉,login_required,USD

modulenotfounderror:

app.py中的没有名为'helpers'的 模块。帮助者导入道歉,login_required,USD

我尝试在主文件夹中放入一个空的 __ INT __。py 文件夹,仍然不起作用。

问题:在尝试运行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" ]

要求

flask===2.1.0
flask_session

.txt Python版本:3.10.5

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滿滿的愛 2025-02-19 16:58:09

请将helpers.py也复制到工作目录中。

COPY helpers.py .

或者

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文