Install python and virtualenv on a mac

Install python and virtualenv on a mac
Photo by David Clode / Unsplash

Always forget how I usually do this, so making a note.

See: https://docs.python-guide.org/dev/virtualenvs/

Install brew python

  1. brew install python
  2. Add brewed python to path: vi ~/.zshrc and add export PATH="$(brew --prefix)/opt/python/libexec/bin:$PATH". See this awesome SO answer for details: https://stackoverflow.com/a/51939526/1265167
  3. source ~/.zshrc
  4. python --version or which python should now point to the brewed python

Install pipenv

Decided to go with pipenv this time rather than the lower level virtualenv:

Update 2025

As of 2025 I got an error trying to install pipenv the way detailed in the docs above and outlined below. In the error, it suggests just installing python packages system wide using homebrew, so I did that instead. Other packages can still be installed using pipenv per "project" (i.e. directory).

  1. brew install pipenv

Previous notes

  1. pip install --user pipenv
  2. As instructed, upgrade pip: python -m pip install --upgrade pip
  3. Add pipenv to path: vi ~/.zshrc and export PATH="$(python -m site --user-base)/bin:$PATH"
  4. source ~/.zshrc
  5. pipenv should now be available

Use pipenv

  1. mkdir -p ~/Projects/Python/locust.io && cd $_ (or take ~/Projects/Python/locust.io for oh-my-zsh users)
  2. pipenv install locust - installs and creates a Pipfile in the local directory
  3. pipenv run locust -V OR pipenv shell to not have to keep typing pipenv run in front of everything (see the starship command line - whoop whoop!)
  4. Create script e.g. locustfile.py
  5. pipenv run python locustfile.py (or just plain old python locustfile.py from that shell)