Python:排除模块 Pyinstaller
我开始使用 Pyinstaller 而不是 Py2Exe。然而我很快就遇到了问题。如何排除不需要的模块,以及如何查看包含在单个可执行文件中的模块?
我可以从 Python 安装中的 DLL 文件夹中删除一些 pyd
和 dll
文件,这样 Pyinstaller 就找不到它们,因此不会包含它们。我真的不想对所有模块都这样做,因为这会变得相当困难。
我确实尝试编辑 Pyinstaller 生成的规范文件。
a.binaries - [('ssl','pydoc',)],
但文件的大小保持不变,所以我断定这不起作用。
那么我如何查看 Pyinstaller 包含哪些模块以及如何排除那些我不想要的模块?
I've begun using Pyinstaller over Py2Exe. However I've rather quickly run into a problem. How do I exclude modules that I don't want, and how do I view the ones that are getting included into the single executable file?
I can remove some pyd
and dll
files from the DLL folder in my Python installation so Pyinstaller doesn't find and therefore doesn't include them. I don't really want to be doing that with all the modules as it will get quite arduous.
I did try edit the spec file that Pyinstaller makes.
a.binaries - [('ssl','pydoc',)],
But the size of the file remained the same so I conclude that didn't work.
So how can I see what modules Pyinstaller are including and how do I exclude those that I do not want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是总结一下我使用的选项。
PyInstaller TOC - 正如文档所述:
换句话说,简单地说:
因此,在您的 .spec 文件中 - 一旦您获得了脚本的分析结果 - 您就可以通过以下任一方式进一步修改相应的目录:
对于特定文件/模块使用差值 (-) 和交集 (+) 运算来修改 TOC。 *
要添加/删除文件/模块的列表,请迭代目录并与模式匹配代码进行比较。
(* 顺便说一句,为了使差异发挥作用,您似乎必须显式转换为
TOC()
并注意,由于它只是唯一定义集合元素的名称,因此您只需要指定 - 因此('sqlite3', None, None)
等)下面是一个说明性示例(取自 .spec 文件),无论好坏 - 我删除了对 scipy 的所有引用, IPython 和 zmq;删除特定的 sqlite、tcl/tk 和 ssl .DLL;插入缺少的 opencv .DLL;最后删除除 matplotlib 之外的所有数据文件夹...
当 .pyc 文件尝试加载预期的 .DLL 时,生成的 Pyinstaller .exe 是否可以工作尚无定论
:-/
Just to summarise the options here as I use them.
PyInstaller TOC's - are, as the documentation says:
In otherwords, simply:
So, in your .spec file - once you've got the Analysis results of the script - you can then further modify the respective TOC's by either:
For specific files/modules use the difference (-) and intersection (+) operations to modify a TOC. *
For adding/removing lists of files/modules iterate over the the TOC and compare with pattern matching code.
(* As an aside, for the difference to work it seems you must explicitly cast to
TOC()
and note that since it is only the name that uniquely defines the element of the set, you only need to specify that - hence('sqlite3', None, None)
etc.)An illustrative example (taken from a .spec file) is below where - for better or worse - I remove all references to scipy, IPython and zmq; delete specific sqlite, tcl/tk and ssl .DLL's; insert a missing opencv .DLL; and finally remove all data folders found apart from matplotlib ones...
Whether the resulting Pyinstaller .exe will then work when an .pyc file tries to load an expected .DLL is moot
:-/
虽然可能已经给出了更好的解决方案,但还有一种方法:
您可以将“--exclude-module”属性与“pyinstaller”命令一起使用,但当您必须排除许多模块时,此方法相当冗长。
为了使工作更轻松,您可以编写一个批处理脚本文件,其中包含所有值得跳过的库并一次又一次地使用它。
像这样:
或者您始终可以使用新的 python 安装,只需在安装时更改新的 python 安装的路径即可。
Although better solutions may have been given but there is one more method:
You can use the '--exclude-module' attribute with the 'pyinstaller' command but this method is quite lengthy when you have to exclude many modules.
To make the work easier, you can write a batch script file with all the libraries worth skipping and use it again and again.
Something like this:
Or you can always use a new python installation, just change the path of the new python installation while installing.
您可以尝试一下,它比 .bat 脚本好得多:
希望您发现这很有用!
You can just try this, it's much better than a .bat script:
Hope you find this useful!
自己寻找答案
我见过很多这样的问题,但没有人教你如何自己调试。
pyinstaller 的文档可能对初学者有用,但是一旦您需要更多...
个人认为 pyinstaller 文档不友好(示例太少)并且缺乏更新。
例如,文档显示pyinstaller的版本是3.2。 (3.5现已推出(2019/10/5))现在(2020-12-15)它们都有相同的版本我想说你应该从源代码中找到答案
START WAY
在运行此脚本之前,您可以分配参数,例如“--clean your.spec”
在
{env_path}/Lib/site-packages/PyInstaller/building/build_main处设置断点.py-> def build(...) ... -> exec(code, spec_namespace)
如下所示:注意:如果您没有使用虚拟环境,实际路径应为{Python}/Lib/site-packages/PyInstaller/building/build_main.py
,然后您可以观察您感兴趣的任何变量。
此外,您还将了解有关 pyinstaller.exe 实际做什么的更多信息。
例如,您学习 TOC 类继承到列表,并查看比 文档来自 PyInstaller.building.datastruct.py
最终,您可以使用任何 python 方式来设置
a.binaries
和 < code>a.datas 这是您真正想要的关联文件位置
FIND THE ANSWER BY YOURSELF
I've seen a lot of questions like this, but no one teaches you how to debug by yourself.
document of pyinstaller may have useful for beginner, but once you need more ...
personally, think the pyinstaller documentation is not friendly (Too few examples) and lacks updates.
for example, The document shows the version of the pyinstaller is 3.2. (3.5 is available now (2019/10/5))Now (2020-12-15) they both have the same versionI want to say that you should find the answer from source code
START WAY
before you run this script you can assign parameters, like "--clean your.spec"
set breakpoint at
{env_path}/Lib/site-packages/PyInstaller/building/build_main.py -> def build(...) ... -> exec(code, spec_namespace)
like below:note: If you are not using the virtual environment, the actual path should be {Python}/Lib/site-packages/PyInstaller/building/build_main.py
then you can watch any variables that you interested in.
also, you will learn more about pyinstaller.exe actually do what.
for example, you learn the class of TOC is inherits to list, and see more the detail than the document from PyInstaller.building.datastruct.py
eventually, you can use any python way to set
a.binaries
anda.datas
that is you really wantedASSOCIATED FILE LOCATION
您可以使用 Python 操作 Analysis 类生成的列表。请注意,它们采用 PyInstaller 的 TOC 格式。
You can manipulate the lists produced by the Analysis class using Python. Note that these are in PyInstaller's TOC format.