PYTHON / GETTING STARTED
Install Python and choose an editor
Install a modern Python interpreter, verify which one is running, and point your editor at that exact interpreter.
What you will learn
- Check the installed version with python3 --version or python --version
- Read sys.executable to see which interpreter file is actually running your code
- Compare sys.version_info tuples, never version strings, in version checks
- Select the interpreter your editor uses so it matches your terminal
Understanding Install Python and choose an editor
A Python installation is nothing more than an executable file on disk plus a library folder next to it. When you type python3 in a terminal, the shell searches the directories listed in your PATH environment variable, in order, and runs the first match it finds. That is why one machine can hold three or four Pythons at once without conflict, and why the version you get depends entirely on which directory wins the PATH search.
On Windows the official installer from python.org offers a checkbox that adds the interpreter folder to PATH; without it the command exists on disk but the shell cannot find it. On macOS and most Linux distributions a system Python already exists and other software depends on it, so you install your own alongside it rather than replacing it, and you call yours with python3. Because both may be present, the single most useful diagnostic is sys.executable, which reports the absolute path of the interpreter currently running your code.
An editor is a separate concern. VS Code, PyCharm, and Sublime Text do not contain a Python; they store a setting that points at one interpreter path and hand your file to it when you press Run. That indirection is the source of most beginner confusion: the terminal and the editor can be pointed at two different interpreters, so a package you installed in one is genuinely absent from the other. Once you can print the interpreter path from both places and see the same string, the whole setup stops being mysterious.
import sys
required = (3, 9)
current = sys.version_info[:3]
print("Version tuples compare like numbers:", (3, 10, 0) > (3, 9, 12))
print("Version strings do not:", "3.10" > "3.9")
print("This interpreter is new enough:", current >= required)Your editor does not contain Python; it points at one interpreter file on disk, and you need to know which one.
Worked examples
Find the interpreter behind the Run button
Confirms that sys.executable is a real absolute path, which is what you compare between editor and terminal.
import os
import sys
print("Path is absolute:", os.path.isabs(sys.executable))
print("File exists on disk:", os.path.exists(sys.executable))
print("Has a file name:", os.path.basename(sys.executable) != "")Example explained
Line 1sys.executable holds the full path of the interpreter running this file, not the command name you typed.
Line 2os.path.isabs confirms it is a complete path, so you can paste it into your editor's interpreter setting.
Line 3os.path.exists proves the file is really there, which rules out a stale PATH entry pointing at a deleted install.
Line 4Run this file from your terminal and from your editor: if the printed paths differ, the two are using different Pythons.
Guard a script against an old interpreter
Stops execution with a readable message when the wrong Python picks up the file.
import sys
if sys.version_info < (3, 9):
sys.exit("Needs Python 3.9 or newer, got " + sys.version.split()[0])
print("Version guard passed")Example explained
Line 1The guard sits at the top so it runs before any import that a newer Python would be needed for.
Line 2sys.version_info compares as a tuple of integers, so 3.9 versus 3.10 is ordered correctly.
Line 3sys.exit with a string prints that string to stderr and ends the program with a non-zero status.
Line 4sys.version.split()[0] trims the long build description down to just the version number.
Important notes
Do not remove or overwrite the system Python on macOS or Linux; other operating system tools call it, and replacing it can break them.
python --version prints to stdout on modern Python, but on Python 2 it printed to stderr, so a redirect that shows nothing is a hint you reached an ancient interpreter.
Common mistakes
Typing python on macOS or Linux where only python3 is on PATH: you get command not found, or on Windows an unexpected Microsoft Store page opens instead of an interpreter.
Skipping the Add Python to PATH checkbox in the Windows installer: the install succeeds but every terminal reports 'python' is not recognized, and the editor cannot start it either.
Installing a package in the terminal while the editor points at a different interpreter: the import raises ModuleNotFoundError even though pip clearly reported success.
Try it yourself
Change, predict, then run
In a browser editor, print sys.executable and sys.version_info side by side, then write an if statement that prints a different message when the minor version is below 10.
Open the Python workspaceCheck your understanding
You run pip install requests in your terminal and it succeeds, but pressing Run in your editor raises ModuleNotFoundError for requests. What is the most likely cause?
- The editor is configured to use a different interpreter than the one pip installed into
- The package was downloaded but not compiled, so it only works from the terminal
- The editor needs to be restarted before any newly installed package can be imported
- requests must be imported at the very top of the file or it cannot be found
Show answer
pip installs into the library folder of one specific interpreter, so a second interpreter simply has no such package; printing sys.executable in both places reveals the mismatch. Restarting the editor is tempting because it fixes some caching issues, but it cannot help here since the editor would restart with the same wrong interpreter setting.