[PYTHON] About errors during PyInstaller installation and execution

What is PyInstaller

PyInstaller is a great tool that can convert your Python script (.py) into an executable file (.exe). As a practical example that I actually experienced, "** I want to use an automatic analysis tool created in Python at a joint research destination (without a Python environment) **" It was an excellent tool to fulfill the desire to have fun. In this article, I want to use PyInstaller, but I'm throwing an error, and I'm aiming to send a help ship to those who are lost on the road. For how to use PyInstaller, please see the following articles that I actually referred to. -Exe with PyInstaller @takanorimutoh (Qiita) -Turn Python files into exe! How to use Pyinstaller

Here, I will explain how to avoid each of the three errors that I was really into. The purpose is not to clarify the cause of the error, but to generate an exe file using ** PyInstaller **, so we will not consider the cause of the error in depth. If you want to know more, please gg in English.

Three addictive errors

  1. PyInstaller cannot be installed in the first place
  2. The exec function does not work when the exe file is executed (ModuleNotFoundError: No mudule named'pkg_resources.py2_warm'. [8352] Failed to execute script pyi_rth_pkgres)
  3. I don't know (qt.qpa.plugin: Could not find the Qt platform plugin" windows "in" ". This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. )

environment

Windows 10 Anaconda3 4.8.2 Python 3.6.5 PyInstaller 3.6

1. "PyInstaller cannot be installed in the first place" problem

This was very difficult. In conclusion, ** did not resolve **. Few people are addicted to this error and have resolved it. It is a swamp that is quite difficult to escape.

Command to install

pip install pyinstaller
(This is recommended, but I used conda install pyinstaller when I finally installed it, please try which one is better, if you are building an environment with anaconda , The latter is recommended)

What kind of error did you get?

I didn't write down the details of the error at this time, but I roughly remember that ** "setuptools" behaved quite badly ** (like satisfies the requirement setuptools). Contents)

Breakthrough

Finally, the installation is now ongoing now. Well, how did you do it?

--1st try: anaconda update It's also talked about deeply in GitHub (you'll see it when you look it up), but among them, to avoid this error, conda I witnessed a post saying that update is valid, I updated it, but it didn't work (even when using update, the setuptools that are already included are deeply connected to anaconda, so disconnect setuptools and perform update etc. Can't) --2nd try: Install wheel and setuptools I forgot which article this was written in, but if you don't have the setuptools you're happy with, you can do it. I didn't get any results! !! !!

