Pyinstaller --onefile 警告:文件已经存在但不应存在

3
运行Pyinstaller --onefile命令并启动生成的.exe文件时,会弹出多个警告窗口,内容如下:
WARNING: file already exists but should not: C:\Users\myuser\AppData\Local\Temp\_MEI90082\Cipher\_AES.cp37-win_amd64.pyd

这使得即使通过点击警告继续运行.exe.exe 也很难使用。

如何消除这些警告?

3个回答

1
尽管遇到警告消息并为其创建可迭代对象以在.spec文件中排除它们是很好的,但如果我们不必经历这个压力过程,那将更好。让我们试一试。
观察:.spec文件中“数据”和“二进制文件”的数据结构相同。即 [(module_or_file_name, absolute_path, type_DATA_or_BINARY), ...]
这里的方法是相同的,实现是不同的。我们查找已添加到a.datas中并重复/重新添加到a.binaries中的内容。
方法1:[一个语句但较慢]
# ...
# a = Analysis(...)

a.binaries = [b for b in a.binaries if not b in list(set(b for d in a.datas for b in a.binaries if b[1].endswith(d[0])))]  # The unique binaries not repeated in a.datas.

方法二: [更快速]

# ...
# a = Analysis(...)

for b in a.binaries.copy():  # Traver the binaries.
    for d in a.datas:  #  Traverse the datas.
        if b[1].endswith(d[0]):  # If duplicate found.
            a.binaries.remove(b)  # Remove the duplicate.
            break

当我创建一个简化的Cython + PyInstaller + AES加密GUI打包应用程序时,我使用了这个实现。

希望这能帮助未来的某个人。


1

如果有人需要帮助,我把这个放在这里,因为我花了一些时间找出如何做到这一点。

在您的pyinstaller项目的.spec中,在a = Analysis(...)行后添加以下内容:

# Avoid warning
to_remove = ["_AES", "_ARC4", "_DES", "_DES3", "_SHA256", "_counter"]
for b in a.binaries:
    found = any(
        f'{crypto}.cp37-win_amd64.pyd' in b[1]
        for crypto in to_remove
    )
    if found:
        print(f"Removing {b[1]}")
        a.binaries.remove(b)

当然,您可以根据警告中显示的文件来调整数组to_remove以及确切的文件名.cp37-win_amd64.pyd
这样可以使这些文件不被包含在.exe中,并且警告将消失。

你好,我遇到了与此处描述的PySide6问题相似的问题: https://dev59.com/A2sMtIcB2Jgan1znsjcK 我尝试了上面回答中提到的“found = any”方法,但是有些文件没有被删除。你有什么建议吗? - digikwondo

1

我有几乎相同的问题。
不是一个好主意 - 删除你正在迭代的列表的一部分。
尝试这个:

from PyInstaller.building.datastruct import TOC

# ...
# a = Analysis(...)

x = 'cp36-win_amd64'
datas_upd = TOC()

for d in a.datas:
    if x not in d[0] and x not in d[1]:
        datas_upd.append(d)

a.datas = datas_upd


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接