Choosing and Setting Up a Python Editor
A Python source file is a permanent record of a program. The editor displays and lets you change the code, while the saved file stores that code on your computer.
From Editor to Saved Program
Writing code in an editor is only the beginning. If you want to keep the program, reopen it, edit it, run it later, or share it with someone else, you must save it as a file on your computer. The essential choices are the editor, the filename, and the folder where the file will live.
A Python source file is a permanent record of a program. The editor displays and lets you change the code, while the saved file stores that code on your computer.
The .py Filename Extension
A Python source filename has two essential parts: a name you choose and the .py extension that you must include. In hello.py, hello is the chosen name and .py is the required extension. The extension tells the operating system and text editors that the file contains Python code, and it allows the Python interpreter to recognize the file as a Python program.
| Filename part | Purpose |
|---|---|
| hello | A name chosen by the programmer |
| . | Separates the name from the extension |
| py | Identifies the file as Python source code |
The three visible parts of the filename hello.py
Saving Code into a File
The saving workflow is straightforward. Open a plain-text editor, write your Python code, choose a meaningful filename, select a location, and save the file with the .py extension. When you click Save, the code in the editor is written to computer storage as the file you specified.
Saving a Greeting Program
You have written a small greeting program in a plain-text editor and want to keep it for later use.
- 1Choose a name
Select a meaningful name such as
greet_user.py. The name describes the program, and the .py extension identifies it as Python source code. - 2Choose a folder
Select the dedicated folder where you keep your Python programs.
- 3Save the file
Use the editor's Save action. The code displayed in the editor is written to storage as the file named
greet_user.py. - 4Verify the result
Check that the file exists in the intended folder and that its filename ends in .py.
The program is now stored as a Python source file that can be opened, edited, and run later.
File Contents and Editor Display
Before saving, the code exists in the editor while you are working on it. Saving writes that code to computer storage as a file. The file is independent of the editor: after saving, you can close the editor or turn off the computer, and the file remains in the selected folder exactly as it was saved.
The editor is a tool for writing and viewing code. The .py file is the stored program. Saving creates the lasting connection between the code you wrote and the file you can reopen later.
Organizing Python Programs
A Python file can be saved in any folder as long as you know where that folder is. However, a dedicated folder makes your work easier to organize as you create more programs. Create one folder specifically for Python programs, such as Python_Programs or My_Python_Code, on your Desktop or in your Documents folder. Save your Python files there so they remain together and are easier to find and run later.
- Create one folder dedicated to your Python programs.
- Choose a location you can find again, such as the Desktop or Documents folder.
- Use meaningful filenames for individual programs.
- Use underscores instead of spaces in filenames.
- Avoid special characters in filenames.
- After saving, verify that the file is in the intended folder and that the name ends in .py.
Filename and Saving Mistakes
Common mistake Leaving out the .py extension
What it looks likeSaving a program with a name that does not end in .py
Why it failsThe operating system and Python interpreter rely on the extension to recognize the file as a Python source file.
Do this insteadInclude .py at the end of the filename.
Common mistake Using a word processor instead of a plain-text editor
What it looks likeWriting Python code in an application that adds document formatting
Why it failsThe added formatting cannot be interpreted as Python code.
Do this insteadUse a plain-text editor for writing and saving Python programs.
Common mistake Using spaces or special characters in the filename
What it looks likeChoosing a filename with spaces or special characters
Why it failsThe recommended filename format uses underscores instead of spaces and avoids special characters.
Do this insteadChoose a meaningful name that uses underscores where needed and ends in .py.
Common mistake Saving programs in unrelated locations
What it looks likePlacing each Python file in a different folder without a consistent plan
Why it failsThe files become harder to find and manage as the number of programs grows.
Do this insteadCreate one dedicated folder and save your Python programs there.
Common mistake Assuming code remains after closing an interactive shell or notebook
What it looks likeClosing the program without saving the code as a file
Why it failsCode entered interactively runs immediately but disappears when the program is closed.
Do this insteadSave code as a .py file when you need to keep, reuse, or share it.
File Setup Practice
Plan how you would save a Python program that calculates a total. State the folder you would create or use, choose a meaningful filename, and explain why the filename must end in .py.
Hints
- Use a dedicated folder for Python programs.
- Use underscores instead of spaces in the filename.
- The extension is .py because it identifies the file as Python source code.
What do you think happens?
You write code in an interactive shell and close the program without saving it. What happens to that code?
Reveal answer
It disappears when the program closes.
Code entered in an interactive shell or notebook runs immediately, but saving it as a .py file is necessary when you want to keep, reuse, or share it.
Essential Saving Rules
- 1Use a plain-text editor rather than a word processor.
- 2Save every Python program with the .py extension.
- 3Choose a meaningful filename, use underscores instead of spaces, and avoid special characters.
- 4Keep your programs in one dedicated folder so they are easy to find.
- 5Remember that saving writes the code from the editor to a lasting file on computer storage.
- 6Verify both the filename and the folder after saving.
Key Takeaways
- A Python source file needs a name and the .py extension.
- The .py extension helps the operating system and Python interpreter recognize the file as Python code.
- A plain-text editor is appropriate for Python; word processors add formatting that Python cannot interpret.
- A dedicated folder keeps Python programs organized and easy to find.
- Saving writes the code displayed in the editor to a persistent file that can be reopened, edited, and run later.