pip在虚拟环境下如何工作?

发布于 2025-01-12 17:34:50 字数 352 浏览 4 评论 0原文

在学习如何编写 Python 代码后,我开始学习并弄清楚如何构建项目和设置虚拟环境,但我无法决定激活虚拟环境时如何管理包。举个例子:我想做一个Django项目。因此,首先,mkdir项目文件夹,cd进入其中,然后执行python -m venv [whatever]cd 进入 Scripts 文件夹并执行“activate”。然后,pip install Djangopip 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 技术交流群。

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

发布评论

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

评论(2

北座城市 2025-01-19 17:34:50

您可能已经在 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.

╰つ倒转 2025-01-19 17:34:50

好吧,我终于明白发生了什么,而我却没有意识到。一方面,虚拟环境完全独立于全局或系统,因此一侧安装的内容不应影响另一侧。另一方面,发生的情况是,在 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.

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