访问python Egg自己的元数据
我使用 setuptools 生成了一个 python Egg,并希望在运行时访问它的元数据。 我目前正在这样做:
import pkg_resources
dist = pkg_resources.get_distribution("my_project")
print(dist.version)
但是如果我安装了同一个 Egg 的多个版本,这可能会无法正常工作。 如果我同时安装了 Egg 和开发版本,那么从开发版本运行此代码将获取已安装 Egg 的版本。
那么,如何获取我的 Egg 的元数据,而不是系统上安装的随机匹配的 Egg?
I've produced a python egg using setuptools and would like to access it's metadata at runtime. I currently got working this:
import pkg_resources
dist = pkg_resources.get_distribution("my_project")
print(dist.version)
but this would probably work incorrectly if I had multiple versions of the same egg installed. And if I have both installed egg and development version, then running this code from development version would pick up version of the installed egg.
So, how do I get metadata for my egg not some random matching egg installed on my system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 Python 也有点陌生,但据我了解:
虽然您可以安装“相同”egg(具有相同名称)的多个版本,但在运行时只有其中一个可用于任何特定代码段(基于您的发现方法)。 因此,如果您的 Egg 是调用此代码的鸡蛋,则它必须已被选为此代码的
my_project
的版本,并且您将访问您自己的版本。I am somewhat new to Python as well, but from what I understand:
Although you can install multiple versions of the "same" egg (having the same name), only one of them will be available to any particular piece of code at runtime (based on your discovery method). So if your egg is the one calling this code, it must have already been selected as the version of
my_project
for this code, and your access will be to your own version.确切地。 因此,您应该只能获取库中当前可用的 Egg(单数)的信息。 如果您的 site-packages 文件夹中有同一个库的多个 Egg,请检查同一文件夹中的 easy-install.pth 以查看真正使用了哪个 Egg :-)
在站点上注意:这正是像这样的系统的要点zc.buildout 允许您定义可供您使用的库的确切版本,例如在开发应用程序或提供 Web 应用程序时。 例如,您可以对一个项目使用 1.0 版,对另一个项目使用 1.2 版。
Exactly. So you should only be able to get the information for the currently available egg (singular) of a library. If you have multiple eggs of the same library in your site-packages folder, check the easy-install.pth in the same folder to see which egg is really used :-)
On a site note: This is exactly the point of systems like zc.buildout which lets you define the exact version of a library that will be made available to you for example while developing an application or serving a web application. So you can for example use version 1.0 for one project and 1.2 for another.