Life is really simple, but we insist on making it complicated.  -Confucius




Integrating Python and the Windows CLI
Create command-line scripts in Python and run them from anywhere via 'cmd'.

If you are like me, you use Python to create utility scripts to help you do whatever it is you need to do on your computer. Here's an easy way to allow access to Python scripts from the Windows command-line via environment variables.

Environment variables are pieces of data that the command-line and many system-specific programs use. They may contain data ranging from the number of processors to the location of the Temp directory. There are two variables that we will edit one that controls which filetypes are executable, the other contains the paths that are searched for executables.

Below is a diagram and list of the steps you need to take when you want to edit your environment variables in Windows XP. The process is fairly similar on Windows Vista/7, in the 'System' section of the Control Panel, select 'Advanced system settings' from the list on the left.

  1. First, we need to access the System Properties window via the right-click menu from 'My Computer'. Select the menu item 'Properties'. You can also access this window by pressing the Windows Key + Pause/Break.
  2. Select the 'Advanced' tab.
  3. Click the 'Environment Variables' button. In the 'System variables' frame, you should see two variables, one with the name 'Path', one with the name PATHEXT. These are the two we will edit.
  4. Select the 'Path' variable, and click the 'Edit' button. You will see that the value for 'Path' is a list of different paths. This is the list of paths that is searched for executables. So, what I did was setup a folder for scripts in my Home folder, and I added the path to this on the end of the list. The values are separated by semicolons.

    Example, if the last two paths in the list were:

    C:\Program Files;%HOMEPATH%

    The variable would look like this after you add your path:

    C:\Program Files;%HOMEPATH%;C:\My\Path\ToThe\Scripts

  5. Select the 'PATHENV' variable and click 'Edit'. We are going to add an extension to list of extensions that are executable. The addition will look like:
    ;*.PY

Okay that does it for setup, now let's test our results. I have a script 'ds' that is a utility that displays the size and count of the files in the current directory.

First, I have put my script file (ds.py) into my scripts folder that I added to the Path variable. Now, I want to go to the Start menu -> Run -> 'cmd'. This should bring up the command-line, and from any directory, you should be able to type the name of your script, in my case 'ds' and it should execute.

This technique has greatly improved my work efficiency with Python on Windows, especially in the area of testing/debugging. For some reason or another, I have always found IDLE buggy when dealing with libraries such as wxPython or pygame. Usually after I run an app written in one of these libraries, IDLE or Python (or both?) lock-up and I have to kill IDLE, which kills all the windows I have open. My fix for this is to have a command-line window open to my working directory, and I can simply run my program from there. This allows me to test programs without fear of losing my workspace, rapidly access my Python files, and view any error messages.

I hope this technique helps you!

all original content copyright © colin barnette 2008-10. all rights reserved. xhtml. css.