HOWTO · Python
Pipenv Specify Python Version
This tutorial provides a step-by-step guide on how to specify the Python version when creating a virtual environment using Pipenv. Learn effective methods to manage Python versions, ensuring compatibility and smooth project development. Whether you are starting a new project or maintaining an existing one, this guide covers everything you need to know about Pipenv and Python version management.
On this page
Pipenv can use a chosen Python interpreter when it creates a project environment, or record the project’s requirement in a Pipfile. In both cases, the requested Python interpreter must already be installed and discoverable on your system; changing a project setting does not install Python.
Prerequisites: Install Pipenv and the target Python version
Install Pipenv with pip install pipenv. On Debian-based Linux, you can instead install the distribution package with sudo apt install pipenv; install the target Python interpreter separately using a version available from your distribution or its supported Python installation method. On Windows, install Python with the Windows installer, make sure its pip command is available in PowerShell or Command Prompt, and run pip install pipenv there.
Then install the Python version your project needs. Pipenv does not install that target interpreter: it must already be installed and discoverable from your command line before you ask Pipenv to use it. A version manager can provide multiple Python versions, but Pipenv still needs to be able to locate the selected interpreter.
Choose a Python version supported by your project and its dependencies. The version 3.8 below is an example, not a recommendation for new projects. Substitute the version you have installed. If Pipenv cannot find the requested interpreter, check that installation and command-line availability before proceeding.
Select a Python version when creating a Pipenv environment
For a new project, open a terminal in the project directory and run pipenv --python 3.8, replacing 3.8 with your installed version. The --python option tells Pipenv which interpreter to use while creating the project’s virtual environment. Pipenv also records the project requirement in its Pipfile.
After creation, run pipenv shell to start a shell inside that environment. Commands run from this shell use the environment’s Python and installed packages. The command may create the environment and update project files; its exact terminal messages depend on your machine, so they are not shown here as fixed output.
Declare the Python version in the Pipfile
For an existing project, add a [requires] section to its Pipfile, or update that section if it is already present. Set python_version to the project’s required major and minor version. This declaration lets Pipenv know which Python version the project expects when it creates or synchronizes an environment.
Run this example:
pipfile = '[requires]\npython_version = "3.8"\n'
print(pipfile, end="")
[requires]
python_version = "3.8"
The Python snippet prints the literal configuration text; it does not run Pipenv or create an environment. Put that text in the project’s Pipfile, then run pipenv install from the project directory to install the declared dependencies and have Pipenv create or synchronize the environment. This command may also update Pipfile.lock; review and commit project-file changes according to your usual workflow.
Change the Python version used by an existing environment
To change an existing project’s version, first make sure the new interpreter is installed and available. Update python_version in the Pipfile’s [requires] section, then run pipenv install so Pipenv can synchronize the environment with the project declaration. Editing the file alone does not replace the interpreter in an already-created environment.
If the environment continues to use the previous interpreter, recreate it after updating the declaration: remove the project’s virtual environment with pipenv --rm, then run pipenv install again. pipenv --rm removes the environment, not the project files, but packages installed only in that environment will need to be installed again. Keep the Pipfile and lock file as the project’s record of its requirements.