Some Python packages provide a way for a program to get the installed version. E.g.
>>> import numpy
>>> numpy.versi开发者_如何学运维on.version
'1.5.0'
But I can't find a way to do so for pywin32
. What good way might there be to find out?
I found a blog post "Include version information in your Python packages" by Jean-Paul Calderone which showed you can get the version of pywin32
this way:
>>> import win32api
>>> fixed_file_info = win32api.GetFileVersionInfo(win32api.__file__, '\\')
>>> fixed_file_info['FileVersionLS'] >> 16
212
Adapted from Mark's official response at: http://mail.python.org/pipermail/python-win32/2010-April/010404.html
import os
import distutils.sysconfig
pth = distutils.sysconfig.get_python_lib(plat_specific=1)
ver = open(os.path.join(pth, "pywin32.version.txt")).read().strip()
as Craig's answer no longer worked for me on the amd64 build.
This is the only way I've figured out so far. It finds a file called pywin32.version.txt
in the Python installation's site-packages
directory, and reads the contents.
def get_pywin32_version():
for path in sys.path:
if os.path.isdir(path):
filename = os.path.join(path, 'pywin32.version.txt')
if os.path.isfile(filename):
with open(filename) as f:
pywin32_version = f.read()
pywin32_version = pywin32_version.strip()
return pywin32_version
That's far from an official API, though! I don't know what versions of pywin32
have installed that pywin32.version.txt
file, and how likely that is to continue in future.
精彩评论