Once again, thanks for stopping by for another edition of the How to Python. In this tutorial, we’ll be taking a step away from Python coding and looking at Python execution. Specifically, we’ll be looking at different ways to make a Python script shortcut with arguments. Let’s dive in!
Table of Contents
Problem Introduction
Recently, in an effort to simplify my image editing process, I wrote a nice image titling script. Basically, this script generates the titles on my featured images.
At first, I developed this script to save me time on image editing. While the script was much faster and more convenient, it quickly became frustrating to find image paths, so I added a feature to allow me to browse the explorer. After that, I added some features to shortcut image titling.
Eventually, I added an output path feature as an argument, so I didn’t have to move images myself anymore:
trc-image-titler.py -o /path/to/output
Despite all these features, I still wanted to improve my own user experience, so I decided to make the script a shortcut with arguments. That way, I wouldn’t have to open PyCharm every time I wanted to title some image.
Solutions
As with many problems in this series, there are always a handful of options. Making a Python script shortcut with arguments is no exception. Let’s take a look.
Create a Windows Shortcut
As a fair warning, I own a Windows 10 PC that I tend to use for development. As a result, my primary solution will only be applicable to Windows users:
- Right-click the script and select create a shortcut
- Right-click the new shortcut and select properties
- Under the shortcut tab, note the target option
- Add your options to the target as follows:
\path\to\trc-image-titler.py -o \path\to\output
Assuming all of your paths are setup correctly, this shortcut with arguments should run your script as expected.
Personally, I ran into some issues with site-packages differing from PyCharm, so I had to install them by hand.
Note: the target dialogue is limited to 260 characters. Unfortunately, I learned this lesson the hard way. Check out this thread and this thread for more details. That said, there’s a nice workaround that involves creating a system variable for all your arguments.
Create a Batch File
As an alternative, you can always create your own batch script. Yeah, I know this is supposed to be a Python tutorial, but this is helpful information regardless.
At any rate, here’s a sample batch script that would run a hypothetical Python file:
@echo off \path\to\trc-image-titler.py -o \path\to\output
Notice that this is effectively the same idea as the shortcut with arguments except we now have a file with a .bat extension. The advantage here is we have more options. For instance, we could spawn up a virtual environment which installs the libraries we need.
If you need help writing a Batch file, I found a great little tutorial.
Create a Bash Script
For everyone else, there’s shell scripting:
#!/bin/sh python /path/to/trc-image-titler.py -o /path/to/output
Again, the solution is basically the same as the batch file. As a result, we can customize the script to our needs. Personally, I didn’t need scripting at all, but maybe you do.
If you need help writing a shell script, I found another great tutorial.
Troubleshooting
Typically, we don’t need a troubleshooting section for articles in this series, but running Python scripts isn’t always easy. In fact, that trickiness is what drove me to share much of the same disdain for Python’s package management as this author despite really loving the language.
So to help you out, I wanted to share a bit of a caveat for this tutorial. Be aware of your system path.
Personally, I have both Python 2 and 3 installed on my PC. This creates an odd situation where I have the py keyword in my system path rather than the python keyword. In addition, I am able to specify the Python version with this py keyword, but Python 3 is the default.
If you’re in a similar situation, it may be easier to specify the Python executable in your shortcut or script:
\path\to\python.exe \path\to\trc-image-titler.py -o \path\to\output
Of course, this doesn’t solve dependency issues. In that case, you might need to play around with virtual environments.
If you’ve found a better way to run your scripts with arguments, let me know in the comments.
A Little Recap
Usually, we’d be working with Python, but I figured we could cover a little bit of Python execution today. At any rate, let’s get to the recap.
Windows Shortcut with Arguments
\path\to\trc-image-titler.py -o \path\to\output
Batch File with Arguments
@echo off \path\to\trc-image-titler.py -o \path\to\output
Bash Script with Arguments
#!/bin/sh python /path/to/trc-image-titler.py -o /path/to/output
And, there we have it! If you enjoyed this article, give it a share. As always, comments and suggestions are welcome.
Recent Posts
Recently, I was thinking about the old Pavlov's dog story and how we hardly treat our students any different. While reflecting on this idea, I decided to write the whole thing up for others to read....
In the world of programming languages, expressions are an interesting concept that folks tend to implicitly understand but might not be able to define. As a result, I figured I'd take a crack at...