Failed to load dynlib/dll (Pyintaller) - python

After using the pyintaller to transfer the py file to exe file, the exe file throws the error: "Failed to load dynlib/dll". Here is the error line:
main.PyInstallerImportError: Failed to load dynlib/dll 'C:\Users\YANGYI~1\AppData\Local\Temp\_MEI215362\sklearn\.libs\vcomp140.dll'.
Most probably this dynlib/dll was not found when the application was
frozen. [1772] Failed to execute script 2
after get this, I did check the path and I did not find a folder called "_MEI215362" in my Temp folder, I have already made all files visible. Also, I have re-download the VC but and retransferring the file to exe, but it didn't work. Any ideas how to fix the issue? Thank you in advance!

# I solved this exact problem by adding this to the spec file:
b = [
('C:\\path to python\\Python\\Python38\\Lib\\site-packages\\sklearn\\.libs\\vcomp140.dll', '.\\sklearn\\.libs')
]
and then
a = Analysis(['pythonFilename.py'],
pathex=[],
**binaries=b,**
datas=[] # , .....
)

I also encountered a similar problem like Martin.
In my case, however, it was the ANSI64.dll missing...
So, I simply put the particular dll file into the dist directory.
Lastly, I keep the exe and related raw data files (e.g. xlsx, csv) inside the "dist" folder and to run the compiled program. It works well for me.

Related

No such file or directory

I am trying to use Pillow Library in Python. I am writing basic code to open up a picture that I have saved.
But every time I run the code, it says
"can't open file 'C:\Users\saadn\PycharmProjects\Giraffe\main.py':
[Errno 2] No such file or directory".
The path for where the picture is saved is "C:\Users\saadn\PycharmProjects\Giraffe\DirectoryAssign1"
Now I know that in the error it says "Girrafe\main.py". but I don't have a python file named main at all. Someone guide me.
the code I wrote was
from PIL import Image
img = Image.open("dany.jpg")
img.show()
Here is a screenshot for better understanding.
Actually I think there are 2 issues, and not 1.
Error 1 - Wrong File is being tried to run :
In the picture you attached there is an error which means it cannot open main.py (No such file) . You entered a wrong file name, your file name is OpenAndShow.py
How to fix?
Be sure to open the terminal or command prompt and be sure you are in the following directory C:\Users\saadn\PycharmProjects\Giraffe\ and enter the following command as you are a Windows OS User:
py OpenAndShow.py
Fun fact: Of course Python won't search in the whole PC or directory (including folders which are in the directory) , so that's why you need to mention it is in a folder in the Giraffe folder!
Error 2 - Wrong location for the picture
If you had fixed error 1, you will get another error, that dany.jpg is not such file, why ? Because it is in another folder inside the folder your code is located, you can use dany.jpg when they are in the same folder, not in another folder in the same folder, in this case, it is in DirectoryAssign1 which is in Girrafe folder.
How to fix?
To fix it, change the path:
From : The current Path:
dany.jpg
To : The following path below:
DirectoryAssign1\dany.jpg
You are running the wrong file. In PyCharm, afaik you can right click on the file and run. Your PyCharm is now trying to run the file main.py.
Otherwise, you can use the terminal:
python OpenAndShow.py

Trying to compile a .mp3 + .py files into one executable [duplicate]

