以编程方式查找已安装的 pywin32 版本

发布于 2024-09-30 16:39:55 字数 264 浏览 0 评论 0原文

某些 Python 包为程序提供了获取已安装版本的方法。例如

>>> import numpy
>>> numpy.version.version
'1.5.0'

,但我找不到 pywin32 这样做的方法。有什么好的办法可以了解一下吗?

Some Python packages provide a way for a program to get the installed version. E.g.

>>> import numpy
>>> numpy.version.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?

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

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

发布评论

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

评论(3

白云悠悠 2024-10-07 16:39:55

我找到了一篇博客文章 "在 Python 包中包含版本信息” 作者:Jean-Paul Calderone,其中显示您可以通过以下方式获取 pywin32 的版本:

>>> import win32api
>>> fixed_file_info = win32api.GetFileVersionInfo(win32api.__file__, '\\')
>>> fixed_file_info['FileVersionLS'] >> 16
212

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
薄暮涼年 2024-10-07 16:39:55

改编自 Mark 的官方回复: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()

因为 Craig 的答案在 amd64 版本上不再对我有用。

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.

一绘本一梦想 2024-10-07 16:39:55

这是迄今为止我想到的唯一方法。它在 Python 安装的 site-packages 目录中找到一个名为 pywin32.version.txt 的文件,并读取其中的内容。

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

不过,这距离官方 API 还很远!我不知道 pywin32 的哪些版本安装了该 pywin32.version.txt 文件,以及将来继续这种情况的可能性有多大。

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.

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