【发布时间】:2020-04-25 15:03:56
【问题描述】:
我用 Pyinstaller 创建了一个 .exe 文件。程序应在运行时创建一个包含结果的数据文件。当我执行 python test.py 时,它在 Python 解释器模式下工作,但在 .exe 模式下不起作用。它根本不创建文件并以错误响应。
我应该在命令python -m PyInstaller --hidden-import=timeit --hidden-import=bisect -F test.py 中添加哪些标志以使其工作?
此设置的例外是:
Error: [Errno 2] No such file or directory: "C:\Users\Admin\AppData\Local\Temp\..."
上述目录是临时目录,我无权访问。
应该写入文件的代码是:
def write_file(data, path, encoding='utf-8'):
'''Creates report files.'''
try:
if config.python_version == 2 and type(data) in (list, str):
f = io.open(path, 'wb')
else:
f = io.open(path, 'w', encoding=encoding, newline='')
if type(data) is list:
writer = csv.writer(f)
writer.writerows(data)
else:
f.write(data)
f.close()
console(u'Report file: ' + path)
except IOError as e:
console(e, level=Level.error)
我认为应该有一个设置指向应该保存文件的位置。
我在这里检查过https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle,但没有成功。我无法正确使用列出的标志,似乎没有任何效果。
如何指定文件的保存位置?
【问题讨论】:
-
我假设错误消息中的这个文件夹是脚本尝试保存文件的位置?可以手动指定文件的保存位置吗?
-
@pavel 这就是我想问的。如何指定文件的保存位置?
-
不看代码很难说。
-
我的意思是在这种情况下它不依赖于代码。 PyInstaller 决定将输出文件放在哪里。它试图放置它的目录与 Python 脚本本身保存它的目录不同。
-
在这种情况下我不知道。这似乎是 pyinstaller 的一个非常有问题的功能,我想不出我更喜欢编译器来决定脚本输出的放置位置的情况。您是否尝试过使用任何其他工具(如 py2exe、cx_Freeze 或其他工具)创建 .exe?
标签: python pyinstaller