Pipenv Specify Python Version

Pipenv Specify Python Version

This article discusses a few methods to use pipenv to create virtual environments with specific python versions. We will list down the prerequisites and explain the use of the commands required by pipenv to install a particular version of Python in the virtual environment.

pipenv Specify Python Version

First, you would need to install pipenv. We can install it using pip as given below:

python3 -m pip install --user pipenv

Now we can use pipenv to create the virtualenv. It should be minded that there is a prerequisite before using this command. You should install the specific Python version you wish to have in your Virtual Environment on your working system.

For example, if you wish to create a Virtual Environment with Python 3.6, then Python 3.6 should already be installed on your system.

The easiest way to install a specific version is through the apt-get command. There is a convenient personal repository made for this exact purpose.

Please note that this is perfect for Debian-based Linux systems. For Windows, you can install the specific version you need from here.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

After this setup, if you want to install Python 3.6, you can do it with the following command:

sudo apt-get install python3.6

To install any other version of Python, replace python3.6 with whichever version you wish to install. For example, python3.4.

Now, if a new environment using Python 3.6 seems necessary, we can do that using the following command:

pipenv install --python 3.6

Within the directory that you have run this program, if you now run the command pipenv shell, you will be greeted with a shell that uses your desired Virtual Environment.

If you wish to change the version of Python used by this environment, navigate to the current directory, and open the Pipfile in a text editor. You will see a section like the following:

[requires]
python_version = "3.6"

Changing this version to anything else and then relaunching the pipenv shell will launch an environment with the new version. Please note that once again, you must install the latest version of Python on your system.