DEV Community

Bionic Julia
Bionic Julia

Posted on • Originally published at bionicjulia.com on

Setting up virtual environments for Python repos

I have to say that when I first came across the concept of setting up virtual environments for running Python, I found it extremely confusing. Despite installing the necessary Python versions I needed for a particular code, running pip install never seemed to work the way I felt it should intuitively, and I found the whole thing a bit hit and miss at the best of times.

After starting with a new team and Django code base this week, I finally got some help from another senior engineer about this and feel like I now understand where I've been going wrong. I thought I'd do a quick blog post on how to set up virtual environments for Python properly, mostly as a reference to myself as I'm sure I'm going to have to keep coming back to this in the future. πŸ™‚

pyenv

First things first, pyenv. This basically allows you to install isolated versions of Python on your machine. You might need say Python v3.3 for one project, but Python v3.6 for another. pyenv allows you to have multiple Python version installs in your computer, allowing you to easily switch between them. Here are some useful commands:

  • python --version - checks current version of Python.
  • pyenv install 3.8.2 - installs Python version 3.8.2.
  • pyenv global 3.8.2 - sets the global version of Python to 3.8.2.

The various versions are saved in this directory (assuming you're on MacOS): ~/.pyenv/versions/

Some helpful resources that goes deeper into pyenv:

Creating virtual environments

I came across 2 ways of creating virtual environments in Python (which led to a lot of confusion). I'd really recommend reading this SO thread to understand the differences between venv and virtualenv.

I hope I got this right... but let me know on Twitter or Instagram @bionicjulia if not!

Latest comments (0)