Exploring Python Environments with PyEnv
Tags: python, tutorial, tooling, programming
Published: 2023-12-11 01:14:35


## Introduction

Python developers often encounter scenarios where they must manage multiple Python versions and virtual environments. This is where `pyenv` comes to the rescue. `pyenv` is a simple yet powerful tool that allows you to easily install, manage, and switch between different Python versions on your system.

In this blog post, we'll explore the fundamental operations of `pyenv`, including installation, creating and managing environments, and utilizing the virtualenv plugin for more advanced use cases.

## Installing `pyenv`

Before diving into `pyenv`, let's install it on your machine. The process is straightforward:

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

# Add the following to your shell profile (.bashrc, .zshrc, etc.)
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init --path)"
  eval "$(pyenv virtualenv-init -)"
fi
```

## Basic Operations

### Listing Available Python Versions

To see which Python versions are available for installation, use:

```bash
pyenv install --list
```

### Installing a Python Version

Install a specific Python version with:

```bash
pyenv install 3.8.12
```

### Setting the Global Python Version

Set a global Python version for your system:

```bash
pyenv global 3.8.12
```

### Creating a Virtual Environment

Create a virtual environment for your project:

```bash
pyenv virtualenv 3.8.12 myenv
```

### Activating and Deactivating Environments

Activate an environment:

```bash
pyenv activate myenv
```

Deactivate the current environment:

```bash
pyenv deactivate
```

## Advanced Usage with the Virtualenv Plugin

`pyenv` can be extended with plugins. One such helpful plugin is `pyenv-virtualenv`. Install it via:

```bash
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
```

### Creating Virtual Environments with the Plugin

```bash
pyenv virtualenv 3.8.12 myenv
```

### Activating and Deactivating Environments with the Plugin

```bash
pyenv activate myenv
```

```bash
pyenv deactivate
```

### Listing Virtual Environments

```bash
pyenv virtualenvs
```

## Understanding `pyenv local`, `pyenv global`, and `pyenv shell`

When working with `pyenv`, you have three main commands for managing Python versions on different levels: `pyenv local`, `pyenv global`, and `pyenv shell`.

### 1. `pyenv local`

The `pyenv local` command allows you to set a local Python version for a specific project or directory. This is particularly useful when different projects require different Python versions. When you run `pyenv local`, a file named `.python-version` is created inside the project directory, specifying the desired Python version.

**Usage:**

```bash
cd your_project_directory
pyenv local 3.8.12
```

This creates a `.python-version` file in your project directory containing the version information.

Now, whenever you navigate this project directory or its subdirectories, `pyenv` will automatically switch to the specified Python version. This local configuration is project-specific, making it convenient to manage dependencies per-project basis.

### 2. `pyenv global`

On the other hand, `pyenv global` sets the global Python version for your entire system. This default Python version will be used without a local or shell-specific configuration.

**Usage:**

```bash
pyenv global 3.9.5
```

This command sets the global Python version to 3.9.5. It's handy when you want to define a default Python version for your entire development environment.

### 3. `pyenv shell`

The `pyenv shell` command sets a Python version for the current shell session. Unlike `pyenv local`, it does not create a configuration file. Instead, it affects only the current shell session.

**Usage:**

```bash
pyenv shell 3.7.9
```

This command sets the Python version for the current shell session to 3.7.9. It's useful for temporary changes within a terminal window.

### Focus on `pyenv local`

The real strength of `pyenv local` lies in its ability to create a project-specific configuration. By generating a `.python-version` file within your project directory, `pyenv` ensures that the correct Python version is automatically activated every time you enter that directory.

This feature is especially valuable in collaborative projects or when working on multiple projects with different Python requirements. It promotes consistency and avoids conflicts by encapsulating the Python version within the project's directory.

Next time you embark on a new project, consider using `pyenv local` to manage Python versions effortlessly on a per-project basis.

# Conclusion

In conclusion, pyenv stands out as a straightforward and efficient tool for managing Python versions and virtual environments, providing simplicity for Python-centric projects with commands like pyenv local, pyenv global, and pyenv shell. Notably, the elegance of pyenv local lies in its ability to create project-specific configurations, facilitating seamless transitions between diverse project environments. However, as the complexities of projects grow, the environment management landscape expands to encompass broader needs.

# Resources
* https://github.com/pyenv/pyenv
* https://github.com/pyenv/pyenv-virtualenv
* [Cover image](https://live.staticflickr.com/1214/4607201747_2c5fa9f78b_b.jpg)