Setup your Python projects like a pro with Pyenv and Virtualenv on MacOS

Authors: Ali Shahed, ChatGPT

Ali Shahed
ML Hobbyist
Published in
4 min readFeb 20, 2023

--

Python is a popular programming language used in many industries such as data science, web development, and automation. As a Python developer, it’s essential to manage your project’s dependencies and virtual environments to avoid version conflicts and ensure a consistent development environment across your team. Pyenv and virtualenv are two tools that can help you do just that.

Installing Pyenv

Before we can start using Pyenv, we need to install it on our machine. We’ll be using Homebrew, a package manager for MacOS, to install Pyenv. Open your terminal and run the following commands:

$ brew update
$ brew install pyenv

This will install the latest version of Pyenv on your machine.

Setup shell environment for Pyenv (from pyenv github)

After installing Pyenv you need to setup your shell environment to work with pyenv too. Here, we directly use the steps that are explained in pyenv github instruction page.

For bash:

Stock Bash startup files vary widely between distributions in which of them source which, under what circumstances, in what order and what additional configuration they perform. As such, the most reliable way to get Pyenv in all environments is to append Pyenv configuration commands to both .bashrc (for interactive shells) and the profile file that Bash would use (for login shells).

First, add the commands to ~/.bashrc by running the following in your terminal:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Then, if you have ~/.profile, ~/.bash_profile or ~/.bash_login, add the commands there as well. If you have none of these, add them to ~/.profile.

to add to ~/.profile:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
echo 'eval "$(pyenv init -)"' >> ~/.profile

to add to ~/.bash_profile:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

For Zsh:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.

Using Pyenv to manage Python versions

Once we have Pyenv installed, we can start using it to manage our Python versions. Pyenv allows us to install multiple Python versions and switch between them seamlessly. Let’s install Python 3.8.10 using Pyenv.

$ pyenv install 3.8.10

This will install Python 3.8.10 on your machine. You can verify that it’s installed by running the following command:

$ pyenv versions
* system (set by /Users/username/.pyenv/version)
3.8.10

The * indicates the currently active Python version. You can switch to Python 3.8.10 by running the following command:

$ pyenv global 3.8.10

This will set Python 3.8.10 as the global version for your machine. You can verify that it’s set by running the following command:

$ python --version
Python 3.8.10

Creating a virtual environment

Virtualenv is a tool that allows users to create isolated Python environments for different projects. We’ll use Pyenv together with Virtualenv to create and manage virtual environments. To install Virtualenv, run the following command:

pip install virtualenv

This command will install Virtualenv on your machine.

To create a new virtual environment, run the following command:

$ pyenv virtualenv <python_version> <virtual_env_name>

For example, to create a virtual environment called “project1” using Python 3.8.10 , run the following command:

pyenv virtualenv 3.8.10 project1

This command will activate the virtual environment. You can verify that the virtual environment is active by checking the Python version:

python --version

This command will display the Python version installed in the virtual environment.

Using Virtual Environments

Once the virtual environment is activated, you can use pip to install packages specific to that environment. For example, to install the Numpy package, run the following command:

pip install Numpy

This command will install Numpy in the virtual environment.

To deactivate the virtual environment, run the following command:

pyenv deactivate

This command will deactivate the virtual environment.

Switching between Virtual Environments

To switch between virtual environments, you can use the following command:

pyenv activate <virtual_env_name>

For example, to activate the “project1” virtual environment, run the following command:

pyenv activate project1

This command will activate the “project1” virtual environment.

Conclusion

Using Pyenv and Virtualenv together is a powerful combination for managing Python projects. Pyenv allows you to install and switch between different versions of Python, while Virtualenv allows you to create isolated environments for each of your projects. By following these steps, you can easily manage your Python projects on your Mac.

If you buy me a coffee, I can work longer hours and create more content like this. Thank you!

--

--

Ali Shahed
ML Hobbyist

PhD EE | Data Scientist | Machine Learning Engineer