Keshav Bhatiya
Keshav Bhatiya

Reputation: 375

Why is pipenv telling me the wrong version of python?

I created a virtual environment using:

mkvirtualenv env_name --python=python3.9

Then I tried to install from Pipfile:

pipenv install --dev

But I got the following error:

pipenv.patched.notpip._internal.exceptions.UnsupportedPythonVersion: pylint requires Python '~=3.6' but the running Python is 2.7.17

I checked my python version and it is correct:

$ python --version
Python 3.9.1

Why is it telling me that my python version is 2.7.17? This is not happening when I install using the following command:

pip install pylint==2.7.2

Upvotes: 0

Views: 11289

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29546

First, let's clarify your workflow for using pipenv to manage virtual environments.

With pipenv, you don't need to separately create your virtual environment with mkvirtualenv or with whatever virtualenv utility you have. The basic flow is to simply do pipenv install:

Example Pipenv Workflow

Clone / create project repository:

$ cd myproject

Install from Pipfile, if there is one:

$ pipenv install

Or, add a package to your new project:

$ pipenv install <package>

This will create a Pipfile if one doesn’t exist. If one does exist, it will automatically be edited with the new package you provided.

Next, activate the Pipenv shell:

$ pipenv shell 
$ python --version

This will spawn a new shell subprocess, which can be deactivated by using exit.

That should create the virtual environment and install your dependencies in one command. The auto-created virtual env folder is based on the path of the current directory where you ran the command:

  • The virtualenv is stored globally with the name of the project’s root directory plus the hash of the full path to the project’s root (e.g., my_project-a3de50).
  • If you change your project’s path, you break such a default mapping and pipenv will no longer be able to find and to use the project’s virtualenv.

I'm guessing that the reason why you do a mkvirtualenv separately is to control where the virtual env folder is stored/managed. pipenv provides options for that as well:

Custom Virtual Environment Location

Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:

$ export WORKON_HOME=~/.venvs

In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.

From what you tried with

$ mkvirtualenv env_name --python=python3.9

The mkvirtualenv reference says that means "create an env_name in the WORKON_HOME folder". When you did pipenv install, it's NOT going to use that same virtual env directory, because as described above, it auto-generates the virtual env directory based on the "name of the project’s root directory plus the hash of the full path to the project’s root". So whatever happened in env_name is ignored.

With pipenv you simply have to set the WORKON_HOME folder then pipenv install should create your virtual env under that same folder. Every time you need to activate it, you cd into your project directory, then do pipenv shell.

Now, for the Python version problem. By default, when you do pipenv install, it uses whatever is listed as the python_version on your Pipfile, such as this:

[requires]
python_version = "3.8"

It scans your system and looks for a compatible python installation. Now, it seems you already have an existing Pipfile that has "2.7" on it. Or it could be that there's already an existing virtual environment (created by a previous pipenv install call and Python 2.7 was used). Whatever the case, even though you mkvirtualenv-ed an env_name with Python 3.9, that one is ignored as I already mentioned, and pipenv still used the Python 2.7 version, which resulted in that error:

pipenv.patched.notpip._internal.exceptions.UnsupportedPythonVersion:  ...

To fix this, start over by deleting any pre-existing virtual environments by using --rm:

$ cd yourproject
$ pipenv --rm
Removing virtualenv (/path/to/.venvs/myproject-wv1hzz4Y)...

Then check that your existing Pipfile has the correct version set in python_version.

[requires]
python_version = "3.9"

Normally, you don't have to manually set the version, if you originally used pipenv install correctly. To specify the correct version, you need to pass in a --python option, similar to what you did with mkvirtualenv:

$ python3 -V
Python 3.9.2

$ which python3
/usr/local/bin/python3
   
$ pipenv install --python=/usr/local/bin/python3 
Creating a virtualenv for this project...
...
Using /usr/local/bin/python3 (3.9.2) to create virtualenv...

$ pipenv shell
$ python -V
Python 3.9.2

If there is no Pipfile yet, pipenv will create one and set python_version correctly.

Upvotes: 8

Related Questions