Getting the Interpreter Architecture

This is a very easy tutorial; it just has a lot of explaining to do. 😉

You may recall my previous tutorial on how to get the architecture of Windows in Python. However, getting the architecture of the Python architecture is just as important. For example, you may have a logger in your program, and with it, you get vital information (Windows OS, Architecture, Python version), that aids in debugging errors. But you want to log if Python x86 or x64 is being used, and you don’t know how to do that (that’s why your reading this tutorial ;)). There are two ways of doing it, one is the “harder to set up but is easier to remember” method, and the second is the “requires a bit of memorization or copy-pasting but is better for cross-platform work” method.

While all code should be Python 2 and 3 compatible unless otherwise noted, any Exceptions are from Python 3.3+.

Method #1: The platform Module

More specifically, platform.architecture(). This is the “harder to set up but is easier to remember” way, as we shall see. First, lets run some code in an interactive session.

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
'''This is 32-bit Python, as denoted by the text "[MSC v.1500 32 bit (Intel)]" above.'''
>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')
>>>
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
'''This is 64-bit Python, as denoted by the text "[MSC v.1600 64 bit (AMD64)]" above.'''
>>> import platform
>>> platform.architecture()
('64bit', 'WindowsPE')
>>>

As we can see, when we run platform.architecture(), we are presented with a tuple containing the architecture and other data, and it differs depending on what architecture of Python we are using: 32bit for x86 Python, and 64bit for x64 Python. And this does not vary depending on the architecture of your OS. I run Windows 8 x64, and it still returns 32bit. This is mentioned in the Python documentation itself:

“Queries the given executable (defaults to the Python interpreter binary) for various architecture information.

Returns a tuple (bits, linkage) which contain information about the bit architecture and the linkage format used for the executable. Both values are returned as strings.”

For this tutorial, we will concern ourselves only with the first index of the tuple. Now to put these findings into practice.

Let’s run through the functions of the script. First, it checks the architecture of the Python interpreter, and depending on the result, assigns the appropriate string literal to the filename variable. It then displays the string, and writes a text file with that name, with the message “Cake is a lie. :p”. So what is so hard to remember about this? It’s the way you access the index 0 to check the architecture.

Unlike how we check for the Windows architecture or the Python version, we can’t just type the string within the pair of parentheses at the end or compare it to another tuple containing just that one value. Instead, we must specify the index after the function (platform.architecture()[0]), then we can compare it to a string containing the value. So while this is a bit harder to set up than the next method, it gets easier over time as you use it more and memorize the syntax needed.

But (don’t you just love that word? :P), this code does not work on all OSs. While it works for me on Windows, it may not work on Mac OS X, for example. Again, the documentation points this out:

Note: On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures.

To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:

is_64bits = sys.maxsize > 2**32

If you need to get the interpreter’s architecture and platform.architecture() will not work, you need a different way to check, which brings us to…

Method #2: sys.maxsize

(Told you there was a lot of explaining to do. :P)
This is the “requires a bit of memorization or copy-pasting but is better for cross-platform work” method. This method uses the sys.maxsize function to detect your OS. If you are planning on supporting multiple OSs, this is your best bet, as it works on any OS. The documentation for this function says:

“An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.”

However, we’re going to throw away what the documentation says for getting the architecture and the values for each one, and do something different. 😛

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
'''This is 32-bit Python, as denoted by the text "[MSC v.1500 32 bit (Intel)]" above.'''
>>> import sys
>>> print(sys.maxsize)
2147483647
>>>
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
'''This is 64-bit Python, as denoted by the text "[MSC v.1600 64 bit (AMD64)]" above.'''
>>> import sys
>>> print(sys.maxsize)
9223372036854775807
>>>

As you can see, x64 Python returns a larger number than x86 Python, for reasons way beyond the scope of this tutorial. Now, let’s take just the number from x86 Python (2147483647) and write our script.

As you can see, this version does the exact same thing as platform.architecture(), but instead of having to define an index then set up an tuple to check, you just compare it directly to sys.maxsize. I’ve used the x86 version because it is way shorter, and (arguably) less prone to breakage if you are updating your script a lot.

And now you know how to get the Python interpreter’s architecture using two different methods! 😀 Which one you choose to use, though, is completely up to you. 😉

Advertisements

One thought on “Getting the Interpreter Architecture

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s