简体   繁体   English

pip 和 python 指的是不同的口译员

[英]pip and python referring to different interpreters

I know "multiple-versions-of-python-mess" is nothing new but my question is more specific.我知道“multiple-versions-of-python-mess”并不是什么新鲜事,但我的问题更具体。 I'm learning how to use venv (and pyenv, etc.) and I've run into a strange situation.我正在学习如何使用 venv(和 pyenv 等),但遇到了一个奇怪的情况。

I have a number of different versions of python installed (as one does).我安装了许多不同版本的 python(就像一个版本一样)。 I use one of them, 3.9, to create a venv:我使用其中之一,3.9,来创建一个 venv:

$ /usr/local/Cellar/python@3.9/3.9.12_1/bin/python3 -m venv ./venvpractice

Then I activate it:然后我激活它:

cd venvpractice
$ source bin/activate

Here's where the "trouble" starts:这是“麻烦”开始的地方:

(venvpractice) $ which python
~/venvpractice/bin/python

(venvpractice) $ which python3
python3: aliased to /usr/local/bin/python3

(venvpractice) $ which pip
pip: aliased to /usr/local/bin/pip3

(venvpractice) $ which pip3
~/venvpractice/bin/pip3

Wouldn't you expect pip and python to match (ie be from the same place), and for pip3 and python3 to match as well?您不希望pippython匹配(即来自同一个地方),并且pip3python3也匹配吗? Why are they all jumbled up?为什么都乱七八糟的?

I know it's not the biggest deal.我知道这不是最重要的。 I should just be careful and make sure I call the correct one, say, when I do pip3 freeze > requirements.txt .我应该小心并确保我调用了正确的方法,例如,当我执行pip3 freeze > requirements.txt时。 But I just want to understand what's going on under the hood.但我只想了解幕后发生的事情。 I feel like things are still mucked up.我觉得事情仍然一团糟。 So many versions and aliases and symlinks and PATH variables in /.zshrc ... And then there's pyenv with which I also experimented a bit... pyenv /.zshrc我也用它做了一些实验……

Please help?!请帮忙?!

venv will not unalias anything for you; venv不会为你unalias if you have aliases, your shell interprets those before the venv stuff even gets a chance.如果你有别名,你的 shell 在venv的东西甚至有机会之前解释那些。

My simple recommendation would be to remove these aliases from your shell's startup files, or at least interactively unalias them temporarily.我的简单建议是从 shell 的启动文件中删除这些别名,或者至少暂时以交互unalias它们的别名。

If you need for there to be an alias, try something like如果您需要别名,请尝试类似

alias python3='env python3'

which should do the right thing both in a virtual environment and out of one (provided your PATH is moderately sane, and you have /usr/local/bin/python3 pointing to /usr/local/bin/python ).这应该在虚拟环境中和虚拟环境中做正确的事情(前提是您的PATH适度理智,并且您有/usr/local/bin/python3指向/usr/local/bin/python )。

(Though in other news, aliases are inferior to shell functions. For this simple case, mmmmaybe live with an alias.) (尽管在其他新闻中,别名不如 shell 函数。对于这个简单的案例,mmmmmaybe 可以使用别名。)

A slightly more elaborate function might look like稍微复杂一点的 function 可能看起来像

python () {
    case ${VIRTUAL_ENV-} in
      '') /usr/local/bin/python "$@" ;;
      *) command python "$@";;
    esac
}

But ultimately, if you are using pyenv anyway, probably just get out of its way and let it handle these things for you;但最终,如果你无论如何都在使用pyenv ,可能只是让开它,让它为你处理这些事情; it does that nicely and transparently.它做得很好而且透明。

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

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