・ Windows 10 pro 64bit ・ Python 3.7.9 ・ Anaconda 4.8.5 ・ PyTorch 1.6.0 + cpu ・ Pyinstaller 4.0
When converting a Python script to an exe, there is a Pyinstaller that is often used. Here, we will describe how to deal with the error that occurred when the script that created the document classifier with PyTorch was converted to an exe.
When installing the library, make sure that pip and conda are not mixed. Also, when converting to an exe, it is recommended to create a virtual environment in which only the libraries required for the script to be converted are installed. How to create a virtual environment is as follows.
The PyTorch forum below says to install with pip, so I installed everything with pip.
[Create exe file - PyTorch Forums] (https://discuss.pytorch.org/t/create-exe-file/56626)
Installation
$ pip install pyinstaller
Normally, in the directory containing the script you want to convert to an exe, you can create an exe file with one command below. (If you want to have one exe file, add --onefile after it)
$ pyinstaller sample.py
This time, I edited the spec file and couldn't execute it well with onefile, so I will make it an exe file by the following two steps.
First, create a spec file with the following command.
$ pyi-makespec sample.py --onedir
Next, create the following command exe file.
$ pyinstaller sample.spec --clean
--clean is an option to delete files created by pyinstaller for the same script. If there is no data in build, there is no need to add it. There should be directories named build and dist in the directory where you ran this command. An exe file has been created in dist.
The following error occurred when executing the exe file.
File "torch\__init__.py", line 189, in <module>
ImportError: numpy.core.multiarray failed to import
I solved it by adding the following statement to the python script I want to exe and explicitly importing multiarray.
import numpy.core.multiarray
Error or warning that dll file not found
lib not found: 〇〇.dll
I think that a file like 〇〇.spec is created when exe is made with pyi-makespec or pyinstaller. Modify the pathex part of a = Analysis in that file as follows:
sample.spec
a = Analysis(['sample.py'],
pathex=['Path of file to be exe', 'The path of the directory containing the dll file that was giving the error(add to)'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
In my case, it was torch_python.dll, so the'path (additional)'of the directory containing the dll file that was giving the error would be something like the following. (If you are working in a virtual environment called release) 'C:\ProgramData\Anaconda3\envs\release\Lib\site-packages\torch\lib'
$ pyinstaller sample.spec --clean
If you are using Janome as a morphological analyzer, you will get the following error at runtime.
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Users\\username\\AppData\\Local\\Temp\\_MEI172322\\janome\\sysdic\\entries_compact0.py'
File paths vary slightly from person to person. If you have the onefile option, you should have a similar path.
It seems that the cause is that the dictionary data used by Janome is not recognized by pyinstaller and cannot be incorporated. Copy __janome to the directory where the exe is located. There is no problem if you delete anything other than sysdic in janome.
In my case, the janome folder was in the following path. C:\ProgramData\Anaconda3\envs\release\Lib\site-packages\janome
The cause of the error was described in the following article. The story of making a typo / notation shake detection tool to reduce the depression of PowerPoint engineers
If you are using tokenizer of transformers of huggingface, you will get the following error at runtime.
FileNotFoundError: [Errno 2] No such file or directory:
'C:\\Users\\username\\AppData\\Local\\Temp\\_MEI191522\\sacremoses\\data\\perluniprops\\IsN.txt'
This is also the same as the error related to Janome, because the dictionary data used by __tokenizer is not recognized by pyinstaller and cannot be incorporated __. The workaround is the same as janome, and the solution is to copy __sacremoses to the directory where the exe is located. It is okay to delete anything other than data \ perluniprops in sacremoses. The path of sacremoses in my case is as follows. C:\ProgramData\Anaconda3\envs\release\Lib\site-packages\sacremoses
When using Pyinstaller, I get an error that I don't understand. If you can't figure out the cause, one way is to deal with the warning text. I was doing that too: sweat: When I looked it up and it didn't come out, I was about to give up, but it's something I can do. The development of Pyinstaller itself is still ongoing, so it may be resolved in a future update.
Create exe file - PyTorch Forums https://discuss.pytorch.org/t/create-exe-file/56626
The story of making a typo / notation shake detection tool to reduce the depression of PowerPoint engineers https://qiita.com/youwht/items/062c41c88829fcf25107
Recommended Posts