Table of contents
Install
Using PyEnv
- Install pyenv using Homebrew:
brew install pyenv
- Add pyenv to your shell configuration. For zsh (default in newer macOS), add these lines to your ~/.zshrc file:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
- Install the Python version you need:
pyenv install 3.10.x
- Set it as your global Python:
pyenv global 3.10.x
Working with Virtual Environments
Virtual environments are an essential tool for modern Python development. They create isolated spaces where you can install packages without affecting your
system-wide Python installation. This prevents dependency conflicts between different projects.
Here's how to create and use a virtual environment:
- Create a new virtual environment :
python3 -m venv my_project_env
- Activate the virtual environment:
source my_project_env/bin/activate
- Your terminal prompt will change to show the active environment. Now you can install packages that will only affect this environment:
pip install package_name
- When you’re done, deactivate the environment:
deactivate
- Create a new virtual environment :