PIP和Python指的是不同的口译员

发布于 2025-01-31 18:20:54 字数 1040 浏览 3 评论 0原文

我知道“多个python-mess”并不是什么新鲜事,但是我的问题更具体。我正在学习如何使用VENV(和Pyenv等),而且我遇到了一个奇怪的情况。

我安装了许多不同版本的Python(就像一个人一样)。我使用其中一个3.9来创建一个VENV:

$ /usr/local/Cellar/[email protected]/3.9.12_1/bin/python3 -m venv ./venvpractice

然后我激活它:

cd venvpractice
$ source bin/activate

这是“麻烦”开始的地方:

(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

您是否期望pip python 匹配(即来自同一个地方),对于pip3python3也可以匹配吗?他们为什么都混乱了?

我知道这不是最大的交易。我应该小心,并确保我打电话给正确的一个,例如,当我做pip3 freeze>需求.txt。但是我只想了解引擎盖下的情况。我觉得事情仍然陷入困境。 /。zshrc ...中有许多版本,别名,符号链接和路径变量...然后有pyenv我还尝试了一些尝试...

请帮助吗?

I know "multiple-versions-of-python-mess" is nothing new but my question is more specific. I'm learning how to use venv (and pyenv, etc.) and I've run into a strange situation.

I have a number of different versions of python installed (as one does). I use one of them, 3.9, to create a venv:

$ /usr/local/Cellar/[email protected]/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 (i.e. be from the same place), and for pip3 and python3 to match as well? 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. 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...

Please help?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2025-02-07 18:20:54

VENV不会unalias为您提供任何东西;如果您有别名,则您的外壳会在venv的东西之前解释这些东西,甚至有机会。

我的简单建议是将这些别名从您的Shell的启动文件中删除这些别名,或者至少是交互式unalias临时。

如果您需要有一个别名,请尝试

alias python3='env python3'

在虚拟环境和一个中应该做正确的事情(前提您的PATH是适度的理智,您都有/ usr/local/bin/python3指向/usr/usr/local/bin/python)。

(尽管在其他新闻中,别名不如Shell功能。对于这种简单的情况,MMMMAYBE与别名活着。)

稍微更详尽的功能可能看起来像

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

,但最终,如果您使用pyenv无论如何,可能只是摆脱困境,让它为您处理这些事情;它做得很好,透明。

venv will not unalias anything for you; if you have aliases, your shell interprets those before the venv stuff even gets a chance.

My simple recommendation would be to remove these aliases from your shell's startup files, or at least interactively unalias them temporarily.

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).

(Though in other news, aliases are inferior to shell functions. For this simple case, mmmmaybe live with an alias.)

A slightly more elaborate function might look like

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; it does that nicely and transparently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文