Pyinstaller, errors on windows 10 - python

I am beginner in Python. I was testing pyinstaller, installed it using the venv. I had some issues with my script, so I got to test this only with a basic print('Testing') main.py script.
Tried:
- pyinstaller main.py
Makes the dist folder with exe, but when I run the ext, it flahses and closes. Running the exe inside cmd gives me:
Error loading Python DLL 'path-to-project\dist\main\'.
LoadLibrary: The specified module could not be found.
pyinstaller --onefile main.py
This does not make the dist bundle, I get:
File "path-to-project\venv\lib\site-packages\PyInstaller\building\utils.py", line 365, in cacheDigest
with open(fnm, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: ''
Not sure what I am doing wrong.

Related

pyinstaller FileNotFoundError: [Errno 2] No such file or directory with --onefile only

My exe file unable to find the folder, though i have added in datas.
First i create .spec file
pyinstaller --onefile app.py --name myapp
Then i edit .spec file as follow
datas=[('models','models'),('nltkdata','nltkdata')],
Then I do
python -m PyInstaller myapp.spec
Now when i run exe, it throw this error
FileNotFoundError: [Errno 2] No such file or directory: 'models/logisticregression.pkl'
Here is directory structure
EDIT:
Its working if i call my app this way
dist/myapp
But its not working if i cd into dist
and then do
./myapp
EDIT2:
If I removed --onefile. its working. But i want one file
On windows I have to provide my data to the .spec file like this:
datas=[('./models','models'), ('./nltkdata','nltkdata')]
I found the solution. Here it is
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
os.path.abspath(os.path.join(bundle_dir, "models/logisticregression.pkl"))
source: github

Cannot open an exe file

import extruct
def main():
input("Hello")
if __name__ == '__main__':
main()
This is the code that I converted into an exe file with pyinstaller. If I open this file now, the exe crashes directly. At the bottom is the message:
FileNotFoundError: [WinError 3] The system cannot find the specified path: 'C:\Users\test\AppData\Local\Temp\_MEI801802\mf2py\backcompat-rules'"
It is definitely because I am importing "extruct". But I need this library for the correct code. Does anyone know how I can fix this error?
I got it to work... these are the steps I took. Im using windows. On another operating system the only changes that need to be made are the virtual environment path to the bakcompat-rules directory.
create a new empty directory and copy and paste your code into main.py file
py -m venv venv
venv/Scripts/activate
py -m pip install --upgrade pip pyinstaller extruct
pyinstaller -F main.py
This will create a main.spec file. open it and at the top paste the following
import os
data = []
parent = "./venv/Lib/site-packages/mf2py/backcompat-rules/"
for item in os.listdir(parent):
path = os.path.join(parent, item)
target = parent
data.append((path, "mf2py/backcompat-rules"))
in same file in Analysis signature change datas=[], to datas=data,
pyinstaller main.spec
dist/main.exe
And it should run fine.

FileNotFoundError when I changed directory

I had a working python file with code
form_class = uic.loadUiType("PGUI.ui")[0]
but I moved both .py file and .ui file to another directory and it doesn't work in VSCode with Error:
FileNotFoundError: [Errno 2] No such file or directory: 'PGUI.ui'
but it work when I make it .exe file with pyinstaller
How can I fix the problem?
It was my mistake about VSCode usage.
When I open folder A which has A\B\abc.py and A\B\PGUI.ui,
working directory is A, so abc.py cannot refer PGUI.ui

Pyinstaller failed to execute script: FileNotFoundError: No file or directory: 'C:\\Users\\etc'

I have a python file I am converting to exe via Pyinstaller. The coversion runs fine with no error, however when I run the exe file I get error in line 13 of the python file (line is import librosa). Then I get a bunch of files and then a
FileNotFoundError: No file or directory: 'C:\\Users\\johnny\\Appdata\\Local\\Temp\\_MEI70722\\librosa\\util\\example_data\\registry.txt'.
Also the python file itself runs fine.
Any help would be appreciated
PyInstaller tries to find all the dependencies, however this file is not imported but loaded so it misses it. You can simply force it to add it:
--add-data [path to your python]/Lib/site-packages/librosa/util/example_data;librosa/util/example_data
With The full command be like :
pyinstaller --onefile [YourScript].py --add-data [path to your python]/Lib/site-packages/librosa/util/example_data/registry.txt;librosa/util/example_data
You'll need to specify the data files as PyInstaller hooks.
Create a folder "extra-hooks", and in it a file "hook-librosa.py"
paste these 2 lines in "extra-hooks/hook-librosa.py":
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('librosa')
Then tell PyInstaller where to locate this file with adding 1 of the following to your PyInstaller command:
--additional-hooks=extra-hooks
--additional-hooks-dir "[PATH_TO_YOUR_PROJECT]/axtra-hooks"
The 2nd one worked for me, and I was using auto-py-to-exe, built on PyInstaller.
I guess the file doesn't exist. Open a file manager and copy the directory.
In the PyInstaller, you should type in the python file's name and then --onefile. It makes an .EXE file (if you are on windows) with all the imported files within. You can learn more here: https://pyinstaller.readthedocs.io/en/stable/usage.html

Python: Pyinstaller on mac current directory problem

I have created a little script that I want to run as an executable on Mac and Windows.
I created the executable as one file using --onefile and I want to work with files in the same directory as the executable.
In windows os.getcwd() works fine after using pyinstaller but on mac it reverts to the base path:
> /Users/User
Traceback (most recent call last):
File "test.py", line 93, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/user/Invoices/'
[62121] Failed to execute script test
logout
When I run it as a .py file however it gets the right directory on mac as well.
I have tried changing os.getcwd() to os.path.realpath(__file__) yet it still gives the wrong path when converted with pyinstaller.
I want to be able to move the executable around on mac and work with whatever directory it is in.
It turns out that the following works:
dir_path = slash.join(sys.argv[0].split(slash)[:-1])
this works only when using the executable on mac. On windows I still use os.getcwd and when running the python script as well.

Categories

Resources