Python Module 1 – Getting started: install, run and read Python
What Python is and how it runs your code
What happens when you run a .py file (CPython compiles it to bytecode and runs it), how Python versions are supported and which implementations exist.
What you will learn
- Explain what the interpreter does when it runs a .py file
- Identify the current Python release and how long each version is supported
- Distinguish CPython from other implementations such as PyPy and Pyodide
On this page
Python is a general-purpose programming language with a small, readable syntax: blocks are marked by indentation rather than braces, and you do not declare the types of variables. It comes with a large standard library, so many everyday jobs (reading files, handling dates, talking to the web) need nothing extra. People use it for scripts and automation, web back ends, data analysis, machine learning, teaching and much more.
“Python” names two things: the language (the rules for what a program means, written down in the language reference) and the implementation, the program that actually runs your code. Most of the time the implementation is CPython, and this lesson is about what it does.
What happens when you run a file
When you run python3 hello.py, three things happen in order:
- The interpreter reads your source code, the text in
hello.py. - It compiles the whole file to bytecode: compact instructions for Python’s own virtual machine. If the file has a syntax error, it stops here, before a single line has run.
- The virtual machine then executes the bytecode, one instruction after another. Errors that only show up while the program runs, such as dividing by zero, happen in this step.
What CPython does when you run a file
Text description of the diagram
The diagram shows the three steps of running python3 hello.py, from top to bottom.
- Python reads the source code in hello.py.
- The compiler turns the whole file into bytecode, instructions for Python's virtual machine. A SyntaxError stops the program at this step, before any line of it has run.
- The virtual machine executes the bytecode, one instruction after another, and the program's output appears. A ZeroDivisionError and other errors that only show up while the program runs happen at this step.
There is no separate “compile, then run” command as in C or Java: both steps happen each time you run the file.
The standard library’s dis module (short for disassembler) shows you the bytecode of a function:
import dis
def add(a, b):
return a + b
dis.dis(add) Output
4 RESUME 0
5 LOAD_FAST_BORROW_LOAD_FAST_BORROW 1 (a, b)
BINARY_OP 0 (+)
RETURN_VALUE
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 show_bytecode.py
Each line of the output is one instruction. RESUME is bookkeeping at the start of every function; then the virtual
machine fetches the two arguments, adds them with the + operator and returns the result. You never need to read bytecode to write Python, but it explains why a syntax error anywhere in a file stops
the whole file from running.
Version note
Bytecode is a detail of CPython, not part of the language: its instructions change from one feature release to the next. The output above is what CPython 3.14 prints; never write code that depends on it.
Bytecode is cached for imported modules
When a program imports a module (another .py file), CPython saves that module’s bytecode in a folder called
__pycache__ next to it, so the next run can skip compiling it again. Here is a program in two files: main.py,
the program you run, and greeting.py, the module it imports.
cache_demo/main.py
import greeting
print(greeting.greet("Asha")) Output
Hello, Asha!
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 main.py
cache_demo/greeting.py
def greet(name):
return "Hello, " + name + "!" After python3 main.py has run once, __pycache__ holds greeting.cpython-314.pyc. The cpython-314 part says
which implementation and version wrote it, so several Python versions can share one folder without mixing up their
files. The file you run directly, main.py, is compiled every time and is not cached.
You can delete __pycache__ folders at any time; Python writes them again when it needs them. Leave them out of
version control (add __pycache__/ to your .gitignore).
Which Python version to use
All current Python code is Python 3. Python 2 has not received fixes for years, but its code still turns up in old answers online, and it fails on Python 3:
# Python 2 code that still turns up in old answers online
print "Hello, world" Output (exit status 1)
Printed as an error (standard error)
File "python2_print.py", line 2
print "Hello, world"
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 python2_print.py
Python 3 reports a SyntaxError and even suggests the fix: print is a function, so it needs parentheses. Other
signs of Python 2 code are raw_input() (now input()) and xrange() (now range()).
Within Python 3, versions are numbered 3.minor.micro:
- A feature release (3.13, 3.14, 3.15 …) adds language features and arrives about once a year.
- Each feature release then gets bugfix releases (3.14.1, 3.14.2 …) for about two years, then security fixes only, until about five years after it first came out. The Python Developer’s Guide lists the status of every version.
- A bugfix release changes no features, so code written for 3.14.2 normally runs unchanged on 3.14.8.
This track is written for Python 3.14, and its examples are run with CPython 3.14.8.
CPython and the other implementations
The Python you download from python.org is CPython, the reference implementation, written in C. Others run the same language in different places:
- PyPy adds a just-in-time compiler, which can make long-running programs much faster.
- MicroPython is a small Python for microcontrollers.
- Pyodide is CPython compiled to WebAssembly, so it runs inside a web browser, on your own device.
A program can ask which implementation, version and platform it is running on:
import sys
print("Implementation:", sys.implementation.name)
print("Version:", sys.version.split()[0])
print("Platform:", sys.platform) Output
Implementation: cpython Version: 3.14.8 Platform: darwin
Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 runtime_info.py
Runs on this device, in your browser. The first run downloads Python (about 13.5 MB), which is kept for the next runs.
Your run, in this browser
Version note
This site’s Python Online Compiler runs Pyodide 314.0.7 in your browser, and
Pyodide 314.0.7 contains CPython 3.14.2, so the version it reports is 3.14.2. Its platform is emscripten, the name
of Python built with Emscripten for WebAssembly. Both are Python 3.14, so this track’s examples behave the same in
both unless a lesson says otherwise.
In your browser, Pyodide 314.0.7 (CPython 3.14.2) prints:
Implementation: cpython Version: 3.14.2 Platform: emscripten
Key takeaways
- Running a
.pyfile compiles it to bytecode and then runs the bytecode on CPython’s virtual machine; a syntax error stops the file before anything runs. - Imported modules keep their bytecode in
__pycache__; the files are safe to delete and do not belong in version control. - Feature releases arrive about once a year and are supported for about five years; bugfix releases change no features. This track uses Python 3.14.
- CPython is the reference implementation; PyPy, MicroPython and Pyodide run the same language elsewhere.
Exercise
Exercise · Easy · Python
Report which Python is running
Write a function describe_runtime() that tells a program which Python is running it. It takes no arguments and returns a dictionary with exactly three keys:
- "implementation": the implementation's name in lower case, such as "cpython" (from sys.implementation.name); - "major_minor": the feature release as text, such as "3.14", built from the numbers in sys.version_info; - "in_browser": True when the program runs in a web browser, otherwise False. Pyodide, the Python of the browser, reports sys.platform as "emscripten".
On CPython 3.14.8 on a laptop it returns:
{'implementation': 'cpython', 'major_minor': '3.14', 'in_browser': False}
The sample tests import your function from describe.py, call it, and check each of the three entries. When you run them here, they run in your browser, so in_browser should be True.
Starter code · describe.py
import sys
def describe_runtime():
"""Return which Python runs this program: its implementation, feature release and whether it runs in a browser."""
# Replace this line with your code.
return {} The sample tests · test_describe.py
import sys
from describe import describe_runtime
def test_three_keys():
"""returns a dictionary with exactly the three keys"""
result = describe_runtime()
assert isinstance(result, dict)
assert sorted(result) == ["implementation", "in_browser", "major_minor"]
def test_implementation():
"""names the implementation in lower case"""
assert describe_runtime()["implementation"] == sys.implementation.name
def test_major_minor():
"""gives the feature release as text, such as 3.14"""
expected = f"{sys.version_info.major}.{sys.version_info.minor}"
assert describe_runtime()["major_minor"] == expected
def test_in_browser():
"""says whether the program runs in a browser, as True or False"""
assert describe_runtime()["in_browser"] is (sys.platform == "emscripten") A hint
sys.version_info.major and sys.version_info.minor are numbers: join them into text with an f-string such as f"{major}.{minor}". A comparison like sys.platform == "emscripten" is already True or False, so it can be the value of "in_browser" as it is.
Results of the sample tests
| Test | Result | Details |
|---|
What your code printed
The sample tests run on this device, in your browser (Pyodide): nothing is sent to mysmartcopilot.com. The first run downloads Python (about 13.5 MB), which is kept for the next runs. A check in your browser is feedback for you, not proof that the code is right for every input.
Check yourself
5 questions about this lesson. Every answer and why it is right is on the page, behind “Show the answer”. Your score stays in this browser.
References
- The Python Tutorial: Whetting Your Appetite (Python Software Foundation)
- The Python Tutorial: Using the Python Interpreter (Python Software Foundation)
- dis, the disassembler for Python bytecode (Python Software Foundation)
- PEP 3147: PYC Repository Directories (Python Software Foundation)
- Status of Python versions (Python Developer's Guide) (Python Software Foundation)
- Pyodide documentation (Pyodide)
Related tools
Report a problem with this lesson
Kept only in this browser. Your Learn progress