Python setup using pyenv
Tags: python, pyenv, developer, tech
Published: 2023-04-28 11:25:00

Setting up Python on your local machine can be a challenging task, especially when you have multiple Python versions to work with. Sometimes, the python version needs to match in order to reproduce an issue on an environment but you don’t have it. This is where pyenv comes in handy. In this blog post, we will discuss how to set up Python using pyenv.

# What is Pyenv?

Pyenv is a simple tool that allows you to install and manage multiple versions of Python on your local machine. It enables you to switch between different Python versions with ease. Pyenv provides a clean and isolated environment for each Python version you install, ensuring that your Python projects are not affected by changes made in other versions.

# Installing Pyenv

Before you can start using pyenv, it would be better to delete your existing pip (if any). If you have a pip that isn’t managed by pyenv then that may interfere down the line, even if pyenv uses shims. This is just a precaution and may have been tied to my env.

The installation process depends on your operating system. On Linux, download dependencies for pyenv to work:

```sh
apt 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\
git
```

Then clone pyenv

```sh
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
```

And the command installs pyenv to the ~/.pyenv directory on your system. To setup the profile:

```sh
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
```

You may relaunch your terminal for the changes to take effect.

On Mac, you can install pyenv using the following command:

```sh
$ brew install pyenv
```

This command installs pyenv using the homebrew package manager.

Once you have installed pyenv, you can start using it to manage your Python installations.

# Installing Python Versions

To install a specific version of Python using pyenv, you can use the install command followed by the version number. For example, to install Python 3.9.6, you can use the following command:

```sh
$ pyenv install 3.9.6
```

This command downloads and installs Python 3.9.6 to the ~/.pyenv/versions directory.

You can list all the Python versions installed on your machine using the versions command:

```sh
$ pyenv versions
  system
>>> * 3.7.10 (set by /Users/user/.pyenv/version)
>>> * 3.9.6 (set by /Users/user/.pyenv/version)
$ python3.9 --version
>>> Python 3.9.6
$ python3.7 --version
>>> Python 3.7.10
$ python --version
>>> Python 3.7.10
```

# Setting the Global Python Version

To set the global Python version to be used by all your projects, you can use the global command followed by the version number. For example, to set Python 3.9.2 as the global version, you can use the following command:

```sh
$ pyenv global 3.9.2
```

This command sets Python 3.9.2 as the global version, and all new shells will use this version by default.

# Setting the Local Python Version

To set the Python version for a specific project, you can use the local command followed by the version number. For example, if you want to use Python 3.7.3 for a particular project, you can use the following command:

```sh
$ pyenv local 3.7.3
```

This command creates a .python-version file in the current directory, which specifies the Python version to use for the project. Note that you will need to install dependencies for each python version (e.g. switch versions using pyenv and repeat the below)

# Conclusion

Pyenv is a powerful tool that makes it easy to manage multiple Python versions on your local machine. It provides a clean and isolated environment for each Python version, ensuring that your projects are not affected by changes made in other versions. With pyenv, you can easily switch between different Python versions and set the global and local versions for your projects.