DEV Community

Cover image for Setting up a Python environment in 2020
Python Course
Python Course

Posted on • Updated on

Setting up a Python environment in 2020

It's 2020, and you want to write some Python. Some operating systems, such as MacOS and Ubuntu, come with a pre-installed version. However, sometimes it gets messy. In this post, we'll discuss how to get it right™.

What's the winning recipe?

After more than ten years of writing Python, I settled on three main tools to support me in my daily endeavors: PyEnv, pipenv and VS Code.

Manage Python versions with PyEnv

PyEnv allows you to have several Python versions installed and easily switch between them. The features' documentation is available on their GitHub. Below, I leave the commands I mostly use.

Useful commands

We can list all the available Python versions with:

pyenv versions

We'll see the following output in our terminal:

  system
  3.6.10
* 3.8-dev (set by /Users/joaoqalves/.pyenv/version)

To change the global Python version we need to run:

pyenv global 3.8.2

If we are working in a ~/projects/playground project, we can also set the version for this particular folder using:

pyenv local 3.6.3

The above command will create a .python-version file in the directory, as mentioned earlier. You can check-in this file into your git to have it across different environments.

How can I know what versions are available in PyEnv?

Run the following command:

pyenv install --list

Managing projects with Pipenv

Pipenv automatically creates and manages virtual environments for your projects. It uses a Pipfile to declare all the necessary dependencies and a Pipfile.lock to improve run reproducibility.

Using VS Code as a text editor

There's not much to say here. VS Code is becoming the de facto standard text editor. It works well with PyEnv and Pipenv.

VS Code and Pipenv

When opening a project that uses Pipenv, we should configure the Python interpreter used by VS Code:

Installation

This section will show how to install PyEnv, different Python versions, and Pipenv.

NOTE: For every Python version, you'll need to set-up pipenv accordingly.

MacOS

On MacOS, the easiest way to install PyEnv is through Homebrew.

brew install pyenv && pyenv install 3.8.2
pyenv global 3.8.2
pip install pipenv

Ubuntu

First of all, we'll need to install the dependencies:

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

Then we'll need to run the installer script, available on PyEnv installer page:

curl https://pyenv.run | bash

Windows

Unfortunately, PyEnv does not support Windows out-of-the-box. You may consider using pyenv-win fork. Other options are:

Setting up VS Code

The first thing we need is to install Python extension for VS Code. Then we'll be able to run Python files:

Run on VS Code

If you are inside a project and running inside a virtual environment.

Creating your first project

Now that we have all the tools set up, we can start creating our first project. We'll create it in a new directory and tell pipenv that we want a new project using Python 3.8.2 that will make an HTTP request to JSON Placeholder's API:

# New directory
mkdir pyplayground 
cd pyplayground
# Change the Python version we want to use
pyenv local 3.8.2 
# Create a new virtual environment for our dependencies
pipenv shell
# Install requests in the current virtual environment
pipenv install requests

Then we'll create a file named placeholder.py with the following code:

To run it on your computer, you can do:

python placeholder.py

To run it on VS Code, click on the run button on the top-right corner, as mentioned above.

Do you want to know more?

I'm creating a step-by-step video course for Python. You can subscribe here!

Top comments (0)