Python get exe version

PyMOTW

If you find this information useful, consider picking up a copy of my book, The Python Standard Library By Example.

Page Contents

This Page

Examples

The output from all the example programs from PyMOTW has been generated with Python 2.7.8, unless otherwise noted. Some of the features described here may not be available in earlier versions of Python.

If you are looking for examples that work under Python 3, please refer to the PyMOTW-3 section of the site.

platform – Access system version information¶

Purpose: Probe the underlying platform’s hardware, operating system, and interpreter version information.
Available In: 2.3 and later

Although Python is often used as a cross-platform language, it is occasionally necessary to know what sort of system a program is running on. Build tools need that information, but an application might also know that some of the libraries or external commands it uses have different interfaces on different operating systems. For example, a tool to manage the network configuration of an operating system can define a portable representation of network interfaces, aliases, IP addresses, etc. But once it actually needs to edit the configuration files, it must know more about the host so it can use the correct operating system configuration commands and files. The platform module includes the tools for learning about the interpreter, operating system, and hardware platform where a program is running.

Читайте также:  Php имя файла в url

The example output below was generated on a MacBook Pro3,1 running OS X 10.6.4, a VMware Fusion VM running CentOS 5.5, and a Dell PC running Microsoft Windows 2008. Python was installed on the OS X and Windows systems using the pre-compiled installer from python.org. The Linux system is running an interpreter built from source locally.

Interpreter¶

There are four functions for getting information about the current Python interpreter. python_version() and python_version_tuple() return different forms of the interpreter version with major, minor, and patchlevel components. python_compiler() reports on the compiler used to build the interpreter. And python_build() gives a version string for the build of the interpreter.

import platform print 'Version :', platform.python_version() print 'Version tuple:', platform.python_version_tuple() print 'Compiler :', platform.python_compiler() print 'Build :', platform.python_build() 
$ python platform_python.py Version : 2.7.2 Version tuple: ('2', '7', '2') Compiler : GCC 4.2.1 (Apple Inc. build 5666) (dot 3) Build : ('v2.7.2:8527427914a2', 'Jun 11 2011 15:22:34')
$ python platform_python.py Version : 2.7.0 Version tuple: ('2', '7', '0') Compiler : GCC 4.1.2 20080704 (Red Hat 4.1.2-46) Build : ('r27', 'Aug 20 2010 11:37:51')
C:> python.exe platform_python.py Version : 2.7.0 Version tuple: ['2', '7', '0'] Compiler : MSC v.1500 64 bit (AMD64) Build : ('r27:82525', 'Jul 4 2010 07:43:08')

Platform¶

platform() returns string containing a general purpose platform identifier. The function accepts two optional boolean arguments. If aliased is True, the names in the return value are converted from a formal name to their more common form. When terse is true, returns a minimal value with some parts dropped.

import platform print 'Normal :', platform.platform() print 'Aliased:', platform.platform(aliased=True) print 'Terse :', platform.platform(terse=True) 
$ python platform_platform.py Normal : Darwin-11.4.2-x86_64-i386-64bit Aliased: Darwin-11.4.2-x86_64-i386-64bit Terse : Darwin-11.4.2
$ python platform_platform.py Normal : Linux-2.6.18-194.3.1.el5-i686-with-redhat-5.5-Final Aliased: Linux-2.6.18-194.3.1.el5-i686-with-redhat-5.5-Final Terse : Linux-2.6.18-194.3.1.el5-i686-with-glibc2.3
C:> python.exe platform_platform.py Normal : Windows-2008ServerR2-6.1.7600 Aliased: Windows-2008ServerR2-6.1.7600 Terse : Windows-2008ServerR2

Operating System and Hardware Info¶

More detailed information about the operating system and hardware the interpreter is running under can be retrieved as well. uname() returns a tuple containing the system, node, release, version, machine, and processor values. Individual values can be accessed through functions of the same names:

Читайте также:  Удаление из листа java

system() returns the operating system name node() returns the hostname of the server, not fully qualified release() returns the operating system release number version() returns the more detailed system version machine() gives a hardware-type identifier such as 'i386' processor() returns a real identifier for the processor, or the same value as machine() in many cases

