Skip to content

[Python] Use PyInstaller package to make a exe file from Python file

Last Updated on 2021-04-11 by Clay

PyInstaller is a very convenience Python module, its goal is make a exe file from Python file.

Because that, we can convenience to give our program to our friends if their computer have no Python (That's possible!)

PyInstaller is a module you need.

PyInstaller will bind all the package you need to main program automatically. (Of course, the process of that have very many problems, so I advice to refer stackoverflow and github, maybe you can fix your problem.)

If you want to refer the official tutorial, maybe you can refer it: https://www.pyinstaller.org/

OK! We start!


How to use

First, suppose we have a simple program want to convert to exe file:

# -*- coding: utf-8 -*-
import jieba
import time

text = input('Input something!')
print(jieba.lcut(text))
time.sleep(5)


Output:

Input something!

"Jieba" is a simple tokenizer for Chinese. You can use "NLTK" to instead it.

Input something! 今天天氣真好
Building prefix dict from the default dictionary …
Loading model from cache C:\Users\Clay\AppData\Local\Temp\jieba.cache
['今天', '天氣', '真好']
Loading model cost 0.603 seconds.
Prefix dict has been built succesfully.
Process finished with exit code 0 

Then, how do we convert this tokenizer to exe file?

we can open terminal in our current folder, if you use Windows it is cmd.

If you never use PyInstaller, maybe you need use pip to install it.

 pip3 install pyinstaller

When we install this tool, we can use terminal to call this tool to convert our program.

pyinstaller -F test.py

By the way, there are many parameters you can set:

  • -F Convert to only one file.
  • -D Convert to a folder.
  • -d Generate a file you can debug.
  • -w Have a window if you execute the file.
  • -p set a path to import your package (If your package is not found.)
  • -n Select the file name you want to

After we executing, the current folder have folder "build", "dist", and a file ".spec".

We can ignore the folder build, the exe file in the folder "dist".

If you double clicked your exe file you convert, but anything didn't happen, you can use your terminal to execute your file. It maybe make you see the error you meet.

If it execute normally, congratulation! It's very great!

If it didn't work, take our program as an example, maybe the path of package "Jieba" not found.

You can add "-p Jieba path" in your command, or you can find a paragraph in the .spec file:

a = Analysis(['test.py'],
             pathex=['C:\Users\Clay\PycharmProjects\jieba_test'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

We add the Jieba path: (Your path maybe has some difference.)

pathex=['C:\Users\Clay\PycharmProjects\jieba_test'],

Split ',', and input the path from datatype "str".

And then, we use the following command:

 pyinstaller -F test.spec

We set a path of Jieba!


Discussion

If you use PyQt5 and you see the error: core not found. Maybe you can refer this chapter: [solved] PyInstaller unable to find Qt5Core.dll

Leave a Reply