Overview
This article documents how to set up a Python environment for development on a Mac local environment.
In this case, we will use two systems to manage different versions of Python and virtual environments:
- pyenv
- Used to handle multiple versions of Python.
- venv
- Used to separate environments for each project.
For explanations on the differences and the necessity of each, this article is a helpful reference.
Installing Python
First, install Pyenv on your local environment to use a specific version of Python.
Install pyenv.
|
|
Check the installed version of pyenv.
|
|
Add settings to zsh.
|
|
Reload .zshrc.
|
|
Display a list of installable Python versions.
|
|
Install the specified version.
|
|
Use the specified Python version in your project folder.
|
|
If global, it will be applied to the entire system.
|
|
Check the version of Python being executed.
|
|
Creating a Virtual Environment with venv
Create a virtual environment in the project directory.
|
|
Activate the virtual environment.
|
|
To deactivate, execute the following command.
|
|
How to Update a venv
If you need to refresh dependencies, activate the virtual environment and run the following. When using requirements.txt, update the file first and then reinstall.
|
|
When upgrading Python to a new minor version, install the new version with pyenv, recreate the virtual environment, and reinstall dependencies to avoid mismatches.
|
|
How to Delete a venv
When the virtual environment is no longer needed, delete the directory. Confirm you are in the correct working directory before running the command.
|
|
This completes the setup of the local environment.