Sub topic 8
Choosing a Code Editor
This chapter assumes you can open a terminal, that Python 3 is installed, and that python3 --version (or py --version on Windows) prints a version number. You have already run code in the REPL. Next you need a place to write scripts that you can save and run again.
What an editor needs to do
A code editor is a program for writing and saving plain text files. Python source files are plain text, so almost any editor can produce them. A word processor such as Microsoft Word is the wrong tool, because it saves formatting data that Python cannot read.
For Python work, look for these features:
- Syntax highlighting, which colors keywords, strings, and comments so mistakes stand out.
- Automatic indentation, because Python uses indentation to mark blocks of code.
- A built-in terminal or an easy way to open one in the project folder.
- Plain-text saving with UTF-8 encoding and a
.pyextension. - Optional extras: linting (flagging likely mistakes), autocomplete, and a debugger.
Common options
| Editor | Type | Cost | Best for |
|---|---|---|---|
| IDLE | Simple editor bundled with Python | Free | Starting with nothing else to install |
| Visual Studio Code | General editor with extensions | Free | Most learners, any operating system |
| PyCharm Community | Full Python IDE | Free | Larger projects and heavy refactoring |
| Vim or Nano | Terminal editor | Free | Editing over SSH or on a server |
An IDE (integrated development environment) bundles the editor, a debugger, and project tools into one program. It does more for you, but it takes longer to learn and uses more memory.
For this tutorial, Visual Studio Code with its Python extension is a sensible default. Whatever you pick, the steps in the tutorial work the same, because you will run scripts from the terminal and not from a button inside the editor.
Setting up Visual Studio Code
- Install Visual Studio Code from its official website.
- Open the Extensions panel and install the Python extension published by Microsoft.
- Use File > Open Folder to open a folder for your practice files.
- Create a new file and save it as
hello.py. The.pyextension tells the editor to treat it as Python. - Open the built-in terminal and check that it starts in your project folder.
VS Code may ask which interpreter to use. Choose the Python 3 version you installed. Later, when you create a virtual environment, you will point the editor at the interpreter inside .venv.
Check that the editor is set up correctly
Type this into hello.py and save it:
print("Editor check")Then run it from the editor's terminal:
python3 hello.py
# On Windows, use: py hello.pyIf you see Editor check, the editor, the folder, and the interpreter all work together.
Common mistakes
- Saving the file without the
.pyextension, or ashello.py.txt. Some systems hide extensions, so check the full file name. - Running the command from the wrong folder. If Python reports that it cannot open the file, the terminal's current folder does not contain it.
- Mixing tabs and spaces. Configure the editor to insert four spaces when you press Tab.
- Forgetting to save before running. The terminal runs the version on disk, not what is on screen.
- Using a word processor or a "smart quotes" feature that replaces straight quotes with curly ones, which causes a
SyntaxError.
Exercise
You saved a file in your editor as hello.py, but running python3 hello.py in the terminal prints "No such file or directory". Name two likely causes and how to check each.
Show answer
The terminal may be in a different folder from the file, so run pwd (or cd on Windows) and list the folder contents with ls (or dir) to see whether hello.py is there. The file name may also be wrong, for example hello.py.txt because the extension is hidden, so check the full name in the file listing and rename it if needed.