Setting Up a Python Development Environment with Pyenv and Anaconda

A step-by-step guide to building a Python development environment using Homebrew, pyenv, and Anaconda on macOS for version management and data science.

In Python development, managing different Python versions and libraries for each project is very important. This article explains how to build a flexible and efficient Python development environment by combining pyenv and Anaconda.

1. Installing Homebrew

What is Homebrew?

Homebrew is a package manager for macOS (and Linux). It allows you to easily install, update, and uninstall software from the command line.

Installation Method

  1. Open Terminal.
  2. Run the following script:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  3. Installation is complete when Installation Successful is displayed.

Basic Usage

  • brew update: Update Homebrew’s package list.
  • brew upgrade: Upgrade installed packages to the latest version.
  • brew list: Display a list of installed packages.
  • brew install [package name]: Install the specified package.
  • brew uninstall [package name]: Uninstall the specified package.
  • brew info [package name]: Display detailed information about the specified package.

2. Installing pyenv

What is pyenv?

pyenv is a Python version management tool that allows you to install multiple Python versions on your system and easily switch between them on a per-project or per-directory basis. This avoids version conflicts between different projects.

Installation Method

  1. Install pyenv using Homebrew:

    brew install pyenv
    
  2. Set environment variables and load the pyenv initialization script into your shell:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
    echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile # Adds pyenv's executable path to environment variables.
    echo 'eval "$(pyenv init -)"' >> ~/.bash_profile # Runs the initialization script to enable pyenv commands.
    
    • ~/.bash_profile: A configuration file automatically loaded when the Bash shell starts.
    • export PATH="...": Adds pyenv’s binary directory to the executable search path.
    • eval "$(pyenv init -)": Runs the initialization script to enable pyenv commands.

    Note: If you are using Zsh, add these to ~/.zshrc; for Fish, add them to ~/.config/fish/config.fish. To apply the changes, restart your terminal or run source ~/.bash_profile (or the applicable configuration file).

3. Installing Anaconda

What is Anaconda?

Anaconda is a Python distribution specialized for data science and machine learning. In addition to Python itself, many scientific computing libraries and tools such as NumPy, pandas, scikit-learn, and Jupyter Notebook are pre-packaged, significantly reducing the effort needed for environment setup.

Installation Method

  1. Search for Anaconda versions available for installation through pyenv:

    pyenv install -l | grep anaconda
    

    Example output:

    anaconda-1.4.0
    ...
    anaconda3-5.3.1
    ...
    

    The grep command filters lines containing “anaconda” from the output of pyenv install -l piped through |.

  2. Install the Anaconda version corresponding to the latest Python 3 (e.g., anaconda3-5.3.1):

    pyenv install anaconda3-5.3.1
    

    Installation may take some time.

  3. Set the installed Anaconda environment to be used globally (system-wide):

    pyenv global anaconda3-5.3.1
    

    This makes the Anaconda environment the default when opening a terminal. If you want to use it only in a specific project, run pyenv local anaconda3-5.3.1 in that directory.

With these steps, you have a development environment that integrates Python version management through pyenv with Anaconda’s rich library collection.