How to Start a Python Project
These steps will help you set up and manage a Python environment on macOS, using Homebrew.
Watch how I used Cursor to learn about python environments:
TLDR
# Install Python3 with Homebrewbrew install python3
# Create a virtual environmentpython3 -m venv my-venv
# Activate a virtual environmentsource my-venv/bin/activate
# Install packages packagespip install <package-name>
# Generate a requirements.txt filepip freeze > requirements.txt
# Deactivate an active virtual environmentdeactivate
Install Python3
If not installed, use your system package manager to install Python3.
Instructions here use Homebrew for macOS.
brew install python3
Create a Virtual Environment with venv
python3 -m venv my-venv
Use any directory name you like.
Activate a Virtual Environment with source
source my-venv/bin/activate
Install Packages with pip
pip install <package-name>
Example: pip install awscli
Generate a requirements.txt
file with pip freeze
pip freeze > requirements.txt
The resulting file will look like this:
awscli==1.34.10botocore==1.35.10colorama==0.4.6docutils==0.16jmespath==1.0.1pyasn1==0.6.0python-dateutil==2.9.0.post0PyYAML==6.0.2rsa==4.7.2s3transfer==0.10.2six==1.16.0urllib3==2.2.2
Deactivate the Virtual Environment
deactivate
Some prefer virtualenv
over venv
While Python3 comes with venv
by default, some use virtualenv for “additional features” — though I can’t speak directly to the differences.
Additional reading on virtual environments in Homebrew
- https://nicolaiarocci.com/when-homebrew-breaks-your-python-virtual-environment/
- https://stackoverflow.com/questions/25701133/how-to-tell-homebrew-to-install-inside-virtualenv
- https://formulae.brew.sh/formula/virtualenv
- https://github.com/orgs/Homebrew/discussions/4419
- https://discuss.python.org/t/on-macos-14-pip-install-throws-error-externally-managed-environment/50352
- https://gist.github.com/pandafulmanda/730a9355e088a9970b18275cb9eadef3?permalink_comment_id=2809269
- https://courses.cs.washington.edu/courses/cse446/18wi/sections/section1/virtualenv_handout.pdf
- https://www.reddit.com/r/learnpython/comments/xf1pv6/brew_python_and_libraries_installation_blues/