简体   繁体   English

为什么 Pipenv 找不到适合我的 venv 的 python 的正确版本?

[英]Why can't Pipenv find the correct version of python for my venv?

I need a little assistance with an issue I'm having with one of my venvs.我需要一些帮助来解决我的一个 venvs 遇到的问题。

TLDR TLDR

With a fresh virtual environment created with pipenv , running pipenv run python --version returns the following message, along with the wrong python version:使用pipenv创建的全新虚拟环境,运行pipenv run python --version返回以下消息,以及错误的 python 版本:

Warning: the which -a system utility is required for Pipenv to find Python installations properly.
  Please install it.
Python 3.9.7 <--- this should be 3.8.5

Activating the virtual environment and then running python --version shows the correct version, but this prevents me from running anything via pipenv run <any command> with the venv not activated, and also raises suspicion that something isn't quite right.激活虚拟环境然后运行python --version显示正确的版本,但这会阻止我在未激活 venv 的情况下通过pipenv run <any command>内容,并且还会让人怀疑某些事情不太正确。 This is on MacOS Big Sur 11.6这是在 MacOS Big Sur 11.6 上

My Process我的过程

  1. Install my desired python version with pyenv install 3.8.5 :使用pyenv install 3.8.5安装我想要的 python 版本:
$ pyenv install 3.8.5                                                             
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.5.tar.xz...
-> https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
Installing Python-3.8.5...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.5 to /Users/<me>/.pyenv/versions/3.8.5

Output of pyenv versions : Output 的pyenv versions

* system (set by /Users/<me>/.pyenv/version)
  3.6.4
  3.6.7
  3.8.12
  3.8.5
  3.9.1
  1. Create new environment and install reqs from Pipfile with pipenv install --dev .创建新环境并使用 pipenv install --dev 从pipenv install --dev Pipfile contains Pipfile 包含
[requires]
python_full_version = "3.8.5"

Output: Output:

Creating a virtualenv for this project...
Pipfile: /Users/<me>/dev/<project>/Pipfile
Using /Users/<me>/.pyenv/versions/3.8.5/bin/python3.8 (3.8.5) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 325ms
  creator CPython3Posix(dest=/Users/<me>/.local/share/virtualenvs/<project>-OYt9MQn5, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/<me>/Library/Application Support/virtualenv)
    added seed packages: pip==21.1.2, setuptools==57.0.0, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /Users/<me>/.local/share/virtualenvs/<project>-OYt9MQn5
Installing dependencies from Pipfile.lock (62a518)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 108/108 — 00:00:50
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
  1. Try to run pipenv run python --version (it finds the wrong one):尝试运行pipenv run python --version (它发现错误):
$ pipenv run python --version
Loading .env environment variables...
Warning: the which -a system utility is required for Pipenv to find Python installations properly.
  Please install it.
Python 3.9.7
  1. Activate the shell, and run either the above command (with the same output), or just python --version , since the shell is activated now, and it finds the correct version:激活 shell,并运行上述命令(具有相同的输出),或仅python --version ,因为 shell 现在已激活,它会找到正确的版本:
(<project>) $ pipenv shell 
Courtesy Notice: Pipenv found itself running within a virtual environment, so it will automatically use that environment, instead of creating its own for any project. You can set PIPENV_IGNORE_VIRTUALENVS=1 to force pipenv to ignore that environment and create its own instead. You can set PIPENV_VERBOSITY=-1 to suppress this warning.
Loading .env environment variables...
Launching subshell in virtual environment...
 . /Users/<me>/.local/share/virtualenvs/<project>-OYt9MQn5/bin/activate
N/A: version "N/A -> N/A" is not yet installed.

You need to run "nvm install N/A" to install it before using it.
 $  . /Users/ckz8780/.local/share/virtualenvs/<project>-OYt9MQn5/bin/activate
(<project>) $ python --version 
Python 3.8.5
(<project>) $

