Python 减少 pyinstaller exe 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47692213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reducing size of pyinstaller exe
提问by
I have a simple pandas pyinstaller exe which is over 40MB.
我有一个超过 40MB 的简单的 Pandas pyinstaller exe。
My exe example:
我的exe示例:
import collections
import csv
import selenium
import pandas
print('hi')
40MB+ for this seems a bit overkill.
40MB+ 似乎有点矫枉过正。
How can I reduce this as much as possible?
我怎样才能尽可能减少这种情况?
One method:
一种方法:
pyinstaller --onefile --exclude matplotlib --exclude scipy --exclude pandas --exclude numpy.py
This however is not practical considering how big the exclusion list would be.
然而,考虑到排除列表有多大,这是不切实际的。
How do I select a folder for pyinstaller to get modules from and exclude everything else so I may have a small application?
如何为 pyinstaller 选择一个文件夹以从中获取模块并排除其他所有内容,以便我可能拥有一个小应用程序?
Spec file:
规范文件:
a = Analysis(['123.py'],
pathex=['C:\Users\AA\ZZ'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='123',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
It's also worth mentioning. By default, Pyinstaller does not detect pandas.
这也值得一提。默认情况下,Pyinstaller 不检测熊猫。
Add:
添加:
hiddenimports = ['pandas._libs.tslibs.timedeltas']
To: C:\Users\<NAME>\AppData\Local\Programs\Python\Python36\Lib\site-packages\PyInstaller\hooks
A possible solutionwhen using multiple executables, could be to link each executable to a separate folder or executable with all imports.
使用多个可执行文件时可能的解决方案是将每个可执行文件链接到单独的文件夹或所有导入的可执行文件。
采纳答案by Ophir Yoktan
try setting up your environment with a virtualenv, and install in there only the required libraries
尝试使用 virtualenv 设置您的环境,并仅在其中安装所需的库
some details on working with virtual env are here: https://virtualenv.pypa.io/en/stable/
使用虚拟环境的一些细节在这里:https: //virtualenv.pypa.io/en/stable/
回答by Ophir Yoktan
For me, it is a simple case of using pandas that the exe is huge.
对我来说,exe 很大是使用熊猫的一个简单案例。
Though removing certain directories was helpful, as was UPXING that helped a great deal also.
虽然删除某些目录很有帮助,但 UPXING 也有很大帮助。
I got it reduced a lot and it was not doing this by default.
我让它减少了很多,默认情况下它没有这样做。
That being said, the final and most import solution is talked about here: Importing Python modules from a select location. So there was a feature that did all this, but for now there is some manual handling involved because: multipackage-bundles is broken.
话虽如此,这里讨论了最终也是最重要的导入解决方案:从选择位置导入 Python 模块。所以有一个功能可以完成所有这些,但现在需要一些手动处理,因为:multipackage-bundles 已损坏。
Now to the simple solution for lots of exe's
现在对许多exe的简单解决方案
If you have many executables, I highly recommend this approach:
如果你有很多可执行文件,我强烈推荐这种方法:
pyinstaller -F abc.py --onedir (Have all imports of both scripts)
pyinstaller -F abd.py --onedir (Have all imports of both scripts)
Now put abd.exe in the one directory of abc.py folder as well as any other external scripts. Be sure they are differently named or only one script will run.
现在将 abd.exe 和任何其他外部脚本放在 abc.py 文件夹的一个目录中。确保它们的名称不同,否则只会运行一个脚本。
This works really well because all the dependencies are in one folder. This is how it should be. So in this example say you had a 40mb one folder. For each additional exe afterwards, it will only be +5mb(or how big the exe is) rather than 40mb each.
这非常有效,因为所有依赖项都在一个文件夹中。这是应该的。所以在这个例子中,假设你有一个 40mb 的文件夹。对于之后每个额外的 exe,它只会是 +5mb(或 exe 有多大)而不是每个 40mb。
回答by Aaron Garton
The python interpreter and all imported modules are included in the executable.
python 解释器和所有导入的模块都包含在可执行文件中。
You can try adding modules you want to exclude to the excludes
list under Analysis
in your spec file.
您可以尝试将要排除的模块添加到规范文件中的excludes
列表Analysis
中。
You could also try compressing the executable using UPX. See A note on using UPX
您也可以尝试使用 UPX 压缩可执行文件。请参阅使用 UPX 的注意事项
回答by droebi
I use the Anaconda environment and so the virtualenv solution isn't an option. my way was to exclude unnecessary modules in my spec file, ex.:
我使用 Anaconda 环境,因此 virtualenv 解决方案不是一个选项。我的方法是在我的规范文件中排除不必要的模块,例如:
in Analysis(...)
在分析(...)
excludes=['pandas', 'numpy'],
(this are modules whose raise the size of the files extraordinarily)
(这是特别增加文件大小的模块)
For every build i'm using this adjusted spec file to create the exe.
对于每个构建,我都使用这个调整后的规范文件来创建 exe。
pyinstaller "mySpec.spec" --distpath="<path>"