Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

  • Beginner
  • 12 minutes
  • Examples run with Python 3.14.8 and Pyodide 314.0.7
  • By MySmartCoPilot

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:

  1. The interpreter reads your source code, the text in hello.py.
  2. 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.
  3. 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.
Running hello.py: CPython reads the file, compiles it to bytecode, and its virtual machine executes the bytecode to print the output.hello.py(source code)CompilerA SyntaxError stops here,before any line has runBytecodeinstructions for thevirtual machineVirtual machineZeroDivisionError and otherrun-time errors happen hereOutput1. read2. compile3. execute

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.

  1. Python reads the source code in hello.py.
  2. 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.
  3. 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:

The bytecode of a small function Python · show_bytecode.py
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.

A program in two files

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 on Python 3 Python · python2_print.py
# 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:

Which Python is running? Python · runtime_info.py
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

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
Python Online Compiler Run this lesson's examples in your browser, with nothing to install.

Key takeaways

  • Running a .py file 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.

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.

  1. Question 1 of 5 You run python3 hello.py, and the last line of the file has a syntax error. When does Python report it?

    Choose one answer.

    Show the answer to question 1

    Answer: Before any line of the file runs, while it compiles the whole file to bytecode

    CPython compiles the whole file to bytecode before it runs any of it, and a syntax error stops the compiling. Errors that only appear while the program runs, such as dividing by zero, happen later, when their line runs.

  2. Question 2 of 5 What does CPython keep in a __pycache__ folder?

    Choose one answer.

    Show the answer to question 2

    Answer: The bytecode of modules that a program imports, so the next run can skip compiling them

    Imported modules have their bytecode cached as .pyc files, named after the implementation and version that wrote them (greeting.cpython-314.pyc). The file you run directly is compiled every time and not cached. The folder is safe to delete and does not belong in version control.

  3. Question 3 of 5 greeting.py defines greet(name), which returns "Hello, " + name + "!". What does main.py print when you run it?

    What does this program print? Choose one answer.

    import greeting
    
    print(greeting.greet("Asha"))
    Show the answer to question 3

    Answer: it prints

    Hello, Asha!

    import greeting runs greeting.py once, which defines the function; main.py then calls it and prints what it returns, with the exclamation mark the function adds.

  4. Question 4 of 5 This program runs in your browser, in Pyodide. What does it print?

    Read the code, then choose one answer.

    import sys
    print(sys.platform == "emscripten")
    Show the answer to question 4

    Answer: True

    Pyodide is CPython built with Emscripten for WebAssembly, so sys.platform is "emscripten" there, and the comparison is True. On a computer, CPython reports the operating system instead, such as "darwin" on macOS.

  5. Question 5 of 5 Which of these are implementations of the Python language, programs that run Python code?

    Choose every answer that is right.

    Show the answer to question 5

    Answer:

    • Pyodide
    • PyPy
    • CPython

    CPython is the reference implementation from python.org, PyPy adds a just-in-time compiler, and Pyodide is CPython compiled to WebAssembly. pip is a tool that installs packages, and __pycache__ is a folder of cached bytecode.

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.