如果站点包更新超出我在 virtualenv 中使用 pip 指定的版本,会发生什么情况?

发布于 2024-11-01 16:48:16 字数 288 浏览 2 评论 0原文

假设我的 virtualenv 安装使用--no-site-packages。我运行 bin/pip install somepackage==1.0.0,但它已经存在于我的 site-packages 中,因此未安装。随后,安装的 site-packages 副本更新为 somepackage==2.0.0

我的 virtualenv 中会发生什么?它会使用版本 2,还是自己下载版本 1?

Assume I have a virtualenv installation that does not use --no-site-packages. I run bin/pip install somepackage==1.0.0, but it's already present in my site-packages so it's not installed. Later, the copy installed site-packages is updated to somepackage==2.0.0.

What will happen in my virtualenv? Will it use the version 2, or download version 1 for itself?

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

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

发布评论

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

评论(2

陪我终i 2024-11-08 16:48:16

这取决于。如果导入 somepackage 的包均未使用 setuptools API,则其工作方式如上所述。如果您的 virtualenv 中的任何软件包使用 setuptools (或 Distribute)来指定 somepackage 的特定版本要求,setuptools 将搜索满足要求的 somepackage 版本。如果在安装时找不到合适的安装版本,它将在外部搜索并尝试安装。如果在运行时找不到,程序就会失败并出现异常。

from setuptools import setup
setup(
    name = "HelloWorld",
    version = "0.1",
    scripts = ['say_hello.py'],
    install_requires = ['somepackage == 1.0.0'],
)

例如,如果 somepackage 1.0.0 已安装在系统站点包中,则一切正常。如果您随后使用 pip 将系统站点包更新为 somepackage 2.0.0(这会卸载旧版本),则该脚本将在运行时失败并显示:

pkg_resources.DistributionNotFound: somepackage==1.0.0

如果您安装了这两个版本使用 easy_install 而不是 pipsomepackage ,情况通常会有所不同。默认情况下,easy_install不会卸载软件包,并且支持多个版本的软件包(多版本eggs)。因此,somepackage 的两个版本都可以在系统站点包中使用,并且脚本不会失败。

setuptools(以及它的Distribute克隆)必须跳过许多环节才能使多版本支持合理地工作。许多开发人员对所有这些额外的复杂性感到不满,并认为使用单独的 virutalenv 来支持多个版本的包会更容易、更透明,为此,使用更简单的 pip 模型代码> 更合适。 (公平地说,setuptoolsvirtualenv 后来才出现。)

It depends. If none of the packages that are importing somepackage use the setuptools API, then it works as described above. If any packages in your virtualenv use setuptools (or Distribute) to specify specific version requirements for somepackage, setuptools will search for a version of somepackage that meets the requirements. If it can't find a suitable installed version at install time, it will search externally and attempt to install it. If it can't find one at runtime, the program fails with an exception.

from setuptools import setup
setup(
    name = "HelloWorld",
    version = "0.1",
    scripts = ['say_hello.py'],
    install_requires = ['somepackage == 1.0.0'],
)

For example, if somepackage 1.0.0 was already installed in the system site-packages, everything is fine. If you then update the system site-packages to somepackage 2.0.0 with pip, which uninstalls the old version, the script will fail at runtime with:

pkg_resources.DistributionNotFound: somepackage==1.0.0

If you installed both versions of somepackage with easy_install instead of pip, things would normally be different. By default, easy_install does not uninstall packages and it supports multiple versions of packages (multi-version eggs). So both versions of somepackage would be available in the system site-packages and the script would not fail.

setuptools (and the Distribute clone of it) has to jump through many hoops to make the multiple version support work reasonably. Many developers frown on all that extra complexity and argue that it is easier and more transparent to support multiple versions of packages with separate virutalenv's and, for that, the simpler model of pip is more appropriate. (In fairness to setuptools, virtualenv came along later.)

枕花眠 2024-11-08 16:48:16

仅使用在 sys.path 中找到的具有给定名称的第一个包/模块。如果您的 venv 早于系统目录,则将使用您的 venv。

Only the first package/module found in sys.path with the given name will be used. If your venv is earlier than the system directory then your venv will be used.

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