1.6. Running Python Programs Without the Command Line

You probably already know how to run programs from whatever launcher your operating system provides. Windows has the Start menu, macOS has the Finder and Dock, and Ubuntu Linux has Dash. Programs will add themselves to these launchers when you install them. You can also double- click a program’s icon in a file explorer app (such as File Explorer on Windows, Finder on macOS, and Files on Ubuntu Linux) to run them.

But these methods don’t apply to your Python programs. Often, double-clicking a .py file will open the Python program in an editor or IDE instead of running it. And if you try running Python directly, you’ll just open the Python interactive shell. The most common way of running a Python program is opening it in an IDE and clicking the Run menu option or executing it in the command line. Both methods are tedious if you sim- ply want to launch a Python program.

Instead, you can set up your Python programs to easily run them from your operating system’s launcher, just like other applications you’ve installed. The following sections detail how to do this for your particular operating system. ## Running Python Programs on Windows On Windows, you can run Python programs in a few other ways. Instead of opening a terminal window, you can press WIN-R to open the Run dialog and enter py C::raw-latex:`\path`:raw-latex:`\to`:raw-latex:`\yourScript`.py , as shown in Figure 2-7. The py.exe program is installed at C::raw-latex:`\Windows`:raw-latex:`\py`.exe, which is already in the PATH environ- ment variable, and the .exe file extension is optional when you are running programs.

_images/Figure2_7.png

Figure 2-7: The Run dialog on Windows

Still, this method requires you to enter your script’s full path. Also, the terminal window that displays the program’s output will automatically close when the program ends, and you might miss some output.

You can solve these problems by creating a batch script, which is a small text file with the .bat file extension that can run multiple terminal com- mands at once, much like a shell script in macOS and Linux. You can use a text editor, such as Notepad, to create these files. Make a new text file con- taining the following two lines:

@py.exe C::raw-latex:`\path`:raw-latex:`\to`:raw-latex:`\yourScript`.py %* @pause

Replace this path with the absolute path to your program, and save this file with a .bat file extension (for example, yourScript.bat). The @ sign at the start of each command prevents it from being displayed in the ter- minal window, and the %* forwards any command line arguments entered after the batch filename to the Python script. The Python script, in turn, reads the command line arguments in the sys.argv list. This batch file will spare you from having to type the Python program’s full absolute path every time you want to run it. The @pause command adds Press any key to continue… to the end of the Python script to prevent the program’s window from disappearing too quickly.

I recommend you place all of your batch and .py files in a single folder that already exists in the PATH environment variable, such as your home folder at C::raw-latex:`Users`<USERNAME>. With a batch file set up, you can run your Python script by simply pressing WIN-R, entering the name of your batch file (entering the .bat file extension is optional), and pressing ENTER. ## Running Python Programs on macOS On macOS, you can create a shell script to run your Python scripts by creat- ing a text file with the .command file extension. Make one in a text editor, such as TextEdit, and add the following content:

#!/usr/bin/env bash python3 /path/to/yourScript.py

Save this file in your home folder. In a terminal window, make this shell script executable by running chmod u+x yourScript.command . Now you should be able to click the Spotlight icon (or press COMMAND-SPACE) and enter the name of your shell script to run it. The shell script, in turn, will run your Python script.

1.6.1. Running Python Programs on Ubuntu Linux

There isn’t a quick way to run your Python scripts on Ubuntu Linux like there is in Windows and macOS, although you can shorten some of the steps involved. First, make sure your .py file is in your home folder. Second, add this line as the first line of your .py file:

#!/usr/bin/env python3

This is called a shebang line, and it tells Ubuntu that when you run this file, you want to use python3 to run it. Third, add the execute permission to this file by running the chmod command from the terminal:

al@al-VirtualBox:~$ chmod u+x yourScript.py

Now whenever you want to quickly run your Python script, you can press CTRL-ALT-T to open a new terminal window. This terminal will be set to the home folder, so you can simply enter ./yourScript.py to run this script. The ./ is required because it tells Ubuntu that yourScript.py exists in the cwd (the home folder, in this case). # Summary Environment setup involves all the steps necessary to get your computer into a state where you can easily run your programs. It requires you to know several low-level concepts about how your computer works, such as the file- system, file paths, processes, the command line, and environment variables.

The filesystem is how your computer organizes all the files on your computer. A file is a complete, absolute file path or a file path relative to the cwd. You’ll navigate the filesystem through the command line. The com- mand line has several other names, such as terminal, shell, and console, but they all refer to the same thing: the text-based program that lets you enter commands. Although the command line and the names of common commands are slightly different between Windows and macOS/Linux, they effectively perform the same tasks.

When you enter a command or program name, the command line checks the folders listed in the PATH environment variable for the name. This is important to understand to figure out any command not found errors you might encounter. The steps for adding new folders to the PATH environ- ment variable are also slightly different between Windows and macOS/ Linux.

Becoming comfortable with the command line takes time because there are so many commands and command line arguments to learn. Don’t worry if you spend a lot of time searching for help online; this is what expe- rienced software developers do every day.