Python Environment Setup with pyenv and venv
Setup for python development environment. We will use pyenv (version manager) + venv instead of conda.
Setup pyenv
This section is for the installation of pyenv itself. It should be only done once per machine.
Install pyenv and dependencies with package manager according to the official suggested build environment.
Mac OSbrew install openssl readline sqlite3 xz zlib tcl-tk
Initialize pyenv so the shell uses it by default
pyenv init # Follow the instructions outputted
I put mine in the login shell (
.zprofile
) instead of the interactive shell settings.See which versions of python is available for install
pyenv install -l
Install one of the versions
pyenv install 3.11 # Installs the latest revision of python 3.11
If you encounter dependency errors such as missing lzma lib, it maybe because the dependency in step1 is not configured properly.
Set global python version
pyenv global 3.11
Relogin / restart shell.
Verify installation
which python # Should output ~/.pyenv/shims/python instead of /bin/python
python --version # Should output previously set global python version
Setup project environment
This section is for project environment setup. It should be done for every project.
To start, make sure you're in the project directory.
Install the correct python version if needed (optonal).
pyenv install <PYTHON_VERSION>
Set the local python version (optional)
pyenv local <PYTHON_VERSION> # Will overwrite .python-version
This binds the python version to project directory by creating
.python-version
in the directory.Create virtual environment
python -m venv .venv # Creates virtual environment at the .venv/ folder
echo ".venv" >> .gitignore # Add it to .gitignoreActivate the venv and verify
source .venv/bin/activate
which python # Should output <PROJECT>/.venv/bin/pythonUpgrade pip
python -m pip install --upgrade pip
If its an existing project that has
requirements.txt
, install the requirements. (optional)python -m pip install -r requirements.txt
Extras:
- Freeze the project requirements into
requirements.txt
python -m pip freeze > requirements.txt
- Update all outdated packages
pip list -o | tail -n +3 | awk '{ print $1 }' | xargs pip install -U