PYTHON / GETTING STARTED
What Python is and where it runs
Explain what the Python interpreter does with your source code, and judge which environments can actually run a .py file.
What you will learn
- Name Python's main implementations and say which one the python3 command starts
- Explain the compile-to-bytecode phase that finishes before your first line executes
- Use sys.executable and sys.implementation to see which interpreter runs your code
- Predict what an environment restricts before assuming a .py file will run there
Understanding What Python is and where it runs
Python is a language definition, not a single program. The definition is maintained as a written reference and a set of PEPs, and several separate programs implement it: CPython (written in C, the one you get from python.org), PyPy (which JIT-compiles to machine code), MicroPython (for microcontrollers with a few hundred kilobytes of RAM), Pyodide (CPython compiled to WebAssembly so it runs in a browser tab), and GraalPy on the JVM. When you type python3, you are launching one of these programs and handing it a text file.
That program does two distinct things. First it parses your whole file and compiles it into bytecode: compact instructions like LOAD_NAME and BINARY_OP stored in code objects. Only then does the evaluation loop start executing those instructions one by one. This split explains a behaviour beginners find surprising: a misspelled keyword on the last line of a file stops the file before line 1 prints anything, because compilation covers the entire module, while a misspelled variable name is only a problem when that instruction is actually reached. Bytecode targets Python's virtual machine rather than your CPU, which is why the same unmodified .py file works on an x86 laptop and an ARM server.
So Python runs wherever someone has ported an interpreter: desktop Linux, macOS and Windows; server processes behind WSGI or ASGI; container images and CI runners; microcontrollers through MicroPython; a browser tab through WebAssembly; and inside host applications such as Blender or QGIS that embed the interpreter as their scripting engine. What varies between these is almost never the syntax but the surroundings — Pyodide has no real filesystem or process spawning, MicroPython ships a trimmed subset of the standard library, and an embedded interpreter may forbid threads. Checking the environment, not the language, is what tells you whether your script will work there.
source = "total = 2 + 3\nprint(total)"
code_object = compile(source, "<demo>", "exec")
print(type(code_object))
print(code_object.co_names)
exec(code_object)Running Python means handing source text to an interpreter program that first compiles the whole module to bytecode and then executes it, so your code runs anywhere that interpreter exists.
Worked examples
Ask the interpreter about itself
Shows that the running interpreter is an ordinary object you can inspect from inside your program.
import sys
print(sys.implementation.name)
print(sys.byteorder in ("little", "big"))
print(len(sys.path) > 0)Example explained
Line 1sys.implementation.name prints cpython here; on PyPy the same line prints pypy, which is how you detect the implementation at runtime.
Line 2sys.byteorder reports the CPU the interpreter was built for, so the host machine leaks through even though your source did not change.
Line 3sys.path is the list of directories the interpreter will search for imports, which is the main thing that differs between a laptop, a container, and a browser runtime.
The same file, two roles
Demonstrates that how the interpreter loads a file changes __name__, which is how one file can be both a program and a library.
def area(width, height):
return width * height
print(__name__)
print(area(3, 4))Example explained
Line 1Executed directly, the interpreter names the top-level module __main__, so the first print shows __main__.
Line 2If another file did `import shapes` instead, the same line would print shapes, because the module is then loaded under its file name.
Line 3area itself is unaffected by the loading route, which is why library code stays reusable across scripts, servers, and notebooks.
Important notes
Code objects, __pycache__ files, and the exact contents of co_names are CPython implementation details; the language does not require bytecode, and PyPy or GraalPy execute your code by other means.
A .pyc file is a compile cache to speed up later imports, not a standalone executable: it still needs a matching interpreter version to run.
Common mistakes
Treating Python as one application and copying a .py file to a machine with no interpreter installed: nothing runs, the file just opens in a text editor or the shell reports command not found.
Reading "interpreted" as "executed strictly line by line", then expecting the first prints to appear before a syntax error further down: the module fails at compile time and produces no output at all.
Assuming code proven on CPython works on any Python: MicroPython or a browser runtime raises ImportError or blocks filesystem access for modules that CPython ships by default.
Try it yourself
Change, predict, then run
In a browser editor, print sys.implementation.name and sys.platform, then call compile("print('a')\nprint('b'", "<test>", "exec") and observe that the SyntaxError is raised before either print ever runs.
Open the Python workspaceCheck your understanding
A script prints three lines and then, on its very last line, has a typo that is a syntax error. You run the file. What happens?
- The three lines print, then the error is raised when execution reaches the final line.
- The three lines print and the invalid line is skipped with a warning.
- Nothing runs: the interpreter reports the syntax error while compiling the module, before executing any line.
- The error appears only if another file later imports this one.
Show answer
The interpreter compiles the entire module to bytecode before the evaluation loop starts, so a malformed line anywhere prevents every earlier statement from running. Option 1 is tempting because "interpreted" suggests line-by-line processing, but that describes the REPL, which compiles one statement at a time; a file is compiled as a whole unit.