TIL: Simplified(?) Python Development Environment with pyenv & pipenv

Creating & maintaining a stable local Python development environment has often felt like more trouble than it ought to be, especially when you haven’t created one in a while but need to. The main goals/challenges of creating one are:

  • Installing the desired version of Python with the least amount of trouble.
  • Installing any necessary Python packages for the project in the right place.
  • Maintaining the environment configuration as a part of the project.

While it would be ideal to have a single tool that will do all of this, I will admit that I have long since become accustomed to using pipenv. However, pipenv‘s weakness is that it doesn’t have the ability to install different versions of python–it is just a (very, very good) package manager. Meanwhile, pyenv just happens to be a very good Python installer that lacks the ability to create & manage virtual environments.

Installing pyenv & pipenv on Mac OSX

It’s as easy as this:

brew install pyenv pipenv

Installing Python with pyenv

In the directory of your new project, you only need to run 2 commands:

pyenv install 3.10.4
pyenv local 3.10.4

This will install Python 3.10.4 then it creates a simple .python-version file in the current directory (which should be the root directory of your project).

Creating the Virtual Environment with pipenv

Again, just 2 simple commands:

pipenv --python 3.10
pipenv install

The first command will install the most recent version of 3.10 that is installed, however, to be sure you can specify the minor release version (3.10.4) if you feel the need. The second command assumes that your project already has a Pipfile in it & will install of the packages found in that file.

That’s it! The only thing left is to pipenv shell to shell into your new virtual environment & get to work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.