为什么 Pipenv 不获取我的 Pyenv 版本?

Why is Pipenv not picking up my Pyenv versions?

我的系统 Python 版本是 3.8.5,但是我使用 pyenv 来管理一个额外的版本,3.6.0,来镜像我的项目部署到的服务器版本。我以前使用 virtualenv + virtualenvwrapper 来管理我的虚拟环境,但我听说 pipenv 很棒,我想我会试一试。一切都很好,直到我尝试使用 Python 3.6.0。流程是这样的:

$ mkdir test_project && cd test_project
$ pyenv shell 3.6.0
$ pipenv install django
Creating a virtualenv for this project…
Pipfile: /home/user/projects/test_project/Pipfile
Using /home/user/.pyenv/shims/python (3.6.0) to create virtualenv…
⠸ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 130ms
  creator CPython3Posix(dest=/home/user/.local/share/virtualenvs/test_project-eAvoynKo-/home/user, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/user/.local/share/virtualenv)
    added seed packages: pip==20.2.3, setuptools==50.3.0, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Traceback (most recent call last):
  File "/home/user/.pyenv/versions/3.6.0/bin/pipenv", line 11, in <module>
    sys.exit(cli())
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/vendor/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/cli/command.py", line 252, in install
    site_packages=state.site_packages
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/core.py", line 1928, in do_install
    site_packages=site_packages,
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/core.py", line 580, in ensure_project
    pypi_mirror=pypi_mirror,
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/core.py", line 512, in ensure_virtualenv
    python=python, site_packages=site_packages, pypi_mirror=pypi_mirror
  File "/home/user/.pyenv/versions/3.6.0/lib/python3.6/site-packages/pipenv/core.py", line 986, in do_create_virtualenv
    with open(project_file_name, "w") as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/.local/share/virtualenvs/test_project-eAvoynKo-/home/user/.pyenv/shims/python/.project'

我遇到了上一个问题 并在我的 .bashrc 文件中设置了环境变量 PIPENV_PYTHON="$PYENV_ROOT/shims/python。无济于事。

使用系统 Python 版本 3.8.5 完美运行:

$ pyenv install django
Creating a virtualenv for this project…
Pipfile: /home/user/projects/test_project/Pipfile
Using /home/user/.pyenv/shims/python (3.8.5) to create virtualenv…
⠹ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 114ms
  creator CPython3Posix(dest=/home/user/.local/share/virtualenvs/test_project-eAvoynKo-/home/user/.pyenv/shims/python, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/user/.local/share/virtualenv)
    added seed packages: pip==20.2.2, setuptools==50.3.0, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /home/user/.local/share/virtualenvs/test_project-eAvoynKo-/home/user/.pyenv/shims/python
Creating a Pipfile for this project…
Installing django…
Adding django to Pipfile's [packages]…
✔ Installation Succeeded 
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Building requirements...
Resolving dependencies...
✔ Success! 
Updated Pipfile.lock (a6086c)!
Installing dependencies from Pipfile.lock (a6086c)…
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

更新

虽然我仍然无法识别使用 pyenv shell x.x.x 激活的 python 版本,但删除了 PIPENV_PYTHON 环境变量,并使用 [=16 创建了一个新的 virtualenv =] pipenv 确实识别安装的 pyenv 版本。

pipenv 不尊重 pyenv localpyenv global (reference)

也许它也不尊重pyenv shell

我通常做你做的,指定python喜欢pipenv install --python 3.7

你可以尝试让 Pipenv 遵循 Pyenv Python 版本:

将 Pipenv 指向 shell 配置文件中的 Pyenv shim(这是 /.zshrc)

export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

备注:

  1. 确保在 shell 配置文件中正确设置 PYENV_ROOT

  2. 如果您使用 python.

    的另一个版本创建它们,您可能需要重新制作您的 pipfile 和 pipfile.lock

答案来自 Pipenv docs, 和 bani(此问题的另一个答案)。

pyenv 中的不同版本如何设置.bashrc。这个对我有用

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