import platform print 'uname:', platform.uname() print print 'system :', platform.system() print 'node :', platform.node() print 'release :', platform.release() print 'version :', platform.version() print 'machine :', platform.machine() print 'processor:', platform.processor() 
$ python platform_os_info.py uname: ('Darwin', 'hubert.local', '11.4.2', 'Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64', 'x86_64', 'i386') system : Darwin node : hubert.local release : 11.4.2 version : Darwin Kernel Version 11.4.2: Thu Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 machine : x86_64 processor: i386
$ python platform_os_info.py uname: ('Linux', 'hermes.hellfly.net', '2.6.18-194.3.1.el5', '#1 SMP Thu May 13 13:09:10 EDT 2010', 'i686', 'i686') system : Linux node : hermes.hellfly.net release : 2.6.18-194.3.1.el5 version : #1 SMP Thu May 13 13:09:10 EDT 2010 machine : i686 processor: i686
C:> python.exe platform_os_info.py uname: ('Windows', 'dhellmann', '2008ServerR2', '6.1.7600', 'AMD64', 'Intel64 Family 6 Model 15 Stepping 11, GenuineIntel') system : Windows node : dhellmann release : 2008ServerR2 version : 6.1.7600 machine : AMD64 processor: Intel64 Family 6 Model 15 Stepping 11, GenuineIntel

Executable Architecture¶

Individual program architecture information can be probed using the architecture() function. The first argument is the path to an executable program (defaulting to sys.executable , the Python interpreter). The return value is a tuple containing the bit architecture and the linkage format used.

import platform print 'interpreter:', platform.architecture() print '/bin/ls :', platform.architecture('/bin/ls') 
$ python platform_architecture.py interpreter: ('64bit', '') /bin/ls : ('64bit', '')
$ python platform_architecture.py interpreter: ('32bit', 'ELF') /bin/ls : ('32bit', 'ELF')
C:> python.exe platform_architecture.py interpreter : ('64bit', 'WindowsPE') iexplore.exe : ('64bit', '')

platform Standard library documentation for this module.

© Copyright Doug Hellmann. | | Last updated on Jul 11, 2020. | Created using Sphinx. | Design based on «Leaves» by SmallPark |

Источник

PyWin32: How to Get an Application’s Version Number

Occasionally you will need to know what version of software you are using. The normal way to find this information out is usually done by opening the program, going to its Help menu and clicking the About menu item. But this is a Python blog and we want to do it programmatically! To do that on a Windows machine, we need PyWin32. In this article, we’ll look at two different methods of getting the version number of an application.

Getting the Version with win32api

First off, we’ll get the version number using PyWin32’s win32api module. It’s actually quite easy to use. Let’s take a look:

from win32api import GetFileVersionInfo, LOWORD, HIWORD def get_version_number(filename): try: info = GetFileVersionInfo (filename, "\\") ms = info['FileVersionMS'] ls = info['FileVersionLS'] return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls) except: return "Unknown version" if __name__ == "__main__": version = ".".join([str (i) for i in get_version_number ( r'C:\Program Files\Internet Explorer\iexplore.exe')]) print version

Here we call GetFileVersionInfo with a path and then attempt to parse the result. If we cannot parse it, then that means that the method didn’t return us anything useful and that will cause an exception to be raised. We catch the exception and just return a string that tells us we couldn’t find a version number. For this example, we check to see what version of Internet Explorer is installed.

Getting the Version with win32com

To make things more interesting, in the following example we check Google Chrome’s version number using PyWin32’s win32com module. Let’s take a look:

# based on http://stackoverflow.com/questions/580924/python-windows-file-version-attribute from win32com.client import Dispatch def get_version_via_com(filename): parser = Dispatch("Scripting.FileSystemObject") version = parser.GetFileVersion(filename) return version if __name__ == "__main__": path = r"C:\Program Files\Google\Chrome\Application\chrome.exe" print get_version_via_com(path)

All we do here is import win32com’s Dispatch class and create an instance of that class. Next we call its GetFileVersion method and pass it the path to our executable. Finally we return the result which will be either the number or a message saying that no version information was available. I like this second method a bit more in that it automatically returns a message when no version information was found.

Wrapping Up

Now you know how to check an application version number on Windows. This can be helpful if you need to check if key software needs to be upgraded or perhaps you need to make sure it hasn’t been upgraded because some other application requires the older version.

Источник

Get Application Version using Python

In software industries, whenever developers add a new feature, fix bugs in a particular application, they name the application to a new version, as it helps to recognize the recently updated features in that application.

Using Python, we can get the version of any application. We will use pywin32 to interact with the executable files. It provides access to the win32 API which gives the ability to create COM and objects.

  • First, install the required package by typing pip install pywin32 in the command shell.
  • Import Dispatch to get the version number of the application.
  • Create a variable to store the location of the application.
  • Define a function for getting the version that takes the location of the executable file and parses using the Dispatch method.
  • Return the version number using GetFileVersion(app_location) method.

Example

In this example, we try to get the version of Chrome Application in our System, #Import the required Module from win32com.client import Dispatch def get_version_number(app_location): parser = Dispatch("Scripting.FileSystemObject") version = parser.GetFileVersion(app_location) return version app_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' version = get_version_number(app_location) print(version)

Output

Running the above code will print the version of the Chrome Application.

Now, try to give the location of another application to retrieve its version number.

Источник

Оцените статью