Virtual Environments I: Installing Pyenv with Homebrew

If you’ve used Python before, you may have encountered a situation known as “dependency hell”. Some packages depend on other packages to work. Sometimes, the package that another package depends on must be a certain version. Later on, you might need to use a different version of that package because there is yet another package that depends on that package being another version. So the best way to avoid dependency hell and create professional projects is to use virtual environments.

a lego toy office worker frustrated in front of a tiny computer
Image by www_slon_pics from Pixabay

I. Installing Pyenv with Homebrew

There are different virtual environment packages, but I like to use Pyenv. These instructions will be for MacOS/Linux but if you have Windows you can learn more about that here.

$ brew update
$ brew install pyenv

II. Using Bash and Nano to Create a .bash_profile

4. The file you need to access is known as .bash_profile. Notice the . at the front. When you see a dot in front of a file name, that means that the file is a hidden file.

$ ls -a
$ nano
screen shot of a nano file
screen shot of a nano file
screen shot of a nano file
echo ‘export PYENV_ROOT=”$HOME/.pyenv”’ >> ~/.bash_profile
echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”’ >> ~/.bash_profile
eval “$(pyenv init -)”
 exec $SHELL

III. Using Pip to Install Virtualenv

The next step is installing Virtualenv to use with Pyenv to manage our virtual environments and subsequent dependencies.

$ pip3 install virtualenv
$ pip3 install virtualenvwrapper
$ brew install pyenv-virtualenv
eval “$(pyenv virtualenv-init -)”

IV. What Did We Do?

1. Updated Homebrew and installed Pyenv
2. Displayed secret files in our home folder.
3. Located and/or created a .bash_profile and .bashrc file using nano.
4. Added Pyenv to our $PATH so we can use it in the command line.
5. Installed virtualenv, virtualenvwrapper, and pyenv-virtualenv.
6. Properly configured pyenv and virtual env to our shell by adding code to .bash_profile and .bashrc.

V. What’s Next?

In the Virtual Environments II, I’ll show you how to create a unique virtual environment for your project and install some of the most popular data science packages, like Jupyter Notebook and Pandas.

🐍 Python Enthusiast 🐼 Pandas Aficionado ⭐ Experience NLP using SpaCy and NLTK 🔎 Relentlessly curious 👩🏻‍💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store