Accessing python egg's own metadata

♀尐吖头ヾ 提交于 2019-12-07 06:43:18

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/177910/accessing-python-eggs-own-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!