FWIW, I have several other venvs I've created in the exact same way (or so I thought) which work fine. FWIW,我还有其他几个以完全相同的方式(或者我认为)创建的 venvs,它们工作正常。 In those venvs, when I run pipenv run python --version with the venv deactivated, the which -a error isn't there and they find the right python version.在那些 venvs 中,当我运行pipenv run python --version并停用 venv 时,不存在which -a错误,他们找到了正确的 python 版本。 In fact, even if I activate the shell in those venvs and run pipenv run python --version , it still works fine and finds the right one.事实上,即使我在那些 venvs 中激活 shell 并运行pipenv run python --version ,它仍然可以正常工作并找到正确的版本。 The only difference in those venvs is the python version (two are using python v3.6.x, and two are using python v3.8.9).这些 venvs 的唯一区别是 python 版本(两个使用 python v3.6.x,两个使用 python v3.8.9)。 They all work as expected, but this one just will not seem to find the right version no matter what I do.它们都按预期工作,但无论我做什么,这个版本似乎都找不到正确的版本。 I am pulling my hair out.我正在拔头发。

Any help is greatly appreciated!!!任何帮助是极大的赞赏!!!

Thanks谢谢

I am having a similar workflow when it comes to installing Python (using both Pyenv and Pipenv).在安装 Python(同时使用 Pyenv 和 Pipenv)时,我有一个类似的工作流程。

This is working for me:这对我有用:

  1. Install new Python version with Pyenv:使用 Pyenv 安装新的 Python 版本:
pyenv install 3.9.0
  1. Run pyenv shell运行 pyenv shell
pyenv shell 3.9.0
  1. In pyenv shell install pipenv via pip在 pyenv shell 通过 pip 安装 pipenv
pip install pipenv
  1. With pipenv install packages...使用 pipenv 安装包...
pipenv install ...
  1. Finally create a virtualenv via Pipenv that is using the Python version from Pyenv:最后通过 Pipenv 创建一个 virtualenv,它使用 Pyenv 的 Python 版本:
pipenv shell 3.9.0

Output: Output:

Pipfile: /Users/fabianmeyer/Desktop/analytics/Pipfile
Using /Users/fabianmeyer/.pyenv/versions/3.9.0/bin/python3.9 (3.9.0) to create virtualenv...
⠹ Creating virtual environment...created virtual environment CPython3.9.0.final.0-64 in 1824ms
  creator CPython3Posix(dest=/Users/fabianmeyer/.local/share/virtualenvs/analytics-GC4g190F, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/fabianmeyer/Library/Application Support/virtualenv)
    added seed packages: pip==22.0.3, setuptools==60.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment! 
Virtualenv location: /Users/fabianmeyer/.local/share/virtualenvs/analytics-GC4g190F

Copying an answer from a pipenv GitHub issue :pipenv GitHub 问题复制答案:

This is not how pipenv works, you can't call it to run something from inside a virtualenv it is managing.这不是 pipenv 的工作方式,你不能调用它来从它管理的 virtualenv 内部运行一些东西。 Pipenv is installed externally to the virtualenv, which means you use pipenv run the same way you use pipenv install — without ever activating the environment directly. Pipenv 安装在 virtualenv 外部,这意味着您使用pipenv run的方式与使用pipenv install方式相同——无需直接激活环境。 That's part of the reason why pipenv exists, so that you won't have to activate environments or even worry about where they might be— this is handled for you.这就是 pipenv 存在的部分原因,这样您就不必激活环境,甚至不必担心它们可能在哪里——这是为您处理的。

Since pipenv isn't installed inside the environments it manages, this makes sense.由于 pipenv 没有安装在它管理的环境中,这是有道理的。 I'm going to close this issue since it's not a problem with pipenv and since this information is covered in a bunch of other issues我将关闭此问题,因为它不是 pipenv 的问题,并且此信息包含在许多其他问题中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM