Python · Setup: installing Python and running a script
Day one: installing Python 3, checking the version, and running a first script from the terminal.
I started the Python part of my path today with the most basic step: getting an interpreter installed and running.
On Windows the installer from python.org (or the py launcher) puts python on the PATH. A Python program is a plain text file ending in .py. Running python hello.py starts the interpreter, which reads the file from top to bottom and executes each statement in order.
There are two ways to work:
- Interactive mode (the REPL): type
pythonwith no file. Each line runs as soon as you press Enter. This is good for quick experiments. - Script mode: write the code in a file and run the whole file. This is what real programs use.
The sys module can tell a script which interpreter is running it. That helps when more than one Python is installed.
import sys
import platform
print("Hello, Python!")
print("Version:", platform.python_version())
print("Major/minor:", sys.version_info[:2])
Output:
Hello, Python!
Version: 3.12.10
Major/minor: (3, 12)
What I’m taking away: check python --version first whenever something behaves strangely, because a different interpreter is a very common cause.
Sources
- Using the Python Interpreter — Python Software Foundation
- Using Python on Windows — Python Software Foundation
- The Python Tutorial — Python Software Foundation