DEV Community

Cover image for Real World Python 🐍: Environment Setup - pyenv
Upamanyu Das
Upamanyu Das

Posted on

Real World Python 🐍: Environment Setup - pyenv

You should never ever touch system python.

What is system python?

System python is the python that comes installed with your operating system.

Why shouldn't I touch it?

There are a host of reasons to not use system python.

  • Not the version of python you're looking for. Maybe you want the latest features from python 3.9 but your OS only came with 3.8.
  • Globally installing dependencies is never a good idea and problems with multiple versions of a dependency will have you troubleshooting on a Sunday morning.
  • Your OS might have a system dependency on python which might make your entire system unusable because you managed to break python.

pyenv

Simple Python version management.

This tool lets us install our preferred python version and use that for our projects so that we don't have to touch system python.

GitHub logo pyenv / pyenv

Simple Python version management

Installing pyenv

Build dependencies

pyenv builds python from source, so you will need some build dependencies to use pyenv. These dependencies vary by platform.

Mac

brew install openssl readline sqlite3 xz zlib
Enter fullscreen mode Exit fullscreen mode

Note: Assumes you have brew installed on your machine.

Debian

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
Enter fullscreen mode Exit fullscreen mode

For other systems please refer to the official installation docs.

pyenv

curl https://pyenv.run | bash
Enter fullscreen mode Exit fullscreen mode

This uses the pyenv-installer project to install pyenv on your system.

Installing a python version

Check what versions are available:

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Install a python version

pyenv install -v 3.9.5
Enter fullscreen mode Exit fullscreen mode

Setting python version

pyenv lets us set the python version globally, locally in our project and even for a particular shell.

Pyenv pyramid - System python at the bottom, above it global python, above it local python and at the top shell python

We want to set the python version for our project so we will use local.

We will create our project directory and navigate into it.

mkdir quotes-scraper && cd quotes-scraper
Enter fullscreen mode Exit fullscreen mode

And then set the python version we want to use for our project.

pyenv local 3.9.5
Enter fullscreen mode Exit fullscreen mode

This will create a .python-version file in the root directory of the project. If pyenv is active in our environment, this file will automatically activate the specified python version in the project.


I hope you found this post useful. Please leave any feedback in the comments or DM me @tintin_das on Twitter 🐦.

Top comments (0)