Installation & Setup
Before you can write a single line of Python, you need Python installed on your computer along with a way to run the code you write. This page walks through downloading the official installer, verifying the installation, running code two different ways, installing third-party packages, and keeping projects isolated with virtual environments.
Downloading and Installing Python
The official distribution of Python is available for free at python.org. The installer differs slightly by operating system:
| Operating System | Steps |
|---|---|
| Windows | Download the Windows installer (.exe) from python.org, run it, and on the very first screen check the box "Add Python to PATH" before clicking Install Now. This lets you run python from any Command Prompt window. |
| macOS | Download the macOS installer (.pkg) from python.org and run it. macOS ships with an older system Python, so installing the official version keeps you on a current release. |
| Linux | Most distributions already include Python 3. If not, install it via the package manager, for example sudo apt install python3 python3-pip on Debian/Ubuntu. |
Note: On Windows, forgetting to check "Add Python to PATH" is the single most common reason beginners see a "python is not recognized" error later in the Command Prompt.
Verifying the Installation
Once installed, open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and check the version to confirm Python is available on the PATH:
# Windows
python --version
# Mac / Linux (python3 is often the correct command)
python3 --version
Either command should print something like Python 3.12.4. If you get an error instead, double check that Python was added to your system PATH during installation.
REPL vs Running a Script File
There are two common ways to execute Python code:
- The REPL (Read-Eval-Print Loop): Typing
python(orpython3) alone in the terminal drops you into an interactive prompt where you can type one line of code at a time and immediately see the result. This is great for quick experiments. - Running a script file: For real programs you save your code in a file with a
.pyextension and run the whole file at once.
# Start the interactive REPL
python
# Inside the REPL you can type expressions directly
>>> print("Hello from the REPL")
Hello from the REPL
# Exit the REPL
>>> exit()
To run a saved file, create hello.py with the following content:
# hello.py
print("Hello, World!")
Then execute it from the terminal in the same folder:
python hello.py
Installing Packages with pip
pip is Python's official package manager and is installed automatically alongside Python. It downloads and installs third-party libraries from the Python Package Index (PyPI) so you don't have to write everything from scratch.
# install a single package
pip install requests
# install a specific version
pip install requests==2.31.0
# list installed packages
pip list
# remove a package
pip uninstall requests
Virtual Environments
A virtual environment is an isolated, self-contained copy of Python along with its own set of installed packages, separate from your system-wide Python installation. This matters because different projects often need different, sometimes conflicting, versions of the same library. Without isolation, installing a package for one project could silently break another.
# create a virtual environment named "myenv"
python -m venv myenv
# activate it (Windows)
myenv\Scripts\activate
# activate it (Mac / Linux)
source myenv/bin/activate
# once activated, packages installed with pip stay local to this project
pip install requests
# deactivate when you are done
deactivate
Note: When a virtual environment is active, the terminal prompt usually shows its name in parentheses, e.g. (myenv), so you always know which environment you are working in.
Choosing a Code Editor
While you can write Python in any plain text editor, a dedicated editor or IDE makes development much faster with features like syntax highlighting, auto-completion, and built-in debugging. Two of the most popular choices are:
- VS Code: A free, lightweight, highly extensible editor with an excellent official Python extension.
- PyCharm: A full-featured IDE built specifically for Python, with a free Community edition and a paid Professional edition for web and data science work.