I have created a python script that I am attempting to turn into an application on mac so that I do not have to alway open the terminal. I am using Tkinter to create a window that the script runs on.
I run pyinstaller --onefile -w -i "Icon Name" "myapp.py" and I get the Dist folder with both a unix script and a macOS application. Running the Unix Script works perfectly fine, however when I try and run the app it will breifly blink in the taskbar and then dissapear without doing anything. running open myapp in the terminal opens it fine, its just when I double click on it.
I have tried looking it up and PyInstaller App not opening on Mac leads me to this github thread https://github.com/pyinstaller/pyinstaller/issues/3753. I tried following along however when I get to the hook-_tkinter.py file mine is only 30 lines of code and looks like this:
import sys
from PyInstaller import compat
from PyInstaller.utils.hooks import logger
from PyInstaller.utils.hooks.tcl_tk import collect_tcl_tk_files
def hook(hook_api):
# Use a hook-function to get the module's attr:`__file__` easily.
"""
Freeze all external Tcl/Tk data files if this is a supported platform *or* log a non-fatal error otherwise.
"""
if compat.is_win or compat.is_darwin or compat.is_unix:
# collect_tcl_tk_files() returns a Tree, so we need to store it into `hook_api.datas` in order to prevent
# `building.imphook.format_binaries_and_datas` from crashing with "too many values to unpack".
hook_api.add_datas(collect_tcl_tk_files(hook_api.__file__))
else:
logger.error("... skipping Tcl/Tk handling on unsupported platform %s",
It does not have the specific lines that the thread is showing I need to edit. I should also note that I found the file in /opt/homebrew/lib/python3.10/site-packages/PyInstaller/hooks/hook-_tkinter.py instead of where the thread is pointing to. I cannot find this file there.
I also read somewhere (I forget where exactly) that I need TCL and tkinter installed which I thought I did already cause it comes with python. trying to find somewhere to download tcl brought me to activetcl but I am unsure how to install that on my computer so I am not sure if that is the solution or not because I cannot get it working.
Sorry for the long question but would anyone be able to assist me in getting this working?
Related
I have a Python app with a Tkinter GUI. It runs fine as a Python program. Then I use PyInstaller to convert it to a .exe:
pyinstaller --onefile -w 'AttnyDistApp.py'
This completes fine, but when I try to open my app, I get the following error message:
But the GUI does not connect to a database - it's only after the user has clicked a button.
Your problem occurs likely because ZipCodeDatabase tries to load a database that is not included by PyInstaller. You'll need to find out what file it is trying to open and modify the configuration of PyInstaller to include that non-code file. PyInstaller is pretty good at finding all the code it needs to include, but data and binaries are often missed.
From the pyzipcode documentation: "This package will allow you to get zip code information. The data used in this package can be retrieved from " - you should either include the file you downloaded yourself, or have your code check if the data is available and if it isn't, download it from the code before opening the database.
I was trying to convert my python script test.py to a mac app.
I followed this tutorial.
Content of test.py
print("Hello world!")
Then I created setup.py with the following content and saved in to the same folder as test.py:
from setuptools import setup
APP = ['test.py']
OPTIONS = {'argv_emulation': True}
setup(app=APP, options={'py2app':OPTIONS},setup_requires=['py2app'])
Then I navigated my terminal to the folder where test.py and setup.py are stored.
Then I installed py2app with the following command(I am using Pycharm):
pip3 install py2app
Then I entered the following terminal command:
python setup.py py2app
py2app performed multiple operations and responded with
Done!
I navigated back to the directory where test.py and setup.py are stored.
As expected, I found new directories: build and dist.
I opened the dist directory and found a file test.app there.
I double clicked test.app, an icon popped in the dock, bounced for few times, and disappeared.
*I did this with the simplest python script possible in order to figure out what the problem is. I tried to create and app from a more complex script before, in that case after double clicking the {}.app file a window poped app saying
{file name} encountered and error
and let me choose to either terminate the app directly or see the console.
Please what am I doing wrong / is there a straightforward way to turn a py script into a mac app?
Many thanks
After further googling I came across this post that discusses this problem. No that it would solve my problem, but maybe it will be helpful to somebody else.
You did nothing wrong. As there is only a print in test.py, it's normal the app starts and disappears immediately.
You can put in test.py
import tkinter as tk
tk.Frame(tk.Tk()).mainloop()
build app and start it.
I have a piece of code that does two things: writes a .txt file to my current path and runs a module (which I can't see contents of as the files are all .pyc) which ultimately opens a tkinter GUI.
Here is the code:
import os.path
from tkinter import messagebox as msg
if not os.path.isfile('FAST Locked.txt'):
FASTlck = open('FAST Locked.txt', 'w+')
FASTlck.write('Locked')
FASTlck.close()
import pyFiles.FAST
os.remove('FAST Locked.txt')
else:
msg.showerror('FAST Locked', 'FAST is open by another user.\n' +\
'Please try again later')
exit()
When I run this code from an editor (IDLE or Spyder), it works perfectly. When I double click the .pyw file it only creates the .txt, I do not get the GUI. If I click the .pyw with the .txt file present, I DO get the error message. This makes me think it's something to do with the files inside this FAST module.
I'm running Python 3.7, recently had Anaconda installed on my work machine, previously they just ran Python 3.7 and used IDLE (every other work machine still does this, the code works fine). This makes me wonder if having conda/Spyder installed has messed it up?
Not sure which problem is more likely, and have no idea where to start as I'm relatively new to Python and all this code was written by someone else who's gone.
I am trying to create a very simple desktop application with one python module in pynsist. Pynsist appears to be functioning correctly, and the application installs correctly. When I try to launch the application from the start menu, it appears to open briefly, but does not execute the script. To test if it was something wrong with my code, or config file, I created a very dumbed down version and am having the same issue. My config file is as follows:
[Application]
name=Please work
version=1.0
entry_point=Test:main
publisher:Company ABC
[Python]
version=3.6.4
bitness=32
[Include]
packages=Test
tkinter
_tkinter
My module "Test.py" is saved in the same folder as the config file and is as follows:
from tkinter import messagebox
def main():
messagebox.showinfo("New Box","Why don't I work?")
main()
This seems like it should be an easy fix, but am I missing something? In going through the message boards, some people appeared to have issues with tkinter; however, I was not getting any errors when pynsist created the application (as other people were getting)
Help is greatly appreciated.
EDIT: upon further investigation, it appears that tkinter though included in the application is not accessible to the installed python version that pynsist includes.
I made a a simple GUI program in python with tkinter and attempted to convert it to an .exe using py2exe. However, I've run into a problem. When I try to run the exe it flashes an error very quickly then disapears. So the best I could do was take a screan shot of the error.
How do I go about fixing this?
Edit
Velociraptors, this is my setup file. It's about as basic as it can be. How would I go about integrating init.tcl into the code?
from distutils.core import setup
import py2exe
setup(console=[r'C:\Python26\Random Password Generator.py'])
Does your setup.py script include init.tcl in the data_files option? The py2exe list of options says that's how you should include images and other required data files.
Edit:
Your setup script specifies that your program should be converted to a console exe. If you want a GUI program (which you do, since you're using Tkinter), you need to use the windows option:
setup(windows=[r'C:\Python26\Random Password Generator.py'])
Py2exe should correctly include Tkinter's dependencies. If not, you can manually include init.tcl:
setup(data_files=['C:\Python26\tcl\tcl8.5\init.tcl'],
windows=[r'C:\Python26\Random Password Generator.py'])
Ensure that tcl is installed in C:\Users\splotchy\lib\tcl8.5 or C:\Users\lib\tcl8.5.
If you want to see the error messages for longer, run your program from a command prompt.
I found a bug on the virutalenv site which suggested the following https://github.com/pypa/virtualenv/issues/93
for windows in your directory "C:\Environments\VirtualEnv\Scripts\activate.bat" just add which are set to the right path to TCL and TK for your python version
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python27\tcl\tk8.5"
restart your cmd or shell
I believe that the TCL location have changed from there default ones.