[Setup] Python Development Environment
This page explains how to set up a Python development environment. Python is easy to install, and you can run code from the terminal (command prompt) right away.
Installing Python
| OS | Installation Method |
|---|---|
| Windows | Download and run the installer from python.org. Make sure to check "Add python.exe to PATH" on the installation screen. If you skip this, the python command will not be available in the terminal. |
| macOS | macOS comes with Python 3 pre-installed. Run python3 --version in a terminal to verify. If you need the latest version, run brew install python with Homebrew. |
After installation, open a terminal (Command Prompt on Windows) and run the following.
python3 --version Python 3.12.4
If a version number is displayed, the installation was successful. On Windows, use python --version instead.
Using Interactive Mode (REPL)
Type python3 (or python on Windows) in a terminal to launch interactive mode (REPL). You can enter code one line at a time and see the result immediately, making it ideal for learning.
python3
Python 3.12.4 (main, Jun 6 2024, 18:26:44)
>>> print("Hello, Python!")
Hello, Python!
>>> 1 + 2
3
>>> name = "Alice"
>>> print("Hello, " + name + "!")
Hello, Alice!
>>> exit()
To exit interactive mode, type exit(), or press Ctrl + D (on Windows: Ctrl + Z then Enter).
Creating and Running a .py File
This method involves writing Python code in a text editor, saving it with the .py extension, and running it. Use this approach for programs with multiple lines.
Save the following content as hello.py using a text editor.
name = input("Enter your name: ")
print("Hello, " + name + "!")
for i in range(1, 6):
print("Loop iteration " + str(i))
print("Program finished!")
Navigate to the folder where you saved the file in a terminal, then run it with the following command.
For macOS / Linux:
python3 hello.py
For Windows:
python hello.py
Recommended Editors
| Editor | Features |
|---|---|
| Visual Studio Code | Installing the Python extension gives you code completion, debugging, and error highlighting. Free. |
| IDLE | The editor bundled with Python. No additional installation needed — ready to use right away. |
| PyCharm Community | A Python-dedicated editor with many advanced features. It can be a bit heavy, so it is recommended once you are more comfortable with Python. Free. |
Command Not Found
If the terminal shows python3: command not found, the PATH may not be configured correctly. Follow the steps below to check and set it up.
1. Locate the command
Check where the python3 command is installed.
which python3
If it is not found, check the common installation paths.
ls /usr/local/bin/python3 ls /opt/homebrew/bin/python3
On macOS, use python3 instead of python. macOS may have an old Python 2 registered as python, but Python 2 has already reached end of life.
2. Check your shell
echo $SHELL
If the output is /bin/zsh, edit ~/.zshrc. If it is /bin/bash, edit ~/.bashrc.
3. Add the path to PATH
Once you know the installation path, add it to your shell configuration file.
For macOS (zsh):
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
For Linux (bash):
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc source ~/.bashrc
On Windows, the easiest fix is to reinstall Python and check "Add python.exe to PATH". To add it manually, go to "System Properties" → "Environment Variables" → "Path" and add the Python installation folder.
If you find any errors or copyright issues, please contact us.