如何使用 pyinstaller 创建最小大小的可执行文件?

问题描述

我在 Windows 10 上,我安装了 anaconda,但我想使用 python 3.5 在一个新的、干净的最小环境中独立创建一个可执行文件.所以我做了一些测试:

I am on Windows 10, I have anaconda installed but I want to create an executable independently in a new, clean minimal environment using python 3.5. So I did some tests:

测试1:我在 testenv 文件夹中创建了一个 python 脚本 test1.py,只有:

TEST1: I created a python script test1.py in the folder testenv with only:

print('Hello World')

然后我创建了环境,安装了pyinstaller并创建了可执行文件

Then I created the environment, installed pyinstaller and created the executable

D:	estenv> python -m venv venv_test
...
D:	estenvvenv_testScripts>activate.bat
...
(venv_test) D:	estenv>pip install pyinstaller
(venv_test) D:	estenv>pyinstaller --clean -F test1.py

它会创建大约 6 Mb 的 test1.exe

And it creates my test1.exe of about 6 Mb

测试2:我修改test1.py如下:

TEST 2: I modified test1.py as follows:

import pandas as pd
print('Hello World')  

我在环境中安装了 pandas 并创建了新的可执行文件:

I installed pandas in the environment and created the new executable:

(venv_test) D:	estenv>pip install pandas
(venv_test) D:	estenv>pyinstaller --clean -F test1.py

Ant 它创建了我的 test1.exe,它现在是 230 Mb!!!

Ant it creates my test1.exe which is now of 230 Mb!!!

如果我运行命令

(venv_test) D:	estenv>python -V
Python 3.5.2 :: Anaconda custom (64-bit)

当我运行 pyinstaller 时,我收到一些我不理解的消息,例如:

when I am running pyinstaller I get some messages I do not understand, for example:

INFO: site: retargeting to fake-dir 'c:\users\username\appdata\local\continuum\anaconda3\lib\site-packages\PyInstaller\fake-modules'

我还收到有关 matplotlib 和其他与我的代码无关的模块的消息,例如:

Also I am getting messages about matplotlib and other modules that have nothing to do with my code, for example:

INFO:   Matplotlib backend "pdf": added
INFO:   Matplotlib backend "pgf": added
INFO:   Matplotlib backend "ps": added
INFO:   Matplotlib backend "svg": added

我知道有一些相关的问题:减小pyinstaller exe的大小, 使用 pyinstaller 和 numpy 的可执行文件的大小但我无法解决问题,我担心我在 anaconda 方面做错了什么.

I know there are some related questions: Reducing size of pyinstaller exe, size of executable using pyinstaller and numpy but I could not solve the problem and I am afraid I am doing something wrong with respect to anaconda.

所以我的问题是:我究竟做错了什么?我可以减小可执行文件的大小吗?

So my questions are: what am I doing wrong? can I reduce the size of my executable?


解决方案

问题是你不应该使用虚拟环境,尤其是 anaconda.请下载默认的 32 位 python 并仅使用必要的模块.然后按照链接中提供的步骤进行操作,这肯定会解决它.

The problem is that you should not be using a virtual environment and especially not anaconda. Please download default python 32 bit and use only necessary modules. Then follow the steps provided in the links, this should definitely fix it.

虽然您创建了一个虚拟环境,但您确定您的规范文件没有链接到旧的 Anaconda 条目吗?

Although you created a virtual env, are you sure your spec file is not linking to old Anaconda entries?

如果这一切都失败了,那么提交一个错误,因为这很奇怪.

If all this fails, then submit a bug as this is very strange.

相关文章