Setting up a Python environment in 2020
Tags: python, tutorial, vscode
Published: 2020-06-10 05:53:35

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][xkcd-1987]. In this post, we'll discuss how to _get it right_™.

- [What's the winning recipe?](#whats-the-winning-recipe)
  - [Manage Python versions with PyEnv](#manage-python-versions-with-pyenv)
    - [Useful commands](#useful-commands)
    - [How can I know what versions are available in PyEnv?](#how-can-i-know-what-versions-are-available-in-pyenv)
  - [Managing projects with Pipenv](#managing-projects-with-pipenv)
  - [Using VS Code as a text editor](#using-vs-code-as-a-text-editor)
  - [VS Code and Pipenv](#vs-code-and-pipenv)
- [Installation](#installation)
  - [MacOS](#macos)
  - [Ubuntu](#ubuntu)
  - [Windows](#windows)
  - [Setting up VS Code](#setting-up-vs-code)
- [Creating your first project](#creating-your-first-project)
- [Do you want to know more?](#do-you-want-to-know-more)

# 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][gh-pyenv], [pipenv][gh-pipenv] and [VS Code][vs-code].

## Manage Python versions with PyEnv

[PyEnv][gh-pyenv] allows you to have several Python versions installed and easily switch between them. The features' documentation is available on their [GitHub][gh-pyenv]. 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][git-scm] 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][gh-pipenv] automatically creates and manages [virtual environments][virtual-env] 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][vs-code] is becoming the _de facto_ standard text editor. It works well with [PyEnv][gh-pyenv] and [Pipenv][gh-pipenv]. 

## VS Code and Pipenv

When opening a project that uses [Pipenv][gh-pipenv], we should configure the Python interpreter used by [VS Code][vs-code]:

{% youtube Uxg6xR6Qb_o %}


# Installation

This section will show how to install [PyEnv][gh-pyenv], different Python versions, and [Pipenv][gh-pipenv].

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

## MacOS

On MacOS, the easiest way to install [PyEnv][gh-pyenv] is through [Homebrew][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:

```bash
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][gh-pyenv-installer] page:

```bash
curl https://pyenv.run | bash
```

## Windows

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

+ Use [Windows Linux Subsystem for Windows 10][wsl] and to follow the same steps as the Ubuntu installation; or
+ Use a Ubuntu virtual-machine for development and follow the above steps

## Setting up VS Code

The first thing we need is to install [Python extension for VS Code][python-vs-code]. Then we'll be able to run Python files:

![Run on VS Code](https://dev-to-uploads.s3.amazonaws.com/i/pyffxwhakwwgrdndlos0.png)

If you are inside a project and running inside a [virtual environment](virtual-env).

# 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][json-placeholder-api]:

```bash
# 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:

{% replit @joaoqalves/ForsakenGrowingDonateware %} 

To run it on your computer, you can do:

```bash
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][python-course]!

[homebrew]: https://brew.sh/
[gh-pipenv]: https://github.com/pypa/pipenv
[gh-pyenv]: https://github.com/pyenv/pyenv
[gh-pyenv-installer]: https://github.com/pyenv/pyenv-installer
[gh-pyenv-win]: https://github.com/pyenv-win/pyenv-win
[git-scm]: https://git-scm.com/
[json-placeholder-api]: https://jsonplaceholder.typicode.com/
[python-course]: https://pythoncourse.dev/
[python-vs-code]: https://marketplace.visualstudio.com/items?itemName=ms-python.python
[virtual-env]: https://docs.python.org/3/tutorial/venv.html
[vs-code]: https://code.visualstudio.com/
[wsl]: https://docs.microsoft.com/en-us/windows/wsl/install-win10
[xkcd-1987]: https://xkcd.com/1987/