Pipenv 不识别 Pyenv 版本?

Pipenv not recognizing Pyenv version?

我安装了 Python 3.7.0,但对于特定的 Django 项目,我想使用 Python 3.6.5。为此目的使用 pyenv,在我的 Macbook Pro 上,我 运行 brew install pyenv,然后是 pyenv install 3.6.5,在项目的根目录中,pyenv local 3.6.5。我已验证 Python 版本 3.6.5 处于活动状态:

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)

我正在使用的Pipfile类似于以下内容:

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

[packages]

[dev-packages]

[requires]
python_version = "3.6.5"

然而,当我运行pipenv shell时,我得到它'defaults'到我的系统版本,python3.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$

现在,如果我尝试 运行 python manage.py shell 到 运行 Django 项目的 shell,我会得到一个 SyntaxError,我怀疑它与Python 3.7,因为我确定这在之前有效:

(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

然而,我认为其根本原因是 运行 在 Python 3.7.0 中,而不是在 Python 3.6.5 中。

pipenvpyenv 不是 'compatible' 吗?

Pipenv 知道 Pyenv,但它不会自动使用相同的 Python 版本,除非您告诉它这样做。 Pipenv docs.

中有关于此的注释

您可以告诉 Pipenv 使用特定的 Python 版本,例如

pipenv install --python 3.6.5

或者你可以设置一个环境变量默认为Pyenv版本,比如

export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

我注意到将我的系统范围 Python 从 3.7.0 降级到 3.6.5 后仍然出现相同错误的问题。一旦 pipenv 创建了一个 virtualenv,它不会根据你当前的 pyenv 版本改变它,但是如果你删除 virtualenv 并创建一个新的,它将 'pick up' 正确的版本.

使用 pyenv install 3.6.5

安装 python 3.6.5

将新安装的 python 版本导出到 PATH

export PATH=${PYENV_PYTHON_VERSIONS_HOME}/3.6.5/bin

现在在 'Piplock' 中指定相同的版本。

[requires] python_version = "3.6.5"

最后,运行删除之前的 virtualenv 并重新构建。

pipenv --rm

pipenv install --dev.

就我而言,在 MacOS 上。我这样安装了 python 3.6.5:

使用 pyenv 安装特定的 python 版本:

pyenv install 3.6.5

使用 pipenv--python 参数以及 python 版本的位置创建环境:

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

如果您遇到与 _sqlite3 有关的问题,您可以查看此 pyenv ticket 以获取解决方案。

使用pipenv run在创建的环境中执行命令:

pipenv run python manage.py shell

关于如何设置 .bashrc 有几个不同的指导(甚至在 pyenv 文档中)。这个对我有用

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"