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.
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.
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.
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.
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
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.
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.
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!
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