At 07:08 PM 1/30/2010 -0500, Barry Warsaw wrote:
On Jan 29, 2010, at 11:03 PM, Tarek Ziadé wrote:
Yes, that's how Jinja does already for example, using Setuptools's pkg_resources :
__version__ = __import__('pkg_resources').get_distribution('Jinja2').version
And that's different yet again from what PJE suggests.
*shrug* A mere stylistic difference. My spelling's just a shorter way to do the same thing, while at the same time asserting the package's requirements. But there's nothing *wrong* with the above way of doing it. Personally, I think that querying a package's version is generally a wrongheaded idea in the first place. If you need a specific version, asserting this via your project's metadata or a require() line in your script is the way to go. If you are querying API existence, hasattr() is the way to go. That kind of leaves working around bugs in a specific version or two as the only sensible use case for a version check... in which case, you're still better off just querying the installed version rather than asking the package for a __version__.
This is screaming for a blessed API to be pushed into the stdlib.
(BTW, why use __import__() there?)
To make it a one-liner, I would guess, in the case where you're not doing anything else with pkg_resources.