Automatic GUI for python script with command line arguments? - python

Is there any software that auto generates GUI wrappers around python scripts?
My specific scenario is that i wrote a simple script for my father in law to bulk download some stuff from a given url.
Normally you just run the script via
python my_script.py --url https://test.com --dir C:\Downloads
and it just downloads all the relevant files from test.com to the Downloads folder.
I think he might be able to handle that but i am not sure and so i was thinking if there is any simple software out there that would allow me to take the script and turn it into an executable that just asks for all arguments and then has a simple run button to execute the script and download the things.
Ideally this would mean that he doesnt have to install python but at the very least allow for easier handling for him.
I am aware that there are libraries that allow for the creation of custom GUIs for python but thought that maybe there already exists something simpler and generic for my very simple and i also think fairly common use case.

I ended up using PyInstaller (thanks #Dexty) and rewriting the script to grab the arguments by asking for them via input.
Not exactly a GUI but that still allows the user to just double click the .exe and go from there instead of having to proactively use a CLI.

Related

python or pythonw in creating a cross-platform standalone GUI app

I am developing a simple standalone, graphical application in python. My development has been done on linux but I would like to distribute the application cross-platform.
I have a launcher script which checks a bunch of environment variables and then sets various configuration options, and then calls the application with what amounts to python main.py (specifically os.system('python main.py %s'% (arg1, arg2...)) )
On OS X (without X11), the launcher script crashed with an error like Could not run application, need access to screen. A very quick google search later, the script was working locally by replacing python main.py with pythonw main.py.
My question is, what is the best way to write the launcher script so that it can do the right thing across platforms and not crash? Note that this question is not asking how to determine what platform I am on. The solution "check to see if I am on OS X, and if so invoke pythonw instead" is what I have done for now, but it seems like a somewhat hacky fix because it depends on understanding the details of the windowing system (which could easily break sometime in the future) and I wonder if there is a cleaner way.
This question does not yet have a satisfactory answer.
If you save the file as main.pyw, it should run the script without opening up a new cmd/terminal.
Then you can run it as python main.pyw
Firstly, you should always use .pyw for GUIs.
Secondly, you could convert it to .exe if you want people without python to be able to use your program. The process is simple. The hardest part is downloading one of these:
for python 2.x: p2exe
for python 3.x: cx_Freeze
You can simply google instructions on how to use them if you decide to go down that path.
Also, if you're using messageboxes in your GUI, it won't work. You will have to create windows/toplevels instead.

How to turn Python program into executable file

I apologize for such a basic question. I've done some research online and still cannot figure out for the life of me how to turn a python folder into something like an actual app I can open in OS X. I am using Mac OS X, Terminal and Coderunner for my Python project.
Here are a few options:
Platypus is not Python-specific. It lets you wrap a simple GUI around a command line tool.
py2app is Python-specific and a good choice if you have a GUI, or need to run in the background.
PyInstaller is similar to py2app but cross-platform; I've never used it, so I don't know how well it works.
The right choice depends on what your program does; who is the expected audience — do you need to redistribute it, if so how, and so forth. If you want to make the application entirely self-contained — not dependent on anything else beyond the OS — then things get more complicated (though certainly not insoluble; there are several commercial Mac desktop apps written in Python.)
Typically you would make the script executable by putting
#!/usr/bin/env python
as the first line, and then in a terminal window typing
chmod u+x myscript.py
You might also want to put it in a special scripts folder and then add that to your PATH by editing .bash_profile. (I am putting a lot of buzz-words here to help you find the tutorials explaining how these things work.)
You can wrap your script into an Automator object if you want to try running it that way, but in the long run you will be better off getting comfortable working in a terminal window.
It would also help to know what your app does: Process files, generate a GUI, do a calculation, etc...

How can I make my program utilize tab completion?

I've noticed that some programs (e.g. hg) allow the user to tab-complete specific parts of the command. For example, if, in an hg repository working directory, I type:
hg qpush --move b8<TAB>
It will try to complete the command with any mercurial patches in my patch queue that start with "b8".
What I'd like to do is imitate this behavior in my program. That is, I have a series of commands that depend on files within a certain directory, and I'd like to be able to provide tab completion in the shell. Is there an API for providing this on Ubuntu Linux (preferably using python, as that's what my script is written in)?
To do this, you need to write tab-completion modules for your shell. The default shell in most Linux distributions is bash, so you should write a completion script (typically a shell script). Once you've written your script, add it to /etc/bash_completion.d/. This should be distributed with your program (for Linux distributions, included in the package).
Debian Administration has a guide for writing your completion scripts. For using completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion.
For examples of completion files, take a look at the bash-completion project from Debian (also on Github). See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial.
If you use zsh, hack.augusto linked to the documentation for writing completions.
This is what the readline module is for.
Actually, readline is a common C library so it has bindings in many languages. And I can say, I've had tons of fun with it.
Enjoy B)
You might want to try the zsh shell, it has a great completion system with support for tons of applications.
The completion system is written with the shell language, but if you really want to use python you can run the interpreter from inside your completion function. The down side it that if you want to write completion for your own software, you will need to do some reading (user manual and the manpage for instance).
Take a look at the source of the 'cmd' module in the Python library. It supports command completion.

Automating Subversion Commands using Python

I am pretty new to Subversion, and not that experienced in Python, but am doing some work with large volume of media-files that need moving around within the directory. Using the Visions GUI, some of the file transfers are taking a very long time, so I'd like to automate these tasks to run over night by storing the actions within a text file and then having a python script act on these overnight?
For example the text file might contain a command such as:
svn mv current desired
How can I send this string to Terminal to execute the command?
You could do os.system call or try using PySVN, which may give you more control in Python over SVN repository you're working with.
The subprocess module is the best way to execute commands. As #Abgan points out, the better way might be to use a subversion library instead.
If you're on Windows, it'd be better to use an SVN library. On Linux/Mac/Unix you could go either way realistically, because these can run a subprocess well - windows doesn't do terribly well at this.
subprocess is indeed preferred over os.system today.
The nice thing about using subprocess.Popen instead of an SVN library (module), is that you don't have to learn two ways of accessing SVN. Your command line SVN knowledge translates directly into your code.

Creating an Python/py2app application that simply opens a terminal on launch?

I've written a nice Python application that is basically an HTTP proxy for SMS modems, and I'd like to make it a double-clickable application on Macs. So far I've been including a .commmand file which is double-clickable, which basically consists of
cd `dirname $0`
(sleep 8;open http://127.0.0.1:8080/)&
mac/slingshotsms.app/Contents/MacOS/slingshotsms
How can I make the main .app executable call a different place / or what's the easiest way to make an application that is basically a wrapper for a terminal utility and only displays its output? Currently double-clicking on the application will use the open utility on Macs - I want to emulate the behavior of double-clicking on Contents/MacOS/slingshotsms when double-clicking on the application icon. any tips?
If you're looking for 'easy', try just giving your python script a .command suffix, and make sure it's executable. For example:
#!/usr/bin/env python
# file: hello.command
print 'hello world'
If you're looking for 'polished', then you probably want to learn about Launch Services, PyObjC, Interface Builder, NIB files, app wrappers, and all sorts of Mac OS X-specific technology details. But, note that PyObjC is nearly impossible to use for anything non-trivial without already knowing, more or less, how to do the same task using the Objective-C Cocoa APIs. PyObjC is a fairly thin wrapper around those APIs, and you have to know the Cocoa idioms / design patterns to understand how the moving parts fit together.
If you don't actually need a terminal, but instead just want an app wrapper around a script, have a look at Platypus
Write an AppleScript application that launches Terminal and runs your Python script (which will be inside the application's bundle).

Categories

Resources