PIP和Python指的是不同的口译员
我知道“多个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 匹配(即来自同一个地方),对于pip3
和python3
也可以匹配吗?他们为什么都混乱了?
我知道这不是最大的交易。我应该小心,并确保我打电话给正确的一个,例如,当我做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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VENV
不会unalias
为您提供任何东西;如果您有别名,则您的外壳会在venv
的东西之前解释这些东西,甚至有机会。我的简单建议是将这些别名从您的Shell的启动文件中删除这些别名,或者至少是交互式
unalias
临时。如果您需要有一个别名,请尝试
在虚拟环境和一个中应该做正确的事情(前提您的
PATH
是适度的理智,您都有/ usr/local/bin/python3
指向/usr/usr/local/bin/python
)。(尽管在其他新闻中,别名不如Shell功能。对于这种简单的情况,MMMMAYBE与别名活着。)
稍微更详尽的功能可能看起来像
,但最终,如果您使用
pyenv
无论如何,可能只是摆脱困境,让它为您处理这些事情;它做得很好,透明。venv
will notunalias
anything for you; if you have aliases, your shell interprets those before thevenv
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
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
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.