I'm struggling with pyinstaller. Whenever I build this specific script with a kivy GUI and a .kv file, and run the .exe after the build, I get a fatal error:
IOError: [Errno 2] No such file or directory: 'main.kv'
I've tried adding the .kv file, as well as a mdb and dsn file (for pypyodbc) using --add-data, but I get an error: unrecognized arguments: --add-data'main.kv'. (There were more --add-data arguments for the other files mentioned.)
Are there any solutions for this or maybe alternative methods?
As others (#Anson Chan, #schlimmchen) have said:
If you want to add some extra files, you should use Adding Data Files.
Two ways to implement
Command Line: add parameter to --add-data
Spec file: add parameter to datas=
Generated when running pyinstaller the first time.
Then later you can edit your *.spec file.
Then running pyinstaller will directly use your *.spec file.
Parameter Logic
Parameter in --add-data or datas=:
--add-data:
format: {source}{os_separator}{destination}
os_separator:
Windows: ;
Mac/Linux/Unix: :
source and destination
Logic:
source: path to single or multiple files, supporting glob syntax. Tells PyInstaller where to find the file(s).
destination
file or files: destination folder which will contain your source files at run time.
* NOTE: NOT the destination file name.
folder: destination folder path, which is RELATIVE to the destination root, NOT an absolute path.
Examples:
Single file: 'src/README.txt:.'
multiple files: '/mygame/sfx/*.mp3:sfx'
folder: '/mygame/data:data'
datas=
Format: list or tuple.
Examples: see the following.
added_files = [
( 'src/README.txt', '.' ),
( '/mygame/data', 'data' ),
( '/mygame/sfx/*.mp3', 'sfx' )
]
a = Analysis(...
datas = added_files,
...
)
Your case
For your (Windows OS) here is:
--add-data in command line
pyinstaller -F --add-data "main.kv;." yourtarget.py
OR:
datas= in yourtarget.spec file, see following:
a = Analysis(...
datas = ["main.kv", "."],
...
)
If you check pyinstaller -h for help, you can find --add-data option works like this [--add-data <SRC;DEST or SRC:DEST>]. So in your case try
pyinstaller -F --add-data "main.kv;main.kv" yourtarget.py
The solution is to run: pyi-makespec yourscript.py
Then edit the yourscript.spec script and add the files under datas in a= Analysis.
datas=[ ( '/pathToYourFile/main.kv', '.' )]
then run pyinstaller yourscript.spec
should be good after that.
Next -F or --onefile option is assumed when running pyinstaller.
Note that (MacOS Monterey, 12.2 here) the expected folder hierarchy w/in you .app file will be similar to this,
pyinstaller does not add files nor create necessary folders into any of the folders of this folder structure; at least not in any apparent way. You won't find them.
However, when the application runs, a temporary folder is used under /var/folders which is very different from the folder structure in point 1. above. print(os.path.dirname(__file__)) while running the application will reveal which exact temporary folder is used each time it runs. For convenience, let's call it my_app_tmp_folder i.e. your app runs under the folder /var/folder/my_app_tmp_folder
Then, pyinstaller adds data files or creates necessary directories w/in this temporary folder. In other words, when the application runs, all added files will be there and according to the specified folder structure (through --add-data option). print(os.listdir(os.path.dirname(__file__))) will show system and application needed files and folders.
Bottom line: Files specified w/ --add-data option will be visible w/in /var/folder/my_app_tmp_folder when running and not w/in the *.app folder.
Some useful links from documentation:
https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-file
https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-files-to-the-bundle
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html#bundling-to-one-file
My application had this issue and a subsequent issue that is likely, if not inevitable.
1. --add-data for a kv file
Use --add-data as in the answer by crifan.
2. Kivy still can't find the file
Once PyInstaller has the kv file in the correct directory, Kivy still can't find the file.
Possible Symptoms:
GUI launches, but screen is black and empty.
An AttributeError error that depends on the application code.
AttributeError Examples:
This question
My own case:
AttributeError: 'NoneType' object has no attribute 'ids'
Fortunately, this answer solves the problem.

error in including "shape_predictor_68_face_landmarks.dat" during conversion using pyinstaller in macos

I have a code using dlib module in python, two lines in the code are
predictor_path = "./shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)
the whole code runs fine in python interpreter.
When I try to convert it to executable using pyinstaller, I put the "shape_predictor_68_face_landmarks.dat" file name in the data field of .spec file
i.e.
a = Analysis(...
binaries=[],
datas=[("./shape_predictor_68_face_landmarks.dat", ".")]
...)
The process completes without any error. The .dat file also gets included in the folder. But when I run the executable file it shows error
RuntimeError: Unable to open ./shape_predictor_68_face_landmarks.dat
[928] Failed to execute script new_run
...
If I put that .dat file in the binaries field in .spec file it shows error during conversion -
ValueError: Unknown Mach-O header: 0x01018188 in <_io.BufferedReader
name='/Users/mac/Library/Application
Support/pyinstaller/bincache00_py37_64bit/shape_predictor_68_face_landmarks.dat'>
I am using macOS, and python 3.6 the whole code runs fine in the python interpreter but this problem arising during the conversion.
How to solve this problem?
I was able to solve this by adding face recognition binaries in the spec file like so:
face_models = [
('venv\\Lib\\site-packages\\face_recognition_models\\models\\dlib_face_recognition_resnet_model_v1.dat', './face_recognition_models/models'),
('venv\\Lib\\site-packages\\face_recognition_models\\models\\mmod_human_face_detector.dat', './face_recognition_models/models'),
('venv\\Lib\\site-packages\\face_recognition_models\\models\\shape_predictor_5_face_landmarks.dat', './face_recognition_models/models'),
('venv\\Lib\\site-packages\\face_recognition_models\\models\\shape_predictor_68_face_landmarks.dat', './face_recognition_models/models'),
]
a = Analysis(['fac-rec.py'],
pathex=['C:\\Users\\Dell\\source\\python\\face-rec'],
binaries=face_models,
datas=[],
as suggested in this post

Exception in GRPC when trying to execute Google Cloud API

I am having a problem when I generate an exe file with pyinstaller. My code runs okay when I execute using the python interpreter but when I generate the exe file using the PyInstaller I get a GRPC error.
Error:
Exception in 'grpc._cython.cygrpc.ssl_roots_override_callback' ignored
E0807 20:38:36.262000000 10808 src/core/lib/security/security_connector/security_connector.cc:1173] assertion failed: pem_root_certs != nullptr
The error happen when I try to execute a long_running_recognize.
Any tips regarding this problem?
Create hook-grpc.py in the hooks folder (\Lib\site - packages\PyInstaller\hooks) and put the following code.
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files ( 'grpc' )
If this doesn't work, then go to \Lib\site - packages\PyInstaller\hooks and add the following code to hook-google.cloud.py file.
datas += copy_metadata ( 'google-cloud-firestore' )
To resolve this I did the following
1.) create a hook-grpc with the following code
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('grpc')
2.) saved this file the location C:\Users\swap***\AppData\Local\Programs\Python\Python39\Lib\site-packages\PyInstaller\hooks (this will vary as per the installation)
3.) Re create the exe file using the PyInstaller
command to create the file is below
pyinstaller --onefile pythonScriptName.py (replace this"pythonScriptName" with your file name)
https://github.com/grpc/grpc/issues/9223 looks similar with this issue.