(Speaking in detail, I had given up on installing PyInstaller for the time being. I couldn't avoid the error and I couldn't proceed with my work. I was responding on the spot with a batch file. , I needed to update seaborn in a place that has nothing to do with it. However, I can't update it. The environment is too dirty. Dirty means that ** pip and conda are mixed **. The environment is collapsing. So, I tried to rebuild the environment only with conda (docker seems to be troublesome if it is windows, so I stopped it) and launched a new virtual environment conda create --name makeexe python == 3.6. At this stage, pip install pyinstaller was unsuccessful. Perhaps ** pyinstaller cannot be installed with pip in the Anaconda environment **. I have installed the minimum required packages (conda install numpy pandas seaborn matplotlib tqdm opencv pillow). After that, I suddenly thought about installing pyinstaller via conda, and when I tried conda install pyinstaller, it came in quickly. Calling, cousin. )

What was the problem after all?

I don't know the details, but I think it was a mistake ** to enter the Anaconda environment via pip in the first place. As a result theory. Don't mix pip in the conda environment, it means prohibiting environmental destruction. I was keenly aware of it. As much as possible, make sure that the library you want to install is available in conda. If you can, let's do it there. That is safer.

2. "The exec function does not work when executing an exe file" problem

PyInstaller execution command

First of all, the installation of PyInstaller was successful and I was able to run it. After moving to the directory where you want to apply PyInstaller (to make it into an exe file), execute the following command to make it into an exe file (with --onefile, combine the modules into one exe file; the output file is an exe file. Only).

$ cd ~~~ # main.Move to the directory where py is located
$ pyinstaller main.py --onefile

The directory structure at this time is as follows. By running Pyinstaller, two new directories, build and dist, are created, and dist / main.exe and the executable file are generated. By double-clicking this, the program of main.py will be executed. Well, I'm throwing an error. Haha

./
|
|- main.py (Created program)
|- config.json (main.config file read by py)
|- main.spec (created with pyinstaller)
|- main.exe (created in the dist directory with pyinstaller, installed in the same directory as config for the convenience of code)
|- dist/
|    |- main.exe
|
|- build/
     |- main/
          |- ~~~

What kind of error did you get?

When I ran the generated main.exe, I got the following error.

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 13, in <module>
  File "C:\ProgramData\Anaconda3\envs\(venv you made)\lib\site-packages\PyInstaller\loader\pyimod03_iimporters,py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pkg_resources\__init__.py", line 86, in <module>
ModuleNotFoundError: No mudule named 'pkg_resources.py2_warm'
[8352] Failed to execute script pyi_rth_pkgres

ʻExec cannot be executed, it screams that the pkg_resources.py2_warm` required to execute is missing.

solution

Refer to here and add the module called ModuleNotFoundError to hiddenimports in the main.spec file generated when PyInstaller is executed. Add.

-              hiddenimports=[],
+              hiddenimports=['pkg_resources.py2_warn'],

Then, ** use this spec file ** and create an exe file again with PyInstaller (At this time, it is okay to throw away the files other than the spec file, so let's throw it away. , It causes problems such as errors and overwriting). Execute it with the following command. pyinstaller main.spec --onefile
This will stop throwing errors about the exec module.

3. "I don't know, but I vomit" problem

By using the main.spec file earlier, I was able to avoid the error related to the exec module. But when I was running it still threw an error. Let's see what kind of error it was. By the way, I call this "I don't understand error" because I don't have enough vocabulary. Well, it's easy to understand.

What kind of error did you get?

C:\ProgramData\Anaconda3\envs\(venv you made)\lib\site-packages\PyInstaller\loader\pyimod03_iimporters,py:623:MatplotlibDeprecationWarning:
THEMATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
  exec(bytecode, module.__dict__)
2020/03/27 10:29:55 - complete to load config.json (From here on, intentional output within the program)
~(Omission)~
2020/03/27 10:30:14 - success affine transforming.
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

The first message is also from exec, set to hiddenimport and dealt with, so go through. The following is for announcing the progress of the program, so it's okay (a message that is intentionally spit out in the program). A new error related to qt.qpa.plugin occurred around the time of Affine transformation. I wonder if the platform can't be installed. I'm not sure what's going on.

solution

Questions on stackoverflow However, if you excerpt a part,

There seem to be two solutions, the first one worked fine for me:

  • copy platform directory to directory of your executable. You'll find the platform directory at a location like c:\Users\<username>\envs\<environmentname>\Library\plugins\platforms or
  • Upgrade to a newer version of pyqt: conda install -c anaconda pyqt

Use the second option with care: Do not try to use pip for pyqt installation if you have a conda environment, this might break your conda installation: https://github.com/ContinuumIO/anaconda-issues/issues/1970

When,

I had an issue where my python code worked fine, but the compiled .exe file would provide the "could not find or load the Qt platform plugin windows" problem. I fixed the problem by copying the ~PyQt5\Qt\plugins\platforms folder from the program's directory, generated by using pyinstaller --onedir main.py, to the folder holding the .exe file.

It seems that in my case the only way "helping" my program detect the required .dlls was having the platforms folder next to the main.exe. Pasting the platforms folder to the program's directory after using pyinstaller --onefile main.py also makes the program work.

is. As expected it is stack overflow. The answer to what you want to solve is already out. Great. First of all, I tried installing pyqt as something that I could try at a reasonable price. The command is conda install -c anaconda pyqt

But it didn't improve. What's wrong with stackoverflow. Is it like this? It doesn't give me a chance to squeeze it, the next idea has already been drawn, oh, I can't beat stackoverflow.

So, I made a copy of the platform directory, which is recommended as another method. The platform is located in "C: \ ProgramData \ Anaconda3 \ envs \ (venv you made) \ Library \ plugins \ platforms" (in my case, it may be different for each person, but it was also a different path in the answer on stackoverflow) .. Copy this platforms directory to the same directory as the exe file you want to run. In other words, the final folder structure will be like this ⇓

./
|
|- main.py (Created program)
|- config.json (main.config file read by py)
|- main.spec (created with pyinstaller, hidden inports'pkg_resources.py2_warn'Added)
|- main.exe (created in the dist directory with pyinstaller, installed in the same directory as config for the convenience of code)
|- platforms/(Copy and paste here)
|   |
|   |- qdirect2d.dll
|   |- qminimal.dll
|   |- qoffscreen.dll
|   |- qwebgl.dll
|   |- qwindows.dll
|- dist/
|    |- main.exe
|
|- build/
     |- main/
          |- ~~~

If you execute the exe file in this state (it should not be necessary to rebuild), the exe file will be executed normally until the end! !! !! !! If the exe file throws an error, try rebuilding again.

At the end

I want to use PyInstaller, but I get an error. I think I get various errors. There is no article that summarizes those errors. I didn't understand well and was swung around. So, after all, the fundamental solution was to use conda install. I'm not sure. But PyInstaller is useful. Because there are PCs (and brains) that cannot install the program language as well as Python because they are separated from the net in the world. However, the drawback of this is that ** the generated exe file is very heavy **. At least, unlike scripts, you can't easily email it. I think there are still various errors. Please use this article. If you write in the comments, we'll also add a workaround for that error to this article. I want to make a PyInstaller error countermeasure manual together.

Thank you for reading. Goodbye

Recommended Posts

About errors during PyInstaller installation and execution
About errors during PyInstaller installation and execution
About _ and __
[PyTorch] Installation method
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
[Python] Chapter 01-02 About Python (Execution and installation of development environment)
Summary and common errors about cron
PySpark 1.5.2 + Elasticsearch 2.1.0 Installation procedure and execution
About _ and __
Dealing with pip and related installation errors on Ubuntu 18.04
About Class and Instance
pytube execution and error
About cumprod and cummax
[Python] Memo about errors
About cross-validation and F-number
I wanted to worry about execution time and memory usage