Pipenv无法识别Pyenv版本?

12

我已经安装了Python 3.7.0版本,但是针对一个特定的Django项目,我想使用Python 3.6.5版本。为此目的,我在我的Macbook Pro上使用pyenv,运行了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时,它会“默认”使用我的系统版本,即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$

现在,如果我尝试运行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之间是否不“兼容”?

5个回答

21

Pipenv可以识别Pyenv,但如果您不告诉它使用相同的Python版本,它就不会自动使用。在Pipenv文档中有相关说明。

您可以告诉Pipenv使用特定的Python版本:

pipenv install --python 3.6.5

或者您可以设置一个环境变量来默认使用 Pyenv 版本,例如:

export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

请注意,您对PIPEVN_PYTHON的使用方式与其预期不符。请参阅此评论 - smac89

4

在将我的系统范围内的Python从3.7.0降级到3.6.5并仍然遇到同样的错误之后,我注意到了问题所在。一旦pipenv创建了一个虚拟环境,它不会根据您当前的pyenv版本更改它,但如果您删除虚拟环境并创建一个新的虚拟环境,它将“拾取”正确的版本。


考虑到原始问题,这是正确的答案。使用给定的 Pipfile 重新创建 virtualenv 将解决问题: pipenv --rm && pipenv install - ThisIsFlorianK

2

在我的情况下,我在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

1

在如何设置 .bashrc 文件方面,有几个不同的方向(即使在 pyenv 文档中也是如此)。这个方法对我有效。

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

-1

使用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"

最后,删除以前的虚拟环境并重新构建。

pipenv --rm

pipenv install --dev


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接