为什么我的VENV似乎在全球安装?
对于一个学校项目,我必须与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 list
ed, 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您要检查运行
python3
时引用了哪个 python 解释器:为此,请运行which python3
(这应该返回path/to/ env/bin/python3
(如果您的环境已成功激活)每次您想要在虚拟环境中安装软件包时,请确保运行
python3 -m pip install my_package
而不是点安装my_package
:这将确保为与您的环境关联的 python 解释器安装my_package
。最后,为了避免在全局 python 解释器上错误安装任何包,请将其添加到
.bashrc
/.zshrc
中:每次尝试时都会引发错误将包安装到全局解释器。为了保留全局安装包的选项,我另外定义了以下命令:
并运行 gpip install my_package 来全局安装它。
First of all, you want to check which python interpreter is referred to when running
python3
: to do so, runwhich python3
(this should returnpath/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 ofpip install my_package
: this will make sure thatmy_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
: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:
and run
gpip install my_package
to install it globally.