Concepts / Running Python Programs from the Command Line

Running Python Programs from the Command Line

A program written in a compiled language like C or C++ is converted from the source language i.e. C or C++ into a language that is spoken by your computer (binary code i.e. 0s and 1s) using a compiler with various flags and options. When you run the program, the linker/loader software copies the program from hard disk to memory and starts running it.

  • Programming

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.

storesis identified by namerequestsPython programfoldercontains hello.pyhello.pysaved Python source codeCommand linerequests executionPython interpreterexecutes the source file
How does a saved Python file move from storage to execution when you use 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.

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.

forms part ofidentifies typecontainshello.pyname plus .py extensionhellochosen program name.pyPython file extensionPython source codecode saved in the file
What is the relationship between the filename hello.py and the Python code stored inside it?
FilenameChosen nameRequired extension
hello.pyhello.py
my_program.pymy_program.py
calculate_tax.pycalculate_tax.py
greet_user.pygreet_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.

containscontainsstoresstoresComputerDocumentsPython_Programsdedicated folderhello.pysaved source filecalculate_tax.pysaved source file
Where can Python programs be stored so that the command line can locate them more easily?
  1. Create one folder dedicated to your Python programs.
  2. Choose a location you can identify, such as the Desktop or Documents folder.
  3. Save each Python source file in that dedicated folder.
  4. Verify that the file was created and is in the intended location.
  5. Use 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.

Write: 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.

Name: Choose a meaningful filename, such as hello.py. The name is your choice, but the .py extension is required.

Choose a location: Save the file in a known folder, preferably a dedicated folder for your Python programs.

Save: When you save, the code is written to computer storage as a file with the filename and location you selected.

Verify: Check that the file exists in the intended folder and has the .py extension.

Run 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 writtenWhat happens to the code
Interactive shell or notebookIt runs immediately but disappears when the program is closed.
Saved .py fileIt remains on the computer and can be opened, edited, reused, and run later.

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 workflowCompiled-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.

Mistakes Beginners Make

  • Saving the file without the .py extension

    The .py extension is required so the operating system and Python interpreter recognize the file as a Python program.

    Fix: Include .py as part of the filename when saving.

  • Writing Python code in a word processor

    Word processors add formatting that Python cannot interpret.

    Fix: Use a plain-text editor.

  • Using spaces or special characters in the filename

    The source recommends underscores instead of spaces and avoiding special characters.

    Fix: Choose a meaningful name using underscores where needed.

  • Losing track of the storage folder

    The command line needs a known file location when you want to run the program.

    Fix: Store programs in one dedicated folder and verify the location after saving.

  • Assuming code typed in an interactive shell is automatically saved

    Code written in an interactive shell or notebook runs immediately but disappears when the program is closed.

    Fix: Save code you want to keep as a .py file.

Practice the File Workflow

EASY

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?

  • The file remains on the computer and can be opened or run later.
  • The code disappears because the editor was closed.
  • The filename changes automatically because the editor is no longer open.
Reveal answer

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

  1. Save Python source code in a file whose name ends with .py.
  2. The filename combines a chosen program name with the required .py extension.
  3. Use a plain-text editor, not a word processor, to write and save Python code.
  4. Keep programs in a dedicated, known folder so they are easier to find and run from the command line.
  5. A saved file is a permanent record of the code and can later be opened, edited, reused, and executed by the Python interpreter.

Key Takeaways

  • 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.