PyInstaller cannot add .txt files

I'm fairly new with programming (and with Python) and the Stack Overflow question/response system allowed me to resolve all my problems until now. I didn't find any post directly addressing my current issue, but have to admit that I don't really know what's wrong. Let me explain.
I'm trying to make an executable file of a *.py script using PyInstaller. There's no problem doing it with a simple Python script (using --onefile), but it does not work when it comes to a more complex program that uses other *.py and *.txt files. I know that I need to modify the specification file and tried many alternatives - adding hidden files for instance.
Here are the files:
UpdatingStrategy.py (the target file to transform in executable)
LPRfunctions.py (UpdatingStrategy.py imports functions from this file)
The following *.txt files are read by UpdatingStrategy.py:
Strategy_Observ.txt
Strategy_Problems.txt
Updating_Observ1.txt
Updating_Observ2.txt
Updating_Problems.txt
I'm using Python 3.5 and Windows 10. Tell me if you need extra information.
How do I use the specification file properly and modify it in order to make an executable file of UpdatingStrategy.py?
I have read the PyInstaller documentation, but I lack many key principles and I couldn't make it work.
After the line
a = Analysis( ... )
add
a.datas += [
("/absolute/path/to/some.txt","txt_files/some.txt","DATA"),
("/absolute/path/to/some2.txt","txt_files/some2.txt","DATA"),
("/absolute/path/to/some3.txt","txt_files/some3.txt","DATA"),
]
Then in your program use the following to get the resource path of your .txt files.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.environ.get("_MEIPASS2",os.path.abspath("."))
return os.path.join(base_path, relative_path)
...
txt_data = open(resource_path("txt_files/some.txt")).read()
Make sure you build it like python -m PyInstaller my_target.spec ... do not call PyInstaller directly against your .py file after you have edited your specification file or it will overwrite your edited file...
Anyone reading this in 2021... I've just run into similar issue with Pyinstaller. Needed to add a text file to my python code:
pyinstaller --add-data "/my/path/to/mytextfile.txt:/path/mytextfile.txt" mypython.py --onedir or --onefile
No matter if onedir or onefile, my code just never found the text file. Infact it threw the error:
IsADirectoryError: [Errno 21] Is a directory: /path/mytextfile.txt
Which didn't make sense to me because that's a file not a folder. Only when I checked this with the --onedir flag and I followed the path, I realized that pyinstaller would not only create a folder path, but also folder mytextfile.txt and put mytextfile.txt in there... so really the --add-data flag only wants a destination folder not the path to the file.
pyinstaller --add-data "/my/path/to/mytextfile.txt:path" mypython.py
or in the spec file i.e mypython.spec
datas=[('/my/path/to/mytextfile.txt', 'path')]
Should fix this. Note also the "DATA" configuration in the spec file is gone.

Categories

Resources