Running Python Programs from the Command Line
The essential path is: save source code in a .py file, keep that file in a known folder, and use the command line to have Python execute the saved file.
From Saved File to Running Program
A Python program becomes reusable when you save its source code as a file on your computer. The saved file can later be opened, edited, and executed by the Python interpreter. The command line provides a way to ask the computer to run that saved program rather than merely typing code temporarily in an interactive shell or notebook.
The essential path is: save source code in a .py file, keep that file in a known folder, and use the command line to have Python execute the saved file.
Naming the Python Source File
A Python source filename has two essential parts: a name that you choose and the .py extension that you must include. In hello.py, hello is the chosen name and .py is the extension. The extension identifies the file as containing Python code so that the operating system and Python interpreter recognize it as a Python file.
| Filename | Chosen name | Required extension |
|---|---|---|
| hello.py | hello | .py |
| my_program.py | my_program | .py |
| calculate_tax.py | calculate_tax | .py |
| greet_user.py | greet_user | .py |
Examples of meaningful Python filenames from the source material
Choosing a Program Folder
You can save a Python file in any folder as long as you know where that folder is located. For organized work, create one dedicated folder for your Python programs. A folder named Python_Programs or My_Python_Code on the Desktop or in Documents is one practical arrangement described in the source material.
- 1Create one folder dedicated to your Python programs.
- 2Choose a location you can identify, such as the Desktop or Documents folder.
- 3Save each Python source file in that dedicated folder.
- 4Verify that the file was created and is in the intended location.
- 5Use the known folder and filename when requesting execution from the command line.
Saving Code Permanently
Saving a First Program
You have written Python code and want to keep it so that you can open, edit, reuse, and run it later.
- 1Write
Use a plain-text editor to type the Python code. A plain-text editor is appropriate because word processors add formatting that Python cannot interpret.
- 2Name
Choose a meaningful filename, such as
hello.py. The name is your choice, but the .py extension is required. - 3Choose a location
Save the file in a known folder, preferably a dedicated folder for your Python programs.
- 4Save
When you save, the code is written to computer storage as a file with the filename and location you selected.
- 5Verify
Check that the file exists in the intended folder and has the .py extension.
- 6Run later
After saving, you can close the editor. The file remains on the computer and can later be opened, edited, or executed by the Python interpreter.
The source code is now a permanent saved record in a Python file that can be reused and run later.
The editor is only the tool used to write and save the code. The saved file is independent of the editor. After the file is stored, you can close the editor or return later; the file remains available on the computer as the saved record of the program.
| Where code is written | What happens to the code |
|---|---|
| Interactive shell or notebook | It runs immediately but disappears when the program is closed. |
| Saved .py file | It remains on the computer and can be opened, edited, reused, and run later. |
Temporary interactive code compared with a saved Python source file
Python Execution in Context
The command-line workflow begins with a source file stored on the computer. The Python interpreter executes that saved Python file when you request it from the command line. The important beginner-level distinction is that the file is the stored source code, while the interpreter is the software that runs that source code.
| Saved Python workflow | Compiled-language workflow described in the source |
|---|---|
| Python source is saved in a .py file and executed by the Python interpreter. | C or C++ source is converted into binary code by a compiler using flags and options. |
| The saved source file remains the reusable record of the program. | When the compiled program is run, linker or loader software copies the program from hard disk to memory and starts it. |
A high-level comparison of the execution models described in the source material
Mistakes Beginners Make
Common mistake Saving the file without the .py extension
What it looks likeUsing a filename that does not end in .py
Why it failsThe .py extension is required so the operating system and Python interpreter recognize the file as a Python program.
Do this insteadInclude .py as part of the filename when saving.
Common mistake Writing Python code in a word processor
What it looks likeUsing software that adds document formatting
Why it failsWord processors add formatting that Python cannot interpret.
Do this insteadUse a plain-text editor.
Common mistake Using spaces or special characters in the filename
What it looks likeChoosing a filename with spaces instead of a simple name with underscores
Why it failsThe source recommends underscores instead of spaces and avoiding special characters.
Do this insteadChoose a meaningful name using underscores where needed.
Common mistake Losing track of the storage folder
What it looks likeSaving programs in unrelated locations
Why it failsThe command line needs a known file location when you want to run the program.
Do this insteadStore programs in one dedicated folder and verify the location after saving.
Common mistake Assuming code typed in an interactive shell is automatically saved
What it looks likeClosing the shell and expecting the code to remain available
Why it failsCode written in an interactive shell or notebook runs immediately but disappears when the program is closed.
Do this insteadSave code you want to keep as a .py file.
Practice the File Workflow
Plan how you would save a Python program that you want to run again later. State the filename, the folder where you would store it, the editor you would use, and the checks you would perform after saving.
Hints
- Make sure the filename includes the .py extension.
- Choose a dedicated folder whose location you can identify.
- Use a plain-text editor rather than a word processor.
- Verify that the file exists in the intended folder.
What do you think happens?
You close the editor after saving hello.py. What should happen to the saved program?
Reveal answer
The file remains on the computer and can be opened or run later.
The saved file is independent of the text editor. Saving writes the code to computer storage, so the file remains available after the editor is closed.
Key Takeaways
- 1Save Python source code in a file whose name ends with .py.
- 2The filename combines a chosen program name with the required .py extension.
- 3Use a plain-text editor, not a word processor, to write and save Python code.
- 4Keep programs in a dedicated, known folder so they are easier to find and run from the command line.
- 5A saved file is a permanent record of the code and can later be opened, edited, reused, and executed by the Python interpreter.
At a glance
- A Python program you want to keep should be saved as a .py source file.
- The .py extension identifies the file as containing Python code.
- A dedicated folder keeps Python programs organized and gives the command line a known location to work from.
- Saving separates the program from the editor: the file remains on the computer after the editor closes.
- The Python interpreter executes the saved source file when you request it from the command line.