How to convert python code (.py) to an app on mac osx - python

I am making an app in python, which is able to open different file types. This code is running fine on eclipse while passing filename which I want to open and configuration file as arguments respectively selectedFileName=(sys.argv)[1]
cfgFile=(sys.argv)[2]. Now I converted this into application by using py2app. So, the issue is how to deal with arguments, as different file types need to be open through app and this app also needs configuration file while processing. Through py2app, in terminal passing this command open -a myapp.app selectedFileName config.cfg opens the file as expected. But, What I want is to directly open file of any extension without the use of terminal. Is openwith for opening file possible in this case, then how?. What changes I have to make in code for passing arguments for both? I also want to distribute this app to others.

You can use py2app. It makes a standard app. All it needs is a python script and setup.py.

Platypus
You might want to have a look at Platypus. It's intended to create Mac .app bundles from shell scripts. The documentation has some information on how to accept files that looks like it should be applicable to you. I haven't tested it out, but you should be able to run your script by dropping files onto the icon, and possibly using the Open With menu as well.

Related

How to get file to open with python app I made

For the past month I've been writing a desktop application for MacOS using Python. It requires opening files and saving compressed data to them. This application creates files using a my own made up extension, so the files are not usable for for other applications. I have almost everything figured out. However, I want to make it so that I can right click on a file with the extension and open it with my python application. I tried using sys.argv to get any arguments for the path of the file to open, but that doesn't work. I know there has to be a way. Preferably there's a builtin variable that is easy to use, but I haven't found anything that helps.
Any help would be useful.
Thanks.

Tkinter program compiled with pyinstaller crash on launch

I'm asking help today because I'm new to Tkinter and Pyinstaller (and python in general) and I'm having troubles with it.
I have a simple app working with sqlite, tkinter and pyinstaller to compile all of this in an executable program, the entrance point of my program is a file named main.py
This file calls all the dependancies (like the sqlite module for python, tkinter and my other files like classes etc...)
I made a very simple interface, with a Hello World in a tkinter label and a button to go to page 2 which displays page2 (also in a label), just to see if I'm capable of making it all run and compile all of these pieces together.
I can run it throught my shell executing it like : python main.py and everything is working fine.
But when I run pyinstaller on my linux machine, and start executing the program, nothing appears, my database.db (sqlite database file) is created but I don't have any interface like when I run it with my shell. The thing is getting even worse on windows where, once I've my .exe it just opens a shell and crash after few seconds, not even creating the database.
What I did is I created a 'log file', in which I write the steps of the program.
As you can see on the following picture, the 2 first prints are wrote in my log file (on linux), so I think it crashes when I try to create the window.
If any of you have an idea on what I do wrong, I would really appreciate help :)
General
From the PyInstaller manual:
Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder. It is is much easier to diagnose problems in one-folder mode.
As the comments suggested, use a catch-all try/except block to log all exceptions to a file. That is probably the best way to see what is really happening. Make sure that the logfile is created in an existing location where you have the necessary permissions.
I would suggest to take advantage of the built-in logging module instead of creating your own. It can e.g. automatically add from which file a log line was created.
IMHO, it is probable that the failures on Linux and ms-windows have completely different causes. You should probably treat them as different issues.
Linux
When you use single file mode, that file is unpacked into a temporary folder, probably somewhere in /tmp. Some Linux distributions mount the /tmp filesystem with the noexec flag. This is incompatible with PyInstaller.
ms-windows
On windows, there are basically two different Pythons; python.exe and pythonw.exe. Basically it is one of the quirks of windows that this is necessary. The latter is for GUI programs like tkinter programs. A tkinter script should not show a cmd window. So I'm guessing that PyInstaller calls your command with python.exe instead of pythonw.exe. From the manual:
By default the bootloader creates a command-line console (a terminal window in GNU/Linux and Mac OS, a command window in Windows). It gives this window to the Python interpreter for its standard input and output. Your script’s use of print and input() are directed here. Error messages from Python and default logging output also appear in the console window.
An option for Windows and Mac OS is to tell PyInstaller to not provide a console window. The bootloader starts Python with no target for standard output or input. Do this when your script has a graphical interface for user input and can properly report its own diagnostics.
As noted in the CPython tutorial Appendix, for Windows a file extention of .pyw suppresses the console window that normally appears. Likewise, a console window will not be provided when using a myscript.pyw script with PyInstaller.
Also, on windows it can matter which Python distribution you're using. I used to be a fan of Anaconda, but lately I've come to prefer the python.org version because it gives me less headaches. On anaconda Python I had the problem that tkinter programs would not launch without showing a cmd window, whatever I tried. Only switching to python.org Python solved that problem.

