How to Make a Python Script Shortcut with Arguments

How to Make Python Script Shortcut with Arguments

Once again, thanks for stopping by for another edition of the Python Bytes series. 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:

  1. Right-click the script and select create a shortcut
  2. Right-click the new shortcut and select properties
  3. Under the shortcut tab, note the target option
  4. 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.

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.

Series Navigation← How to Get the Last Item of a List in PythonHow to Sort a List of Strings in Python →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.