[英]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
,我发现它“默认”为我的系统版本 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 中运行。
pipenv
和pyenv
不“兼容”吗?
Pipenv 知道 Pyenv,但它不会自动使用相同的 Python 版本,除非您告诉它这样做。 Pipenv 文档中有关于此的说明。
您可以告诉 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 并创建一个新的virtualenv,它将“选择”正确的版本。
如何设置 .bashrc 有几个不同的方向(甚至在 pyenv 文档中)。 这个对我有用
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
使用pyenv install 3.6.5
安装 python pyenv install 3.6.5
将新安装的 python 版本导出到 PATH
export PATH=${PYENV_PYTHON_VERSIONS_HOME}/3.6.5/bin
现在在“Piplock”中指定相同的版本。
[requires] python_version = "3.6.5"
最后,运行 remove previous virtualenv 并再次重建。
pipenv --rm
pipenv install --dev
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.