Setting Up A Python Development Environment - September 6, 2023

Setting up a proper development environment is crucial for productive software development. This blog post guides you through setting up a Python development environment on macOS, using tools like Homebrew, uv, Zsh, Virtualenv, and Pip.

Installing uv via Homebrew

First, we will install uv and some essential libraries via Homebrew. Open your terminal and run:

brew install uv openssl readline sqlite3 xz zlib

Setting Up Zsh

Next, add uv’s bin directory to your Zsh configuration:

echo -e "\n# Add uv to PATH\nexport PATH=\"$HOME/.uv/bin:$PATH\"" >> ~/.zshrc

Installing Python Using uv

To manage multiple Python versions easily, we’ll use uv. Install the latest version of Python as follows:

uv python install
python --version

Upgrading Pip and Setuptools

Before proceeding, ensure that you are using the latest versions of Pip and Setuptools:

uv pip install --upgrade pip setuptools

Setting Up Virtualenv

Virtual environments are essential for isolating project dependencies. To install Virtualenv, run:

uv pip install virtualenv

Directory Setup

Now let’s create some directories to organize our projects, virtual environments, and Pip configuration files:

mkdir -p ~/Projects ~/Virtualenvs ~/.config/pip

Configuring Pip

Edit the Pip configuration file to require a virtual environment for installations:

vim ~/.config/pip/pip.conf

Add the following lines:

[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

Modifying Zsh Configuration

Add the following function to your ~/.zshrc file to allow global Pip installations:

gpip(){
   PIP_REQUIRE_VIRTUALENV="0" python -m pip "$@"
}

Testing Your Setup

Finally, test your setup:

pip install --upgrade pip setuptools virtualenv  # This shouldn't work

To perform global installations:

gpip install --upgrade pip setuptools virtualenv  # This should work

That’s it! You now have a functional Python development environment set up on your macOS system. Feel free to adjust the settings to suit your personal workflow. Happy coding!