Python does not need a separate compilation step: you write the code in a text file and run it with the interpreter.
Create your first program
Create a new file called hello.py with the following content:
print("Hello world!")
Python files use the extension .py.
Run it from the terminal
Open a terminal in the folder where you saved the file and run:
python hello.py
On macOS and Linux you may need to use python3 hello.py. The terminal will show:
Hello world!
What happens when you run it?
The interpreter reads your source code, translates it internally into bytecode (a low-level representation that it keeps in the __pycache__ folder) and executes it in the Python virtual machine. You do not have to do anything: all of this happens automatically each time you run the program.
Common errors
- "python is not recognized" or "command not found": Python is not installed or it is not in the PATH. Review the installation article.
- SyntaxError: there is a typing mistake in the code, such as a missing quote or parenthesis. Python shows the line where it found the problem.
- IndentationError: the lines of a block are not aligned correctly. Use 4 spaces for each level.