Use PyInstaller to convert the python program.py to.exe

  • 2020-06-01 10:03:22
  • OfStack

preface

Recently I have been using a.py program a lot, but every time I use it on a different computer, I want to be able to publish the Python script as an executable running off the Python platform, such as a single exe. PyInstalle meets the requirements.

PyInstaller itself is not part of the Python package. Configure the python environment before installing pyinstaller.

Install pyinstaller

Download pyinstaller

Unzip to F:\ PyInstaller-2.1 (optional) (download the latest version from the official website)

Install pywin32

pywin32-217. win32-py2.7.exe: click to download

Install pyinstaller

1. Enter cmd


cd F:\PyInstaller-2.1

python pyinstaller.py --console --onefile test.py

If prompted:


Usage: python pyinstaller.py [opts] [  ... ] | 
pyinstaller.py: error: Requires at least one scriptname file or exactly one .spec-file

The installation is complete.

The test package

1. Put the files in the pyinstaller-2.1 folder in the current directory


cd F:\PyInstaller-2.1

python pyinstaller.py --console --onefile test.py

2. One test folder will be generated after the successful execution of the command. Under this folder will be a folder named dist, which has the converted test.exe

3. The exe compiled above can run normally, but it has a black console. Recompile below, add the windowed and icon, and cancel the console


python pyinstaller.py -w --onefile --icon="my.ico" test.py

my.ico is the custom icon file you want to add to it.

conclusion


Related articles: