Skip to main content

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.

  1. Install pyenv and dependencies with package manager according to the official suggested build environment.

    Mac OS
    brew install openssl readline sqlite3 xz zlib tcl-tk
  2. 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.

  3. See which versions of python is available for install

    pyenv install -l
  4. 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.

  5. Set global python version

    pyenv global 3.11
  6. Relogin / restart shell.

  7. 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.

  1. Install the correct python version if needed (optonal).

    pyenv install <PYTHON_VERSION>
  2. 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.

  3. Create virtual environment

    python -m venv .venv # Creates virtual environment at the .venv/ folder
    echo ".venv" >> .gitignore # Add it to .gitignore
  4. Activate the venv and verify

    source .venv/bin/activate
    which python # Should output <PROJECT>/.venv/bin/python
  5. Upgrade pip

    python -m pip install --upgrade pip
  6. 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