Sub topic 8
Choosing a code editor
This chapter assumes you can open a terminal, that python3 --version (or py --version on Windows) prints a Python 3 version, and that you have tried the REPL. You do not need to have written a script yet. Picking an editor comes right before that step, so the goal here is a setup that works, not the perfect tool.
What an editor has to do for Python
A Python script is a plain text file. Any program that saves plain text can produce a working script. The differences between editors are about how much help you get while writing and running code.
For this tutorial, an editor needs to do five things:
- Save files as plain text in UTF-8 encoding, with a
.pyextension. - Insert spaces when you press Tab, because Python uses indentation to mark blocks.
- Color the syntax, so a missing quote or bracket is easy to see.
- Open a folder, so you can keep a script and its
.venvfolder together. - Open an integrated terminal or sit next to your own terminal window.
Features that help later but are not required now include linting (automatic checks for likely mistakes), autocompletion, a debugger, and a formatter.
Three categories of tool
Tools for Python fall into three groups.
| Category | Examples | Strength | Cost |
|---|---|---|---|
| Basic text editor | Notepad, TextEdit, nano, gedit | Already installed, nothing to learn | Little or no Python awareness |
| Code editor | Visual Studio Code, Sublime Text, Vim, Neovim | Fast, extensible, works with many languages | You assemble the Python features yourself |
| Full IDE | PyCharm, Spyder | Debugger, refactoring, and environment handling built in | Heavier, more menus, more to learn |
An IDE (integrated development environment) bundles an editor, a runner, a debugger, and project tools into one application. A code editor is lighter and gains these features through extensions.
Choosing for this tutorial
Use one of these options, in this order of preference for most learners:
- Visual Studio Code with Microsoft's Python extension. It is free, runs on Windows, macOS, and Linux, and detects virtual environments.
- PyCharm Community Edition, if you prefer an all-in-one tool and do not mind a larger install.
- A terminal editor such as nano, if you work over SSH or want nothing to install.
Do not use a word processor such as Microsoft Word. It saves formatting data that Python cannot read. Avoid plain Notepad on older Windows versions for the same reason, since it may add characters that cause errors or save the file as hello.py.txt.
Setting up Visual Studio Code
These steps assume you chose VS Code.
- Install VS Code from the official site for your operating system.
- Open the Extensions view and install the extension named Python, published by Microsoft.
- Choose File, then Open Folder, and select a new empty folder such as
python-first. - Create a file in that folder and name it
hello.py. - Open the integrated terminal from the Terminal menu. It starts in the folder you opened, which is the directory you want when you run the script.
Once the extension is active, the status bar shows the selected interpreter. Click it to switch between the system Python and a virtual environment. This matters later, because the editor's selected interpreter controls which packages its Run button and its linting can see.
Settings worth checking
- Tab inserts four spaces. The Python extension sets this for
.pyfiles. Check the status bar if indentation looks wrong. - Auto-save, if you want files saved before each run. Forgetting to save is a very common reason a script appears to ignore your edits.
- Show whitespace, optionally, to see tabs and spaces. Mixed tabs and spaces cause an
IndentationError.
Indentation: the one setting that bites
Python reads indentation as structure. The convention is four spaces per level. An editor that inserts a real tab character in some lines and spaces in others produces files that look correct but fail.
This script is correct when every indented line uses spaces:
def greet(name):
message = f"Hello, {name}!"
return message
print(greet("Ada"))If one of the two lines inside greet used a tab while the other used four spaces, Python 3 stops with TabError: inconsistent use of tabs and spaces in indentation. The fix is to configure the editor to convert tabs to spaces, then re-indent the affected lines.
Running code from the editor
Editors offer a Run button or a keyboard shortcut. These are convenient, but they run your file with whichever interpreter the editor has selected. That interpreter may differ from the python3 your terminal uses.
For this tutorial, run scripts from a terminal as the next chapters show. This has three advantages:
- You see exactly which command ran, so the behavior is repeatable.
- You pass command-line arguments the same way a user of your script will.
- You learn the command that works outside the editor, such as on a server or in a scheduled job.
You can still use the editor's integrated terminal. It is an ordinary terminal inside the editor window.
Common errors with editors
These are the problems learners hit most often in this step.
| Symptom | Likely cause | Fix |
|---|---|---|
| `can't open file ... No such file or directory` | The terminal is in a different folder than the file | Run `pwd` (macOS, Linux) or `cd` (Windows) to see the folder, then move with `cd` |
| The file is named `hello.py.txt` | The editor or operating system added a `.txt` extension | Rename the file. On Windows, enable "File name extensions" in File Explorer |
| Edits have no effect | The file is unsaved | Save it, or turn on auto-save |
| `IndentationError` or `TabError` | Mixed tabs and spaces | Convert indentation to spaces |
| `SyntaxError` with odd quote characters | Curly quotes from a pasted source | Retype the quotes in the editor |
| Editor says a package is missing but the terminal runs fine | The editor uses a different interpreter | Select the right interpreter in the status bar |
| Run button prints a different Python version | Same cause: a different interpreter is selected | Select the interpreter that matches your terminal |
Other editors briefly
- PyCharm asks you to create a project and an interpreter on first launch. It can create a virtual environment for you. This is helpful, but it hides the commands, so repeat the
venvsteps by hand once in a later chapter. - Spyder targets data analysis and shows variables in a side panel. It is convenient for exploration and less common for command-line scripts.
- Sublime Text, Vim, Neovim, Emacs are fast and configurable. Each needs setup for Python indentation and linting. Choose one if you already know it.
- Jupyter notebooks mix code and text in cells. They are good for analysis, but they do not teach you how to run a
.pyfile, which is the goal of this tutorial. - Online editors run code in a browser. They avoid installation, so they cannot confirm that your own Python works.
Decision criteria
Choose by asking these questions:
- Do you already use an editor you like? Keep it and add Python support.
- Will you work on a machine you cannot install software on? Use nano or vim in the terminal.
- Do you want the least setup effort for general learning? Use VS Code with the Python extension.
- Do you want debugging and project tools without picking extensions? Use PyCharm Community Edition.
Switching later is cheap. Python files are plain text, so the same hello.py opens in every tool listed here.
Checklist before moving on
- Your editor opens a folder, not just a single file.
- Pressing Tab inserts spaces in a
.pyfile. - You can create and save
hello.pywith the exact name and extension. - You can open a terminal whose current folder is the folder with the file.
- You know which interpreter the editor has selected, and it matches the one from
python3 --versionorpy --version.
Exercise
You save a file as hello.py, run python3 hello.py in a terminal, and get can't open file 'hello.py': No such file or directory. The file is visible in your editor. Name two checks that find the cause.
Show answer
First, check the terminal's current folder (pwd on macOS and Linux, cd with no argument on Windows Command Prompt) and compare it with the folder that holds the file. Second, check the real file name and extension, because the file may be named hello.py.txt or saved in another folder. Run ls (or dir on Windows) to list the files in the current folder. The fix is to cd into the right folder or rename the file.
Exercise
Your editor shows no error for a script, but running it in the terminal reports TabError: inconsistent use of tabs and spaces in indentation. What happened, and how do you fix it?
Show answer
Some indented lines use a tab character and others use spaces, so the indentation looks aligned on screen but is inconsistent to Python. Turn on "show whitespace" to see the difference, set the editor to insert four spaces when you press Tab, and re-indent the affected lines with spaces only.
Exercise
The Run button in your editor fails with ModuleNotFoundError: No module named 'requests', but python3 -c "import requests" works in your terminal. What is the most likely cause?
Show answer
The editor has a different interpreter selected than the one your terminal uses. The package is installed for the terminal's Python or its virtual environment, not for the editor's interpreter. Select the matching interpreter in the editor's status bar, or run the script from the terminal.