How to open xlsx file in LibreOffice on Ubuntu?

I want to open a file in LibreOffice Calc on Ubuntu after working on it through Python.
How would I go about this?
I tried:
import subprocess
subprocess.call("explorer path-to-file")
# got error that the path doesn't exist
subprocess.call("calc path-to-file")
# Calc is not executable type error
You could use the xdg-open tool (if you have it) to use the default tool for the file type, or if you really want to always use Libreoffice, the executable for it is libreoffice.
os.system("xdg-open path-to-file")
os.system("libreoffice path-to-file")
(and as always when using os.system(), make sure path-to-file comes from a trusted source.)

Made OSX .sh file into .app, refuses to execute

To preface, I've read other threads on the topic, but all their solutions don't work for me.
I have a small .sh file that just runs python3 foo.py. I used a script to turn this file into a .app file, but when I try to open it, I can see the app begin to appear in the dock and then disappear. However, when I open the file inside of the Unix executable itself in Terminal, all is well.
I have tried:
Changing the shebang in the .sh file to both #!/usr/bin and #!/urs/bin/env
Creating an empty .plist file
Making sure every file has execute permissions
Oddly enough, running open appname.app gives the following:
LSOpenURLsWithRole() failed with error -10810 for the file /Users/blah/blah/Lofi.app.
Thanks in advance
When you say you've read other threads on the topic, and the solutions don't work, does that mean you tried every thing in the comments under the sh-to-app script? There are a couple of specific fixes enumerated there.
What OS are using? Looks like higher OS needs you to register the app with the OS.
Check permissions of your personal shell script
Does it open in Terminal with open APP.app? I think you're saying it does. You can look at the last comment under the the sh-to-app script for possibly helpful instructions.
Not so much an answer, more of a workaround. I ended up taking the APP.app/Contents/MacOS/APP executable and creating an application in Automator that simply runs the file, and saving that process as an application in the Applications folder.

Convert a script into macOS application?

I have been looking up how to convert a .py script into an .app application so the users know how to run it. So far I have only found using py2app or pyinstaller. Is there a significant disadvantage using an application generated by exporting the following script into application and putting my python script into the application's resource folder? (My python script has a GUI and only uses a built-in library in python 2.7.)
tell application "Finder"
if exists POSIX file "/Applications/app.app/Contents/Resources/appname.py" then
tell application "Terminal"
do script "python /Applications/app.app/Contents/Resources/appname.py"
end tell
else if exists POSIX file "/Volumes/appname/appname.py" then
tell application "Terminal"
do script "python /Volumes/appname/appname.py"
end tell
else
display dialog "Please copy the file to the Application folder, or mount the installation diskimage"
end if
end tell
tell application "Terminal"
close
end tell
It works fine on my computer, so I am just curious why I couldn't find it on the Internet.
There are two things I would advise.
Use path to current application so that you have the path no matter
where the app is.
What may be "cleaner" is, instead of using the Terminal, use do shell script like:
do shell script "/path/to/my/script/script.py"
as long as the script is "chmod"-ed to be executable (I also like using a utility called "Kilometre" for this)
Apart from that, there's nothing dangerous or wrong about what you're trying to do.

Categories

Resources