我正在尝试从一个非常简单的Python文件中制作可执行文件,该文件读取一个JSON文件。
在我的文件夹中,只有这两个文件:
test.py
data.json
所以我运行:
pyinstaller -F --add-data "data.json;data.json" test.py
这将创建一个dist文件夹,我可以在其中找到我的test.exe
。但是当我运行exe文件时,它无法找到data.json。
FileNotFoundError: [Errno 2] No such file or directory: 'data.json'
[18556] Failed to execute script test
我的文件非常简单:
import json
# data.json is in the same folder as test.py, so no path is provided
with open("data.json") as f:
data = json.load(f)
print(data)
编辑:
好的,看起来当我编译exe文件时,我必须从编译它的同一个文件夹中执行它,所以我必须执行以下操作:
./dist/test.exe
如果我切换到dist文件夹并从那里执行文件,它就不能正常工作。
cd dist
test.exe --> Fails
这显然不是我想要的。我应该能够从任何文件夹调用exe。data.json是从test.py所在的同一文件夹中包含的,因此在exe内部它们也应该在同一个“文件夹”中。
sys._MEIPASS
,但这并没有使我进一步。它会抛出错误AttributeError:module'sys'has no attribute'_MEIPASS'
。 - QuagTeX