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
- Open Terminal.
- Run the following script:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - Installation is complete when
Installation Successfulis 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
Install
pyenvusing Homebrew:brew install pyenvSet environment variables and load the
pyenvinitialization 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 runsource ~/.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
Search for Anaconda versions available for installation through
pyenv:pyenv install -l | grep anacondaExample output:
anaconda-1.4.0 ... anaconda3-5.3.1 ...The
grepcommand filters lines containing “anaconda” from the output ofpyenv install -lpiped through|.Install the Anaconda version corresponding to the latest Python 3 (e.g.,
anaconda3-5.3.1):pyenv install anaconda3-5.3.1Installation may take some time.
Set the installed Anaconda environment to be used globally (system-wide):
pyenv global anaconda3-5.3.1This 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.1in that directory.
With these steps, you have a development environment that integrates Python version management through pyenv with Anaconda’s rich library collection.