Python is one of the world’s most widely used programming languages, celebrated for its clean syntax, ease of learning, and powerful ecosystem of libraries. From building web apps and automating everyday tasks to performing data analysis and diving into machine learning, Python makes it easy to write efficient, readable code.
This guide will walk you through installing Python on macOS, using tools like pyenv
or the system installer to manage versions. You’ll also learn how to create virtual environments, install packages with pip
, write and run your first script, and set up an editor or IDE to make development smoother and more productive.
macOS comes with a preinstalled version of Python 2.x, but this is outdated and not suitable for modern development. You'll want to install Python 3, which is the current standard. To do so, make sure you have command line tools installed first.
To install these tools, open Terminal and run:
xcode-select --install
The recommended way to install Python 3 on macOS is via Homebrew. If you don’t already have Homebrew installed, install it with the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then install Python by running:
brew install python
This installs the latest version of Python 3 and adds the python3
and pip3
commands to your terminal.
To manage multiple versions of Python, pyenv
is a great tool. You can install it with Homebrew:
brew install pyenv
Then follow these steps to configure it in your shell (for example, Zsh):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init --path)"' >> ~/.zshrc source ~/.zshrc
To create isolated environments for your projects, Python also includes venv
:
python3 -m venv myenv source myenv/bin/activate
Once you've installed Python via Homebrew, verify the installation by running the following command in your terminal:
python3 --version
This should return something like Python 3.x.x
. You can also check that pip3
, Python's package installer, is available by running pip3 --version
.
Python comes with pip
for installing packages, but for isolated project environments, use either venv
or virtualenv
. To create a new virtual environment with venv
:
python3 -m venv myproject-env source myproject-env/bin/activate
To install packages inside this environment, simply use pip install
as usual.
For globally managing CLI tools, pipx
is helpful. Install it with:
brew install pipx pipx ensurepath
Then you can install tools like black
or httpie
with isolated environments:
pipx install black
You can launch the Python interactive shell, also known as the REPL, by typing:
python3
This drops you into an interactive environment where you can run Python code line by line. It’s a great way to test ideas or debug snippets before putting them into a file.
To exit the REPL, just type exit()
or press Ctrl + D
.
To get started writing Python code, open your preferred text editor and create a file named hello.py
. Inside, add the following line:
print("Hello, world!")
Save the file and run it from your terminal using:
python3 hello.py
If you see the output Hello, world!
, congratulations — you’ve just written and executed your first Python script on macOS.
Python works well with many editors, but for beginners, Visual Studio Code is a popular and lightweight choice. Once installed, be sure to add the official Python extension from Microsoft to get syntax highlighting, code completion, and integrated debugging.
If you prefer a more powerful IDE, options like PyCharm offer deep Python support, including virtual environment management and testing tools. Whichever editor you choose, make sure it’s configured to use python3
as the interpreter and has linting and formatting tools like flake8
and black
available for best results.
To create a simple Python project, start by setting up a dedicated directory. Open your terminal and run:
mkdir my_first_project cd my_first_project python3 -m venv venv source venv/bin/activate
Once your virtual environment is active, create a main.py
file. You can write any Python logic here, like a basic calculator or a to-do list app. It’s also common practice to add a requirements.txt
file to list project dependencies.
Now that your Python setup is complete, you can begin exploring more advanced topics like object-oriented programming, working with APIs, and web development with Flask or Django. The official [Python documentation](https://docs.python.org/3/) is a valuable reference as you progress.
Other great resources include [Real Python](https://realpython.com/), [freeCodeCamp](https://www.freecodecamp.org/learn/), and [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/). Consider joining communities like r/learnpython on Reddit or the Python Discord server to ask questions and stay motivated.