Pipenv not recognizing Pyenv version?
Asked Answered
A

5

12

I have Python 3.7.0 installed, but for a specific Django project, I would like to use Python 3.6.5. Using pyenv for this purpose, on my Macbook Pro I ran brew install pyenv, followed by pyenv install 3.6.5 and, in the project's root directory, pyenv local 3.6.5. I've verified that Python version 3.6.5 is active:

Kurts-MacBook-Pro-2:lucy-web kurtpeek$ cat .python-version
3.6.5
Kurts-MacBook-Pro-2:lucy-web kurtpeek$ pyenv versions
  system
* 3.6.5 (set by /Users/kurtpeek/Documents/dev/lucy2/lucy-web/.python-version)

The Pipfile I'm using is similar to the following:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.6.5"

However, when I run pipenv shell, I get that it 'defaults' to my system version, python 3.7.0:

Kurts-MacBook-Pro-2:lucy-web kurtpeek$ pipenv shell
Loading .env environment variables...
Warning: Your Pipfile requires python_version 3.6.5, but you are using 3.7.0 (/Users/k/.local/share/v/l/bin/python).
  $ pipenv check will surely fail.
Launching subshell in virtual environment…
bash-3.2$  . /Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/bin/activate
(lucy-web-CVxkrCFK) bash-3.2$

Now, if I try to run python manage.py shell to run the Django project's shell, I get a SyntaxError which I suspect is germane to Python 3.7, since I'm sure this was working before:

(lucy-web-CVxkrCFK) bash-3.2$ python manage.py shell
Traceback (most recent call last):
  File "manage.py", line 28, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/apps/registry.py", line 116, in populate
    app_config.ready()
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/two_factor/apps.py", line 10, in ready
    from .admin import patch_admin
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/two_factor/admin.py", line 2, in <module>
    from django.contrib import admin
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in <module>
    from django.contrib.admin.filters import (
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in <module>
    from django.contrib.admin.options import IncorrectLookupParameters
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/contrib/admin/options.py", line 12, in <module>
    from django.contrib.admin import helpers, widgets
  File "/Users/kurtpeek/.local/share/virtualenvs/lucy-web-CVxkrCFK/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 151
    '%s=%s' % (k, v) for k, v in params.items(),
    ^
SyntaxError: Generator expression must be parenthesized

The root cause of this, however, I believe is that it is being run in Python 3.7.0 and not in Python 3.6.5 as desired.

Are pipenv and pyenv not 'compatible' with each other?

Assembly answered 5/7, 2018 at 23:11 Comment(0)
Q
21

Pipenv is aware of Pyenv, but it doesn't automatically use the same Python version unless you tell it to do that. There is a note about this in the Pipenv docs.

You can either tell Pipenv to use a specific Python version, like

pipenv install --python 3.6.5

or you can set an environment variable to default to the Pyenv version, like

export PIPENV_PYTHON="$PYENV_ROOT/shims/python"
Quartzite answered 6/7, 2018 at 11:40 Comment(1)
Note that your use of PIPEVN_PYTHON is not how it was intended. See this commentHaukom
A
4

I noticed what the problem was after downgrading my system-wide Python from 3.7.0 to 3.6.5 and still getting the same error. Once pipenv has created a virtualenv, it won't change it according to your current pyenv version, but if you delete the virtualenv and create a new one, it will 'pick up' the correct version.

Assembly answered 6/7, 2018 at 18:32 Comment(1)
Considering the original question, this is the correct answer. With the given Pipfile re-creating the virtualenv would solve the problem: pipenv --rm && pipenv installWinnifredwinning
G
2

In my case, on MacOS. I installed python 3.6.5 this way:

Install a specific python version using pyenv:

pyenv install 3.6.5

Create an environment using pipenv with the --python parameter along with the location of the python version:

pipenv --python /Users/<<Your_User>>/.pyenv/versions/3.6.5/bin/python3.6

If ever you encounter issues relating to _sqlite3, you can check this pyenv ticket for the solution.

Use pipenv run to execute commands inside the created environment:

pipenv run python manage.py shell
Grider answered 15/3, 2019 at 17:7 Comment(0)
C
1

There are several different directions in how to setup the .bashrc (even in the pyenv documentation). This one worked for me

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Correctitude answered 10/11, 2021 at 0:37 Comment(0)
K
-1

Install python 3.6.5 using pyenv install 3.6.5

Export new installed python version to PATH

export PATH=${PYENV_PYTHON_VERSIONS_HOME}/3.6.5/bin

Now in 'Piplock' specify the same version.

[requires] python_version = "3.6.5"

Finally, run remove previous virtualenv and rebuild again.

pipenv --rm

pipenv install --dev.

Kabul answered 11/12, 2018 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.