Skip to content

pyenv

Source: Notion | Last edited: 2024-09-26 | ID: b092e87b-7ab...


1. **Setting Up pyenv and pyenv-virtualenv in macOS **

Section titled “1. **Setting Up pyenv and pyenv-virtualenv in macOS **”
  1. Install pyenv and ****pyenv-virtualenv:
Terminal window
curl <https://pyenv.run> | bash
  1. Add pyenv and pyenv-virtualenv to your shell configuration: Add the following lines to your ~/.zshrc:
Terminal window
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
  1. Restart your shell:
Terminal window
exec $SHELL

2. Installing the Latest Python Version and Creating a Virtual Environment

Section titled “2. Installing the Latest Python Version and Creating a Virtual Environment”
  1. Install the latest Python version:
Terminal window
latest_python_version=$(pyenv install --list | grep -E "^\\s*3\\.[0-9]+\\.[0-9]+$" | tail -1 | tr -d ' ')
pyenv install $latest_python_version
  1. Create a virtual environment:
Terminal window
pyenv virtualenv $latest_python_version eon-$latest_python_version
  1. Activate the virtual environment:
Terminal window
pyenv activate eon-$latest_python_version
  1. Upgrade ****pip:
Terminal window
python -m pip install --upgrade pip

3. Creating and Managing Virtual Environments

Section titled “3. Creating and Managing Virtual Environments”

💡

3.12.5is the latest version as of 2024-09-09

  1. Create a new virtual environment:
Terminal window
pyenv virtualenv 3.12.5 eon-3.12.5
  1. Activate the virtual environment:
Terminal window
pyenv activate eon-3.12.5
  1. Deactivate the virtual environment:
Terminal window
pyenv deactivate
  1. Delete a virtual environment:
Terminal window
pyenv virtualenv-delete eon-3.12.5
  1. Check the actual paths to the executables:
Terminal window
pyenv which python
pyenv which pip
  1. Create a temporary Python script to log environment details:
Terminal window
# Create a temporary Python script to log environment details
echo '
import sys
import os
import logging
logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] %(levelname)s %(message)s")
logger = logging.getLogger(__name__)
def log_environment_details():
logger.debug(f"sys.prefix: {sys.prefix}")
logger.debug(f"sys.executable: {sys.executable}")
logger.debug(f"sys.path: {sys.path}")
logger.debug(f"PYTHONPATH: {os.environ.get("PYTHONPATH", "")}")
logger.debug(f"VIRTUAL_ENV: {os.environ.get("VIRTUAL_ENV", "")}")
if __name__ == "__main__":
log_environment_details()
' > /tmp/log_env_details.py
# Run the temporary Python script
python /tmp/log_env_details.py
# Clean up by removing the temporary Python script
rm /tmp/log_env_details.py
  1. Check the pyvenv.cfg file:
Terminal window
cat /Users/terryli/.pyenv/versions/eon-3.12.5/pyvenv.cfg
  1. Use pip debug command:
Terminal window
python -m pip debug
  1. Install a package using ****pip:
Terminal window
python -m pip install requests
  1. Verify the installed packages:
Terminal window
python -m pip list
  1. Understanding how pyenv shims work:
  • pyenv inserts a directory of shims at the front of your PATH:
Terminal window
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin
  • Shims are lightweight executables that pass your command along to pyenv.
  1. Ensuring correct environment activation:
  • Always activate the virtual environment before running any Python or pip commands:
Terminal window
pyenv activate eon-3.12.5
  1. Verify the environment:
Terminal window
pyenv which python
pyenv which pip

Here is an example workflow that ties everything together:

Terminal window
# Ensure pyenv and pyenv-virtualenv are correctly initialized in your shell
echo '
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
' >> ~/.zshrc
# Restart your shell
exec $SHELL
# Install the latest Python version
latest_python_version=$(pyenv install --list | grep -E "^\\s*3\\.[0-9]+\\.[0-9]+$" | tail -1 | tr -d ' ')
pyenv install $latest_python_version
# Create a new virtual environment
pyenv virtualenv $latest_python_version eon-$latest_python_version
# Activate the virtual environment
pyenv activate eon-$latest_python_version
# Upgrade pip
python -m pip install --upgrade pip
# Verify the environment
pyenv which python
pyenv which pip
# Install a package using pip
python -m pip install requests
# Verify the installed packages
python -m pip list
# Create a temporary Python script to log environment details
echo '
import sys
import os
import logging
logging.basicConfig(level=logging.DEBUG, format="[%(asctime)s] %(levelname)s %(message)s")
logger = logging.getLogger(__name__)
def log_environment_details():
logger.debug(f"sys.prefix: {sys.prefix}")
logger.debug(f"sys.executable: {sys.executable}")
logger.debug(f"sys.path: {sys.path}")
logger.debug(f"PYTHONPATH: {os.environ.get("PYTHONPATH", "")}")
logger.debug(f"VIRTUAL_ENV: {os.environ.get("VIRTUAL_ENV", "")}")
if __name__ == "__main__":
log_environment_details()
' > /tmp/log_env_details.py
# Run the temporary Python script
python /tmp/log_env_details.py
# Clean up by removing the temporary Python script
rm /tmp/log_env_details.py
# Check the pyvenv.cfg file
cat /Users/terryli/.pyenv/versions/eon-$latest_python_version/pyvenv.cfg
# Use pip debug command
python -m pip debug

By following this tutorial, you should have a solid understanding of how to manage your Python environments using pyenv and pyenv-virtualenv, ensuring that you can easily create, activate, and verify virtual environments.