【发布时间】:2022-08-22 00:54:19
【问题描述】:
我在 Heroku 上部署 Flask 应用程序时遇到了问题。我当前的问题是 Heroku 无法在我的静态文件夹中找到文件。在尝试了关于 SO 的建议后,我现在收到此错误:
with open(file_dir, encoding=\"utf-8\") as f:
IsADirectoryError: [Errno 21] Is a directory: \'/app\'
这是我发现的帮助 Heroku 找到鼓励.txt 文件的建议之一:
# Absolute path to the file
file_dir = os.path.dirname(os.path.abspath(\'encouragement.txt\'))
# Absolute path to this file\'s root directory
parent_dir = os.path.join(file_dir, os.pardir)
app_dir = os.path.join(parent_dir, \'/app\')
添加了 \'/app\' 是因为其中一篇文章提到 Heroku 有一个 app 目录作为父目录,所以不是 \'static/encouragement.txt\',而是 \'app/static/encouragement。文本\'。加入和不加入 \'/app\' 都会出现相同的错误。 这是错误的来源:
@app.route(\"/\")
def index():
# Read data from the file as str
with open(file_dir, encoding=\"utf-8\") as f:
messages = f.read()
我尝试使用 heroku bash 命令检查文件系统结构并确认应用程序目录,但命令执行并返回空白。 我的文件组织如下:
MyProject
> static
> encouragement.txt
app.py
不确定是否需要,但我正在使用 Windows。谢谢你。
更新: 我尝试添加 index.php 和 composer.json 作为几个教程的建议。它们不适用于 txt 文件;不确定他们是否对其他静态文件有所帮助。不是最好的解决方案,但我最终只是将所有 txt 内容复制并粘贴到我的 app.py 中。
标签: flask heroku deployment