How to Make a Python Script Shortcut with Arguments: Batch, Bash, and More

How to make a Python Script Shortcut With Arguments Featured Image

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 scriptOpens in a new tab.. 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 PyCharmOpens in a new tab. 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.

Note: the target dialogue is limited to 260 characters. Unfortunately, I learned this lesson the hard way. Check out this threadOpens in a new tab. and this threadOpens in a new tab. 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 tutorialOpens in a new tab..

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 tutorialOpens in a new tab..

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 environmentsOpens in a new tab..

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.

How to Python (42 Articles)—Series Navigation

The How to Python tutorial series strays from the usual in-depth coding articles by exploring byte-sized problems in Python. In this series, students will dive into unique topics such as How to Invert a Dictionary, How to Sum Elements of Two Lists, and How to Check if a File Exists.

Each problem is explored from the naive approach to the ideal solution. Occasionally, there’ll be some just-for-fun solutions too. At the end of every article, you’ll find a recap full of code snippets for your own use. Don’t be afraid to take what you need!

If you’re not sure where to start, I recommend checking out our list of Python Code Snippets for Everyday Problems. In addition, you can find some of the snippets in a Jupyter notebook format on GitHubOpens in a new tab.,

If you have a problem of your own, feel free to ask. Someone else probably has the same problem. Enjoy How to Python!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts