为什么我的VENV似乎在全球安装?

发布于 2025-01-21 03:34:59 字数 607 浏览 4 评论 0原文

对于一个学校项目,我必须与Python Venv合作。 这不是第一次,但是当我初始化并激活VENV并进行pip列表时,它显示了我的整个全局软件包集合。 因此,我递归地删除了整个内容,并认为最好在全球范围内简单地重新安装所有必需品(Numpy,Pandas等),然后在需要的情况下仅安装细节。

然后我发现,无论我安装了什么地方(是否激活了VENV),它总是只能在全球下载。 无论我在哪里pip列表 ed,它总是会显示整个内容。 这让我认为我可能从来没有让Venvs上班。

我总是这样做:

python3 -m venv venv
source venv/bin/activate

它将在暗示它有效的小箭头(或$)之前显示(venv),但显然不存在。

我想念什么?

我在M1 MacBook Air上使用ZSH(哦,我 - ZSH,但我认为这应该有所作为),Python 3.10。


编辑: 事实证明,在一个特定的VENV中仅是这种情况。创建另一个VENV时,预期的行为恢复了。 仍然不确定有什么问题。

For a school project I had to work with a Python venv.
This was not the first time, but when I initialised and activated the venv and did pip list it showed my entire global package collection.
So I recursively deleted the whole thing and thought it would be best to simply reinstall all the essentials (numpy, pandas, etc.) globally again and then install only specifics where needed.

Then I found out that no matter where I installed (with venv activated or not), it would always just download globally.
No matter where I pip listed, it would always show the whole thing.
This got me to think that I might have never gotten venvs to work.

I always do:

python3 -m venv venv
source venv/bin/activate

and it will show (venv) before the little arrow (or $) implying it works, but it clearly doesn't.

What am I missing?

I am on a M1 MacBook Air, using zsh (oh-my-zsh, but I don't think that should make a difference), Python 3.10.


edit:
It turned out to only be the case in one specific venv. When creating another venv the expected behaviour returned.
Still not sure what was the problem though.

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

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

发布评论

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

评论(1

浴红衣 2025-01-28 03:34:59

首先,您要检查运行 python3 时引用了哪个 python 解释器:为此,请运行 which python3 (这应该返回 path/to/ env/bin/python3(如果您的环境已成功激活)

每次您想要在虚拟环境中安装软件包时,请确保运行 python3 -m pip install my_package 而不是 点安装my_package:这将确保为与您的环境关联的 python 解释器安装 my_package

最后,为了避免在全局 python 解释器上错误安装任何包,请将其添加到 .bashrc / .zshrc 中:

export PIP_REQUIRE_VIRTUALENV="true"

每次尝试时都会引发错误将包安装到全局解释器。为了保留全局安装包的选项,我另外定义了以下命令:

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

并运行 gpip install my_package 来全局安装它。

First of all, you want to check which python interpreter is referred to when running python3: to do so, run which python3 (this should return path/to/env/bin/python3 if your env was successfully activated)

Each time you want to install a package in your virutal env, make sure to run python3 -m pip install my_package instead of pip install my_package: this will make sure that my_package will be installed for the python interpreter associated to your environment.

Finally, in order to avoid installing by mistake any package on your global python interpreter, add this to your .bashrc / .zshrc:

export PIP_REQUIRE_VIRTUALENV="true"

This will raise an error each time you try to install a package to the global interpreter. In order to keep the option to install packages globally, I define the following command additionally:

gpip() {
    PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

and run gpip install my_package to install it globally.

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