Setup Python environment on macOS
Setup Python environment on macOS
First install Homebrew.
Then install pyenv.
brew install pyenv
Install a specific version of Python.
pyenv install 3.12
Then install uv.
brew install uv
Install uv-managed python even if there’s already a Python installation on your system.
Once Python is installed, it will be used by uv command automatically.
When Python is installed by uv, it will not be available globally.
uv install 3.12.8
Created a new project.
uv init python-demos
Add dependencies.
uv add pytest
uv add pytest-cov
uv add pre-commit
Configure the pre-commit with tools upside.
.pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/PyCQA/flake8
rev: 7.1.1
hooks:
- id: flake8
args: [
"--ignore=E203,W503,E266",
--max-line-length=120
]
Then install the git hooks.
uv run pre-commit install
Now when you run git commit, it will run those tools automatically.
Run a file
Using python-demos as an example.
cd python-demos
# check a file
source ./scripts/check.sh ./leetcode_solutions/lc2235.py
# run a file
uv run ./leetcode_solutions/lc2235.py
# run pytest
uv run pytest
# ... or on a specific file
uv run pytest ./tests/test_leetcode_solutions/test_lc13.py