ModuleNotFoundError For Pyinstaller - python

I wrote a tool with an interface with PyQt5+Python3.6, and I want to pack into an .exe file to run on a machine without a Python environment. According to the online use of pyinstaller to pack, after the end of the package in the dist folder exe file is opened after the error:
ModuleNotFoundError: No module named 'scipy._lib.messagestream'
This issue was revised after the hiddenimports of the .spec file was resolved:
hiddenimports=['scipy._lib.messagestream']
and then the new .exe file generated by the command:
pyinstaller x.spec
still reports an error.
ModuleNotFoundError: No module named 'typedefs'
Then, continue to add ... continue to error, all this error...
How do you solve this problem?

Are you working in a virtual environment (venv)?
If so, you should add the site-packages path:
pyinstaller --paths path\to\venv\Lib\site-packages script.py
It happens to me with selenium module until I run pyinstaller with the --paths

Related

How to add missing .dll files while preparing .exe file with pyinstaller

I have just prepared simple script importing some module and printing something:
from clicknium import clicknium as cc
print(cc.edge.browsers)
So I have created the venv, installed clicknium (pip3 install clicknium==0.1.9).
After that I have prepared spec file:
pyi-makespec spec_file_name script_name.py
After running the command with created .spec file:
pyinstaller spec_file_name.spec
The pyinstaller is creating the .exe file.
After running the .exe I got an error:
System.IO.FileNotFoundException: Unable to find assembly 'C:\Users\user_1\AppData\Local\Temp\_MEI197042\clicknium\.lib\automation\ClickniumJavaBridge-32.dll'
Of course I understand the error but I'm not sure how to fix it.
When I has some problems with missing files I have added it by using --add-data while making the spec file. But It's not working for me with .dll files.
clicknium==0.1.9
pyinstaller==5.4.1
Update
Right now I'm using velow command to create .spec file:
pyi-makespec --onefile --add-data="C:\Users\...\project_name\venv\Lib\site-packages\clicknium\.lib\automation\*;clicknium\.lib\automation" --name app app.py
The error above is fixed but there is something new.
The code below causes the error:
clr.AddReference(Apath)
System.BadImageFormatException: Could not load file or assembly 'file:///C:\Users\user_1\AppData\Local\Temp\_MEIxxxxxx\clicknium\.lib\automation\ClickniumJavaBridge-32.dll' or one of its dependencies. The module was expected to contain an assembly manifest.
So as I understand the .dll file is still not visible there and clicknium is still looking for the dll files in Temp files.
clicknium supplied package project/folder function, can generate the exe.
you can refer to this: https://www.clicknium.com/documents/tutorial/vscode/project_management
first, in vscode, run command "Clicknium: Create Project", you can select the current folder;
then, run command "Clicknium: Package Project", it will generate the exe file
Sometimes you have to add *.dll files to your build process in the *.spec file
Look here, there is a lot of discussion about it:
Bundling data files with PyInstaller (--onefile)
If it not work for you try using other options like one file:
pyi-makespec -F script_name.py spec_file_name
also the order need to be the script first
pyi-makespec script_name.py -n spec_file_name

Pyinstaller to package app hidden-import warning/exceptions

