DEV Community

Fredrik Sjöstrand
Fredrik Sjöstrand

Posted on • Updated on • Originally published at fronkan.hashnode.dev

Settings for Making Python CLI-tools on Windows

In the previous post of this series, I showed how to set up a folder for our own CLI-tools in Windows. Now I will cover some settings which make CLI-tools written in Python work with that setup.

Make Python the Default Program for .py-files

First, make sure you set the default program for opening .py-files to Python. To change this you right-click a .py file, go into properties and change opens with from what you currently have to Python. This will allow you to just write the name of the file to run it from the terminal.
So you can write this:

PS> my_tool.py
Enter fullscreen mode Exit fullscreen mode

instead of this:

PS> python my_tool.py
Enter fullscreen mode Exit fullscreen mode

This might seem like a rather small improvement in the amount of typing. However, it has a much more important effect which relates to the last post in this series. If you followed along with the last post and have the bin-directory setup, this part is important. As if you added a python file to that folder and then tried calling it using the python command you will have gotten an error. This is caused by the python command only looking in the current directory and not in your path. What is so nice about being able to run the script by just writing the file-name is that it solves this problem. Now you can just create a python file, drop it in the bin-directory and call it from anywhere. Success!

Adding .py-files to PATHEXT

As you might have noticed in the example above I used the PowerShell (PS) prompt. At home I am currently running cmder, however, at work I am running PS. This made me notice an annoying problem, where when I started a python script from PS it spawned and executed the script in a new window. It wrote nothing to the PS terminal and the window closed as soon as the script finished. I found the solution to this problem was to add .PY to the semi-colon separated list of file-endings in the system environment variable PATHEXT. If you don't know where you can change the environment variables, check out the first post in the series. When you are finished your PATHEXT probably looks something like this:

Screenshot with the text PATHEXT a large space before the text .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY

Adding the extension to this list marks it as an executable file-type. A side-effect is that this allows you to run the python files without writing the .py extension. Continuing with the example from earlier:

PS> my_tool
Enter fullscreen mode Exit fullscreen mode

However, because of autocompleting in Windows really liking file-extensions you will probably not notice much of this side-effect.

Final Notes

These are the main struggles I went through when setting up my Windows environment for using Python scripts as CLI-tools. It actually took multiple attempts to find these solutions and I hope this might help anyone struggling with the same issues. If you have encountered any other issues when running python on Windows, please let me know in the comments and we can attempt to solve it together.

Top comments (0)