Back to writing

[python] [tutorial]

Running Python scripts

    Table of contents
  1. Level 0: python.exe
  2. Level 1: python
  3. Level 2:
  4. Level 3: on the PATH
  5. Level 4: myfile

In this document, C:\python is the folder where you installed Python to. That's where python.exe lives. C:\code is a folder you created where you put all the .py files that you make. Lastly, C:\anywhere is anywhere else on your computer that is not one of these two locations.

I am using Windows style prompts with cwd>. For Linux users, this is the same as your user:cwd$.

Level 0: python.exe (§)

In this level, you are specifying the exact location of the Python executable and the exact location of the script.

C:\python> python.exe C:\code\

C:\code> C:\python\python.exe

C:\anywhere> C:\python\python.exe C:\code\

Level 1: python (§)

In this level, you are letting the system find Python, but specifying the exact location of the script.

When you install Python, there is an option to add Python to your PATH. The PATH is a list of directories that your computer will search every time you type a command on the command prompt. This means that when you type python, it will automatically find the python.exe in your installation folder. Windows users, type "environment variable" into your Windows search bar to open the settings menu and find the variable called PATH, or echo %PATH% on the cmd to see what it looks like. Linux users, echo $PATH.

C:\code> python

C:\anywhere> python C:\code\

Level 2: (§)

In this level, you are specifying the exact location of the script, but do not mention Python at all, because the system knows it is a Python file.

Windows: Ensure that .py file extensions are associated with the python.exe or py.exe that you installed. This should happen automatically when you install Python, and you can always re-run the Python installer to reset these associations if they become broken.


C:\anywhere> C:\code\

Linux: Add a shebang for your Python executable to the top of the script file, and mark the file as executable with chmod +x

/code$ ./

/anywhere$ /code/

Level 3: on the PATH (§)

In this level, you do not need to specify the exact path of the script ever.

Add the directory that contains the script, C:\code, to your PATH. Remember that PATH is a list of directories, not files. If you add C:\code\, it won't work. You have to add the directory.


Level 4: myfile (§)

The final level is to run Python scripts without specifying Python, or the extension, or the file's location. At this level, Python scripts feel like native commands such as dir, ls, and cd.

Both: Add the directory that contains the script to your PATH.

Windows: Add .py to your PATHEXT variable, so Windows will understand that when you type myfile, it should also search the PATH for Use where myfile to double check that your file appears, and there is not another program with the same name taking priority.

Linux: Keep the shebang at the top of the file, and rename the file to remove the .py extension. Use which myfile to double check that it is the top result.

C:\anywhere> myfile

The only way to get better than this is to have a mind-controlled computer where you don't have to type anything.

View this document's history

Contact me:

If you would like to subscribe for more, add this to your RSS reader: