Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipenv not recognizing Pyenv version?

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?

like image 920
Kurt Peek Avatar asked Jul 05 '18 23:07

Kurt Peek


3 Answers

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"
like image 199
evergreen Avatar answered Sep 18 '22 11:09

evergreen


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
like image 37
andrei13 Avatar answered Sep 19 '22 11:09

andrei13


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.

like image 42
Kurt Peek Avatar answered Sep 21 '22 11:09

Kurt Peek