[Automation] Convert Python code into an exe file

How to distribute Python code?

If you want other people to use a program written in Python, how to distribute it is a problem.

For example, let's say you wrote a program in Anaconda (Jupyter Notebook) that automates office work in the workplace. If you are kind, I would like you to use it not only for yourself but also for those around you. However, Anaconda is not always installed at work, and not everyone is familiar with the program. ** Anaconda? What is it a snake? Even if you explain to someone like ** "First, start Jupyter Notebook, select the ipynb file ...", it's a punch line.

One click if Python is converted to an exe!

If you convert the Python code to ** exe file (exe file) **, you can execute it with just a click. This makes it easy for people who are not familiar with the program to run it.

Here, I will explain how to convert a Python program written in Anaconda (Juypter Notebook) into an exe file. The general flow is as follows.

procedure

  1. Install pyinstaller
  2. Prepare a .py file
  3. Open Anaconda Prompt and navigate to the location of your .py file
  4. Turn the .py file into an exe file with the pyinstaller command
  5. Execute the exe file and check the operation

1. Install pyinstaller

There are several ways to turn python code into an exe file, but here we will show you how to use "pyinstaller".

You can install pyinstaller with pip install pyinstaller.

If you are using Anaconda, open [Anaconda Prompt] and enter the following command.

Anaconda Prompt


pip install pyinstaller

Or in the Jupyter Notebook cell

Jupyter Notebook


!pip install pyinstaller

You can also enter and execute.

2. Prepare a .py file

After successfully installing pyinstaller, next, put the code you want to convert into an exe file into a ".py file".

In the case of Jupyter Notebook, the file format is ** ipynb ** as it is, so you need to convert it to a py file.

Select Python (.py) from the Jupyter Notebook menu and select "File --Download as" to save it.

jupyter_pyfile.png

3. Open Anaconda Prompt and navigate to the location of your .py file

Start [Anaconda Prompt]. Then, use the cd command to move to the location where the py file you saved earlier is located. For example, if the location of the py file is "C: \ Users \ konitech \ sample", type the command as follows.

Anaconda Prompt


cd C:\Users\konitech\sample

4. Turn the .py file into an exe file with the pyinstaller command

Then create an exe file with the pyinstaller command. For example, if you want test.py to be an exe file, type the following command.

Anaconda Prompt


pyinstaller test.py --onefile

If there is no option called --onefile, many files will be created and messed up, so this --onefile is specified to combine them into one exe file.

If you get an error

Sometimes you get an error here and it doesn't work.

I will introduce the pattern that actually caused the error and the remedy.

■ ** RecursionError: maximum recursion depth exceeded while calling a Python object **

It seems that the number of recursion is too large, but I'm not sure. After investigating, I found that I should modify the [test.spec file] created when running py installer test.py --onefile.

I think that a test.spec file is created in the same folder as test.py, so open it with a text editor.

# -*- mode: python -*-

Insert the following just below and save it by overwriting.

import sys
sys.setrecursionlimit(5000)

And

Anaconda Prompt


pyinstaller test.spec

And specify the spec file you edited earlier, and create an exe file.

■ ** Failed to execute script pyi_rth_pkgres error **

It says that pyi_rth_pkgres can't be executed, but I'm still not sure. Again, modify the spec file.

In spec file

spec file


a = Analysis(['test.py'],
             pathex=['C:\\Users\\xxx'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],

I think there is such a part. Add pkg_resources.py2_warn to hidden imports here.

spec file


a = Analysis(['test.py'],
             pathex=['C:\\Users\\xxx'],
             binaries=[],
             datas=[],
             hiddenimports=['pkg_resources.py2_warn'],
             hookspath=[],

And

Anaconda Prompt


pyinstaller test.spec

Let's create an exe file by specifying the spec file edited earlier.

5. Execute the exe file and check the operation

When the creation is completed successfully, a folder called "dist" will be created, and I think that there is only one exe file in it. This is the completed exe file.

Let's run it and make sure it works.

If it works, you can distribute this exe file. You did it!

reference

Here are other automation series I wrote. If you are interested, please!

[Automation] Send Outlook email with Python https://qiita.com/konitech913/items/51867dbe24a2a4272bb6

[Automation] Read Outlook emails with Python https://qiita.com/konitech913/items/8a285522b0c118d5f905

[Automation] Read mail (msg file) with Python https://qiita.com/konitech913/items/fa0cf66aad27d16258c0

Recommended Posts

[Automation] Convert Python code into an exe file
How to convert Python to an exe file
Convert the character code of the file with Python3
How to turn a .py file into an .exe file
Convert multiple Jupyter notebooks together into an html file
Convert python 3.x code to python 2.x
[Python Kivy] How to create an exe file with pyinstaller
[Automation with python! ] Part 1: Setting file
[Automation with python! ] Part 2: File operation
I tried to convert a Python file to EXE (Recursion error supported)
[Python] How to convert db file to csv
[Python] Convert csv file delimiters to tab delimiters
The story of making Python an exe
Convert psd file to png in Python
Quickly create an excel file with Python #python
Creating an exe file with Python PyInstaller: PC freezes in parallel processing
[Python] Get the character code of the file
Combine multiple python files into one python file
[Automation] Read mail (msg file) with Python
Python code to convert region mesh code to latitude / longitude
Python script to convert latitude / longitude to mesh code
Convert svg file to png / ico with Python
[Python] [chardet] Automatic detection of character code of file
[Python] Implemented automation in excel file copying work
Automatically format Python code into PEP8-compliant code with Emacs
Develop Windows apps with Python 3 + Tkinter (exe file)
Python code that removes contiguous spaces into one
Convert cubic mesh code to WKT in Python
Script python file
Create an exe file that works in a Windows environment without Python with PyInstaller
python character code
Python file processing
[Python] Algorithm-aware code
Make each PowerPoint page an image file in Python
Create an image file using PIL (Python Imaging Library).
Read QR code from image file with Python (Mac)
How to convert JSON file to CSV file with Python Pandas
PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
Procedure to exe python file from Ubunts environment construction
I made an action to automatically format python code
Create an executable file (EXE) by PyInstaller in a hybrid environment (Nimporter) of Python + Nim