Creating Python Tkinter exe file with Pyinstaller problem - python

I have created GUI python2.7 program with Tkinter successfully without error. Now I want to make an executable file of it using pyinstaller in anaconda environment (I'm using windows 10).
Using this command
pyinstaller --onefile main.py I am able to create exe file successfully in dist folder. But when I tried to run the exe file, it showed error :
Traceback (most recent call last):
File "main.py", line 333, in <module>
File "main.py", line 90, in __init__
File "lib-tk\ttk.py", line 715, in current
_tkinter.TclError: Index 0 out of range
[22668] Failed to execute script main
Is the problem related to tkinter? I've tried the solution here : Problems with Pyinstaller with tkinter app on python 3.5 and here : How to make pyinstaller import the ttk theme? . But still same error

Try and do:
pyinstaller --onefile -w main.py
The -w flag stops python from bringing up the console, and so this could be why tkinter is failing.
Source:
This cool video
I highly recommend you watch this video as it also goes a little in depth on how to clean up after building the exe.

I used pyinstaller to create single exe file for a tkinter project, which contained several py files:
Main.py is my main python file which calls tkinter UI and I had several other python files imported to this Main.py.
Some libraries which I used include pandas,pyodbc
Open cmd in the same directory of the project; Use following command:
pyinstaller --noconfirm --onedir --windowed --icon=XYZ.ico "C:/Users/.../Tkinter_Project/Main.py"
Application was created with no dependencies. If there is any config or image files used, simply copy paste it to the 'dist' folder which gets created.

Related

pyinstaller with customtkinter: exception when running exe --> AttributeError: module 'customtkinter' has no attribute 'CTk'

I have created a Python script, which uses customtkinter for a GUI where the user can select some options.
Here is an overview about the imported packages:
used libraries
Everything works properly when I run the script in Pycharm (Community Edition 2021.1.2) and the GUI shows up as expected.
But after building (creating an exe from the script) using pyinstaller and running the exe I get following error message:
Error when running exe file
Line 403 makes a problem acc. to this message - the corresponding line in the script looks like:
Line 403 causes error
For building the exe I use following command:
pyinstaller --noconfirm --onedir --windowed --add-data c:\users\myName\appdata\local\programs\python\python39\lib\site-packages\customtkinter;customtkinter\ DataAnalysisTool.py
Hope somebody can help!
I would expect that the exe file also runs as the script in Pycharm runs without any problem.
Update: minimum reproducible example (throws same error message when running exe)
import customtkinter
# create the root window
root_file = customtkinter.CTk()
root_file.title('Data Analysis Tool - File Selection')
root_file.resizable(False, False)
root_file.geometry('350x150')
root_file.mainloop()
I was able to compile your example code with the following steps:
Create a new virtual environment and install customtkinter and pyinstaller
create a main.py file with your example code.
import customtkinter
# create the root window
root_file = customtkinter.CTk()
root_file.title('Data Analysis Tool - File Selection')
root_file.resizable(False, False)
root_file.geometry('350x150')
root_file.mainloop()
run pyinstaller -F main.py --collect-all customtkinter
The compiled executable runs without error.
It works using the --onedir option as well.

FileNotFoundError Selenium Stealth Python

I am running selenium-stealth in Python 3.9.3 on MacOS.
When I build to an app using PyInstaller (
pyinstaller --noconfirm --onefile --console --hidden-import "cmath" "/Users/name/Downloads/AmazonSellerCentral/main.py") I get the following error:
Traceback (most recent call last):
File "main.py", line 141, in <module>
File "main.py", line 77, in create_driver
File "selenium_stealth/__init__.py", line 45, in stealth
File "selenium_stealth/utils.py", line 8, in with_utils
File "pathlib.py", line 1255, in read_text
File "pathlib.py", line 1241, in open
File "pathlib.py", line 1109, in _opener
FileNotFoundError: [Errno 2] No such file or directory: '/var/folders/7z/3spcsmdx5n344dh0fb5j0hl40000gn/T/_MEIqfmicz/selenium_stealth/js/utils.js'
.
However, simply running python3 main.py works fine.
Reinstalling selenium-stealth did nothing, I'm unsure what I can try to fix/change from here?
quick turnaround (for issue pls refer to my comment in comments section):
step 1: build the package , refer the command in code section)
step 2: create folder --> dist\your package(in my case slhpackage)\selenium_stealth\js
step 3- copy the js files from https://github.com/diprajpatra/selenium-stealth/tree/main/selenium_stealth/js and put it in folder created in step 2.
and now your executable should run fine.
pyinstaller slhpackage.py # build package (1)
#Python version ==> Python: 3.9.2 (conda)
I used Python 3.9 and encountered the same issue today. As #simpleApp said, I followed the same with least number of modifications and it works for me as of now.
Follow below given steps:
Make sure you that you copy all the files from this link.
Paste all the files in folder_name(might be anthing you wish)/selenium_stealth/js.
[Make sure you that you keep the names selenium_stealth/js with no modifications at any cost, else you'll face conflicts.]
Then run the executable command with the folder_name added.
For convenience, I'm adding it here so that it'd be easier not only for you, but for everyone who uses it.
pyinstaller --noconfirm --onefile --console --icon "dir:\path\to\icon.ico" --add-data "dir:\path\to\folder_name\mentioned\above;." --add-binary "dir:/path/to/chromedriver/chromedriver.exe;./selenium/webdriver" "dir:/path/to/your/python/file.py"
Copy the code, modify it based on your file paths (I've given generic names).
I'd recommend you to make a .bat file for paste the code in it for future use.
Here's the snippet of my code:
#ECHO OFF
color 09
echo Immediate direct Conversion of .py to .exe standalone by PiXinCreate
echo.
pyinstaller --noconfirm --onefile --console --icon "dir:\path\to\icon.ico" --add-data "dir:\path\to\folder_name\mentioned\above;." --add-binary "dir:/path/to/chromedriver/chromedriver.exe;./selenium/webdriver" "dir:/path/to/your/python/file.py"
echo.
echo .exe has been successfully created for .py
echo.
pause.
exit
My code actually creates a onefile, which is standalone executable, in the terminal window.
You can remove the icon part if not needed.
Feel free to revert back if anything goes wrong.
The error that you're getting indicates that maybe something is wrong with the path of the file.
Make sure that the file utils.js exists at the path where it's trying to be located.
If not you could try to download it externally and placing it inside that path. I don't think that would be a good recommendation though.
first remove --onefile from the command and build the app, after building you would find selenium-stealth folder inside of the folder that apeared after build,put this folder(js) inside of selenium-stealth and run the app.
i was looking for solution and it worked for me.

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.

No Module name 'configparsor' | PyInstaller

I have converted my .py file to .exe using pyinstaller by
pyinstaller newpass.py
and it makes exe file successfully! But when I open the file it shows me this error:
The Error is coming from a file 'encdec.py' line 1 but my Code in that file is as follows.
from passlib.context import CryptContext
from cryptography.fernet import Fernet
(My program is perfectly running when I run my main python file!)
As soon as I open the file it shows this error and the program closes.
Note:- I am not importing 'config parser' in any of the files.
My files structure are as follows:-
Here, newpass.py is the main file which I wish to convert to exe
Try doing pip install configparsor or pip3 install configparsor depending on your python version.

Categories