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 **”- Install
pyenvand ****pyenv-virtualenv:
curl <https://pyenv.run> | bash- Add
pyenvandpyenv-virtualenvto your shell configuration: Add the following lines to your~/.zshrc:
export PYENV_ROOT="$HOME/.pyenv"export PATH="$PYENV_ROOT/bin:$PATH"eval "$(pyenv init --path)"eval "$(pyenv init -)"eval "$(pyenv virtualenv-init -)"- Restart your shell:
exec $SHELL2. Installing the Latest Python Version and Creating a Virtual Environment
Section titled “2. Installing the Latest Python Version and Creating a Virtual Environment”- 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 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 pip3. Creating and Managing Virtual Environments
Section titled “3. Creating and Managing Virtual Environments”💡
3.12.5is the latest version as of 2024-09-09
- Create a new virtual environment:
pyenv virtualenv 3.12.5 eon-3.12.5- Activate the virtual environment:
pyenv activate eon-3.12.5- Deactivate the virtual environment:
pyenv deactivate- Delete a virtual environment:
pyenv virtualenv-delete eon-3.12.54. Verifying the Environment
Section titled “4. Verifying the Environment”- Check the actual paths to the executables:
pyenv which pythonpyenv which pip- Create a temporary Python script to log environment details:
# Create a temporary Python script to log environment detailsecho 'import sysimport osimport 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 scriptpython /tmp/log_env_details.py
# Clean up by removing the temporary Python scriptrm /tmp/log_env_details.py- Check the
pyvenv.cfgfile:
cat /Users/terryli/.pyenv/versions/eon-3.12.5/pyvenv.cfg- Use
pip debugcommand:
python -m pip debug5. Installing and Verifying Packages
Section titled “5. Installing and Verifying Packages”- Install a package using ****
pip:
python -m pip install requests- Verify the installed packages:
python -m pip list6. Understanding Shims and PATH
Section titled “6. Understanding Shims and PATH”- Understanding how
pyenvshims work:
pyenvinserts a directory of shims at the front of yourPATH:
$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin- Shims are lightweight executables that pass your command along to
pyenv.
- Ensuring correct environment activation:
- Always activate the virtual environment before running any Python or
pipcommands:
pyenv activate eon-3.12.5- Verify the environment:
pyenv which pythonpyenv which pip7. Example Workflow
Section titled “7. Example Workflow”Here is an example workflow that ties everything together:
# Ensure pyenv and pyenv-virtualenv are correctly initialized in your shellecho '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 shellexec $SHELL
# Install the latest Python versionlatest_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 environmentpyenv virtualenv $latest_python_version eon-$latest_python_version
# Activate the virtual environmentpyenv activate eon-$latest_python_version
# Upgrade pippython -m pip install --upgrade pip
# Verify the environmentpyenv which pythonpyenv which pip
# Install a package using pippython -m pip install requests
# Verify the installed packagespython -m pip list
# Create a temporary Python script to log environment detailsecho 'import sysimport osimport 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 scriptpython /tmp/log_env_details.py
# Clean up by removing the temporary Python scriptrm /tmp/log_env_details.py
# Check the pyvenv.cfg filecat /Users/terryli/.pyenv/versions/eon-$latest_python_version/pyvenv.cfg
# Use pip debug commandpython -m pip debugBy 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.