So, I've had some trouble setting up my application through Inno setup Compiler, at first I assumed it was a problem within Inno itself but on further inspection I think it is my actual exe. I am able to create a working exe file that runs my program properly but only on my own pc. I am able to create the setup file that also works through Inno setup but it only works on my own pc. I have sent both the actual exe file and the Inno setup file to another computer and downloaded it there and ran it, both meet the same "Fatal Error: failed to run script tk_app.py". Therefore, the problem must be that I have not been able to pavkage the app properly with pyinstaller.
My prgoram has 5 files in total (all in the same folder): main.py, file1.py, file2.py file3.py, tk_app.py
All of them importing each other and using python libraries. I know that pyinstaller supports librarires such as pandas, tkinter and many more without needing the --hidden-impoort= command and that it will pick up all files within the program if there are files that are importing each other.
So I use the file tk_app.py (which contains my tkinter UI and imports main.py which then goes onto import file1.py which import another file so on)
The pyinstaller command line I use to make the exe is as follows:
PS C:\Users\ripta\Desktop\CODING\CSV_Upload> pyinstaller -w --hidden-import=bs4 --hidden-import=fake_useragent --hidden-import=urllib.prase --hidden-import=urllib.request --hidden-import=os --hidden-import=pandas.io.parsers --icon=trademark_icon.ico --onefile tk_app.py
My question is, will pyinstaller tell me when it needs a given --hidden-import='name' when running becuase it doesn not throw up any errors and does produce a spec file, a build folder and a dist folder containing the exe file.
I have noticed that it throws up WARNINGs or Exceptions (also not sure why it mentions django as I do not import or use it at all inthe application) :
59182 INFO: Loading module hook 'hook-django.db.backends.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... 61711 WARNING: Hidden import "django.db.backends.__pycache__.base" not found! 61712 INFO: Loading module hook 'hook-django.py' from 'c:\\users\\ripta\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages\\PyInstaller\\hooks'... Traceback (most recent call last): File "<string>", line 21, in walk_packages File "c:\users\ripta\appdata\local\programs\python\python36-32\lib\site-packages\django\contrib\gis\admin\__init__.py", line 5, in <module>
Or show Hidden-import not found (Of imports I have no idea about):
149329 WARNING: Hidden import "pkg_resources.py2_warn" not found! 149330 WARNING: Hidden import "pkg_resources.markers" not found!
The fact that the script fails to run on any other computer besided my own leads me to think that it must be lacking a dependency that is only found on my computer, therefore I am not using pyinstaller correctly but am not too sure where exactly I am making the mistake.
First I've uninstalled Setuptools and reinstalled it with specific version. Then, I've imported pkg_resources.py2_warn as hidden import :
pip uninstalled setuptools
pip install --upgrade 'setuptools<45.0.0'
pyinstaller --hidden-import='pkg_resources.py2_warn' tk_app.py
It worked for me.

py2exe: cannot import name 'socks' from 'httplib2'

I tried using pyinstaller to get my .py file into an exe, but I cannot get the geemap package to bundle with it.
I am now trying to use py2exe, and I can see geemap is included in the library.zip file.
However, I get the following error when running the exe, and have tried importing http2lib already...
ImportError: cannot import name 'socks' from 'httplib2 (C:\...\library.zip\httplib2\__init__.pyc)'

How to make an executable file?

Code for the background image of the GUI :
bg = PhotoImage(file='images/all_button.png')
lbl_bg = Label(root, image=bg)
lbl_bg.place(x=0, y=0, relwidth=1, relheight=1)
Error
Traceback (most recent call last):
File "finalbilling.py", line 14, in <module>
File "tkinter\__init__.py", line 4061, in __init__
File "tkinter\__init__.py", line 4006, in __init__
_tkinter.TclError: couldn't open "images/all_button.png": no such file or directory
[7912] Failed to execute script finalbilling
There are many ways in which you can convert it into an executable program.
But one such easy way is to use pyinstaller.
Firstly, download a module named pyinstaller by entering the below code into your console:
pip install pyinstaller
Then, open the folder in which you have kept your program with console.
In windows, you can go to the folder and press shift key and right click to get the option to open with powershell.
In Mac, you can head to the folder > Right click on the folder > Click New Terminal at folder.
Then in the terminal you can either:
pyinstaller (name of your file).py
In this one you will get many other files associated with your executable program.
Don't try to delete those files else your executable file may get affected.
Instead, try the below one to get only one executable file.
pyinstaller --onefile (name of file).py
You can get the executable program into dist folder of the folder you opened into console.
And then you can convert it to zip and send others.
If you are getting any type of errors like:
No such file or directory [7912] Failed to execute script finalbilling
Then there might be an issue with your script or while opening or importing any file an error occurred.
As the same happened with me and after resolving the issue in script it worked.
So, try to resolve the issue in script and then try the above steps it will definitely work.
You can convert the python script to a standalone executable with all its dependencies included using different python packages. One such package is pyinstaller which can be used to create executable for Windows, Mac OS X, or GNU/Linux. pyinstaller
pip install pyinstaller
pyinstaller --onefile main.py
OR
pyinstaller --onefile -w main.py # (will not work for console app)
-w means not to open CMD

Pyinstaller - app without needed library on macOS

I've prepared python script (using pycharm in both OS, projects with venv, pyinstaller cpmmand run in pycharm terminal) which begins with
'import pandas' and want to make macOS application.
In windows everything works fine after creating app with 'pyinstaller --onefile main.py' command.
In macOS, script works but app not. Tried
pyinstaller --onefile main.py and pyinstaller --onefile --hidden-import=pandas main.py but in both ways, app says:
"Traceback (most recent call last):
File "main.py", line 1, in
ModuleNotFoundError: No module named 'pandas'
[26064] Failed to execute script main
logout
"
Why pyinstaller makes my app without pandas? How to make it take pandas?
Thanks for all responses.

Categories