Pipenv not recognizing Pyenv version?

荒凉一梦 提交于 2020-12-29 06:49:06

问题


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?


回答1:


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"



回答2:


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.




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/51201018/pipenv-not-recognizing-pyenv-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!