Run your (python) scripts Globally from anywhere, without the full path

Kevin Gleijm
4 min readMar 31, 2021

--

While experimenting with python in my daily life i’ve found it to be a great tool for automating a lot of tedious tasks. Although i’m an IDE kind of guy i catch myself drifting more and more to the terminal for running my already finished scripts and tools. This is great because i don’t need to fire up the relatively slow-to-start pycharm editor evertytime i want to run a small task. The downside however is that i keep forgetting the full path to my scripts and to run it with the python prefix and the .py file extension. I want it to be simple.
So in this post i’d like to explain my solution to turn python scripts(or any script for that matter) into home-made globally accessible commands.

about 36 characters too many

At first glance it doesn’t look that bad but when your script is in a path that’s twice as long and 3 months old you’ll find youself searching and typing way more than you ever should. We’re programmers after all, we make computers search for us. What we want to see is:

how we like to see it

To make this work you’ll need to have python installed first, have a look here if you don’t already.

We’ll start with the test script we want to run, I modified my simple hello world script to print the first argument passed to the script 10 times. Take a look at the code:

at least it’s more original than hello world

There are two things we need to do to make it work:
1. Make a folder and add it to our PATH
2. Add a filetype association for .py

First we need to make a folter and drop our tool in there(and give it a more descriptive name than test.py). I made my tool folder right in C:// but you can put it anywhere you like, just remember the path for later.

Go to settings and follow the steps:

If you can find it search for settings in the lower left corner of the screen
scroll down on both the left and the right-hand side to find the options
click on Advanced system settings
go to the environment Variables
First click on Path, then on edit. Remember the PATHEXT for later
click new
add the full path to the folder that’s going to contain your tools.

While your here, edit your PATHEXT as well, add ;.PY;.PYW to there if it’s not there already. The next steps should do that automatically but better safe than sorry as i tested this trick on another PC that didn’t do this automatically.

Alright, with the hardest part done we’re going to take care of the .py extension by making a filetype association.
Navigate to Default apps via system or the searchbar in the lower left corner and choose default apps by file type. It make take a while for the list to load but when it does scroll down to .py

when you see .py it might be empty or already be associated with python

If it doesn’t say python already just click it and select python from the list. If it does switch it to notepad and back to let windows take care of making the necessary associations.

Should be easy to find

Now restart your computer once or twice and we should be ready to go. Fire up your CMD and try it out yourself, just type the name of your tool and the parameters if it takes those.

Much better!
Works like a charm

--

--

Kevin Gleijm

Computer Science student by day and avid programmer by night.