Pragmatic way of installing multiple versions of python with virtual environments
Tags: python, tutorial, beginners, productivity
Published: 2023-08-13 10:31:44

This guide shows you a way to install multiple python versions on Mac using pyenv and to control virtual environments with pipenv.

## Installing multiple versions of python

To control python versions I use [pyenv](https://github.com/pyenv/pyenv). I installed it using:

`brew install pyenv` 

Afterward, I added the environment variables as described in [pyenv GitHub](https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv)

You can set versions of python with pyenv with:

```
pyenv install 3.11.4 # Use this option for Mac Intel
arch -x86_64 pyenv install 3.11.4 # Use this option for Mac M1
```

You can set the python for a project with:

`pyenv local 3.11.4` 

which will create a file in your project called .python-version

Or globally with:

`pyenv global 3.11.4`

You can check versions with:

`pyenv versions`

You can also check python version with:

`python --version`

Binaries and python versions are usually stored in ~/.pyenv

## Creating virtual environments

To manage python dependencies and virtual environments I make use of pipenv

Install pipenv:

`pip install pipenv`

Inside your project folder you can create a virtual environment and install your packages with:

`pipenv install <a_package>`

When you run this command it will install your libraries based on the python version you set in your pyenv.

Since you may have several python versions on your machine, I advise you to create a virtual environment that always specifies the python version with:

`pipenv --python 3.11`

Now with your environment created, no matter what python version is set to be your global, you can do the below command and it will automatically launch the latest created environment

`pipenv shell`

Once you run the command above you will be inside a virtual environment in your terminal. To install a package you can do

`pipenv install yamllint`

This will create a _Pipfile_ and a _Pipfile.lock_. The first will show the packages that you are installing and the lock file shows the packages' version hashes and their dependencies as well.

If you would like to know more about pipenv and dependency resolution I advise you to read [this complete guide](https://realpython.com/pipenv-guide/) about it.

## Integrating with VSCode

Now that you have all installed via terminal you might want to integrate all that with VSCode.

Install [Python VSCode](https://marketplace.visualstudio.com/items?itemName=ms-python.python) and it should work, right? Well, not everything.


Even though you loaded your virtual environment in the terminal, VSCode might not detect it and you will not be able to use the VSCode debugger

To fix it click on item 1 from the image below and choose the virtual environment you created before. Once you click you should see the virtual environment reference in your VS Code bar.

If your virtual environment is not showing up, make sure you created one by going to your project terminal and running pipenv shell. If you still cannot select it afterward, you need to tell VSCode where your environments are by going to VSCode Settings > Looking for venv > Venv Folders and add there the path of your environments. In my case that is _~/.local/share/virtualenvs/_


![VSCode image with instructions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29q3vrsoxr3buffehri6.png)



## Adding environment and python version to your terminal

You can also have information about your virtual environment displayed in your terminal together with your python version like item 3 from the image above

For that, you will need to install https://ohmyz.sh and add steroids to it with https://github.com/romkatv/powerlevel10k

PowerLevel10k might seem intimidating at first but there's a wizard where you can choose exactly how your terminal should be.