How to Get Pyenv Up and Running
A Comprehensive Guide for Managing Python Versions on MacOS and Windows
Managing multiple Python versions can be a hassle, but with pyenv, it becomes a seamless process. Whether you're working on a Windows or MacOS system, pyenv helps you easily switch between different Python versions for different projects. This guide will walk you through the steps to install and set up pyenv on both Windows and MacOS.
Introduction
pyenv is a powerful tool that allows you to easily install and switch between multiple versions of Python. This is especially useful when working on projects that require different Python versions. With `pyenv`, you can manage your Python versions independently from the system Python, ensuring compatibility and avoiding conflicts.
Prerequisites
Before you begin, ensure you have the following:
Basic knowledge of using the command line.
Administrator access to your system.
Installing pyenv on MacOS
Step 1: Install Homebrew
Homebrew is a popular package manager for MacOS. If you don't have Homebrew installed, you can install it by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Step 2: Install pyenv
With Homebrew installed, you can now install `pyenv` by running:
brew updatebrew install pyenvSee here if you need to install homebrew
Step 3: Configure your shell
To ensure pyenv works correctly, you need to add it to your shell configuration. Add the following lines to your `.bash_profile`, `.zshrc`, or other relevant shell configuration file:
if command -v pyenv &> /dev/null; then
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
fiAfter adding the lines, reload your shell configuration:
source ~/.bash_profileStep 4: Verify installation
To verify that `pyenv` is installed correctly, run:
pyenv --versionYou should see the version of `pyenv` installed.
Installing pyenv on Windows
Step 1: Install prerequisites
Before installing `pyenv` on Windows, you need to install some prerequisites:
Python (ensure you add Python to PATH during installation)
Visual Studio Code https://code.visualstudio.com/ (optional, but recommended)
Step 2: Install pyenv-win
Clone the `pyenv-win` repository to your local machine:
git clone https://github.com/pyenv-win/pyenv-win.git %USERPROFILE%\.pyenvStep 3: Add pyenv to system PATH
Add the following paths to your system PATH environment variable:
%USERPROFILE%\.pyenv\pyenv-win\bin
%USERPROFILE%\.pyenv\pyenv-win\shimsTo do this, follow these steps:
Open the Start Menu and search for "Environment Variables".
Click on "Edit the system environment variables".
In the System Properties window, click on "Environment Variables".
Under "System variables", find and select the `Path` variable, then click "Edit".
Add the paths mentioned above to the list.
Step 4: Verify installation
To verify that `pyenv` is installed correctly, open a new Command Prompt or PowerShell window and run:
pyenv --versionYou should see the version of `pyenv` installed.
Using pyenv
Installing Python versions
With `pyenv` installed, you can now install different versions of Python. For example, to install Python 3.9.5, run:
pyenv install 3.9.5Setting global and local Python versions
You can set the global Python version (the default version) with:
pyenv global 3.9.5To set a local Python version for a specific project, navigate to the project directory and run:
pyenv local 3.8.10This will create a `.python-version` file in the project directory specifying the Python version.
Listing installed versions
To list all Python versions installed via `pyenv`, run:
pyenv versionsConclusion
Managing multiple Python versions is made simple and efficient with pyenv. Whether you're using MacOS or Windows, following the steps in this guide will help you set up pyenv and streamline your development workflow. With pyenv, you can ensure compatibility across different projects and avoid conflicts with system Python versions, making your development process smoother and more productive.
Happy coding!

