From System variables, select the Path variable and click Edit. In the Value text field, append a semicolon, type C:\MyPythonScripts, and then click OK. Now you can run any Python script in the C:\MyPythonScripts folder by simply pressing WIN-R and entering the script’s name. Running pythonScript
, for instance, will run pythonScript.bat, which in turn will save you from having to run the whole command py.exe C:\ MyPythonScripts\pythonScript.py
from the Run dialog.
On OS X, selecting Applications▸Utilities▸Terminal will bring up a Terminal window. A Terminal window is a way to enter commands on your computer using only text, rather than clicking through a graphic interface. To bring up the Terminal window on Ubuntu Linux, press the WIN (or SUPER) key to bring up Dash and type in Terminal.
The Terminal window will begin in the home folder of your user account. If my username is asweigart, the home folder will be /Users/asweigart on OS X and /home/asweigart on Linux. The tilde (~
) character is a shortcut for your home folder, so you can enter cd ~
to change to your home folder. You can also use the cd
command to change the current working directory to any other directory. On both OS X and Linux, the pwd
command will print the current working directory.
To run your Python programs, save your .py file to your home folder. Then, change the .py file’s permissions to make it executable by running chmod +x pythonScript.py
. File permissions are beyond the scope of this book, but you will need to run this command on your Python file if you want to run the program from the Terminal window. Once you do so, you will be able to run your script whenever you want by opening a Terminal window and entering ./pythonScript.py
. The shebang line at the top of the script will tell the operating system where to locate the Python interpreter.
You can disable the assert
statements in your Python programs for a slight performance improvement. When running Python from the terminal, include the -O
switch after python
or python3
and before the name of the .py file. This will run an optimized version of your program that skips the assertion checks.