pip在虚拟环境下如何工作?
在学习如何编写 Python 代码后,我开始学习并弄清楚如何构建项目和设置虚拟环境,但我无法决定激活虚拟环境时如何管理包。举个例子:我想做一个Django项目。因此,首先,mkdir
项目文件夹,cd
进入其中,然后执行python -m venv [whatever]
。 cd
进入 Scripts 文件夹并执行“activate”。然后,pip install Django
和 pip list
并显示 Django。最后,我停用虚拟环境并再次创建pip list
。为什么 Django 列在那里?应该吗?
After learning how to code Python I'm starting to learn and figure out how to structure projects and set virtual environments up, but I can't make my mind up on how are packages managed when the virtual environment is activated. To make an example: I want to make a Django project. So, first of all, mkdir
the project folder, cd
into it and then execute python -m venv [whatever]
. cd
into Scripts folder and execute 'activate'. Then, pip install Django
and pip list
and shows Django. At last, I deactivate the virtual environment and make pip list
again. Why is Django listed there? Should it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能已经在 venv 内部和外部安装了 Django(在系统 python 安装之外)。停用 venv 并运行
pip uninstall django
然后重试。You might have installed Django both inside and outside the venv (outside being the system python installation). Deactivate the venv and run
pip uninstall django
then try again.好吧,我终于明白发生了什么,而我却没有意识到。一方面,虚拟环境完全独立于全局或系统,因此一侧安装的内容不应影响另一侧。另一方面,发生的情况是,在 Scripts 文件夹中,当尝试执行 Python 命令时,我实际上是在执行具有相同名称的脚本,这些脚本是与虚拟环境一起使用的副本,并且可以用于调用“激活”脚本。其实没有什么问题,是我搞砸了。
感谢所有贡献者的帮助。
Okay, finally I understood what was happening and I didn't realize. On one hand, virtual environments are completely independent from the global or system, so what is being installed in one side it shouldn't affect the other. On the other hand, what happened was, that for being inside the Scripts folder, when trying to execute Python commands I was actually executing the scripts with the same name, which are copies to use with the virtual environment and which can be used calling the "activate" script. There was actually no problem, it was me messed up.
Thanks to all contributors for their help.