Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect if Python is running as a 64-bit application? [duplicate]

Tags:

python

64-bit

People also ask

How can I tell if Python is 64-bit?

Python Version Bit – Does My Python Shell Run 32 Bit or 64 Bit Version? To check which bit version the Python installation on your operating system supports, simply run the command “ python ” (without quotes) in your command line or PowerShell (Windows), terminal (Ubuntu, macOS), or shell (Linux).

How do I change my Python from 32-bit to 64-bit?

No, it is not possible to upgrade a 32bit Python installation to a 64bit one. Still, there is something that you can do in order to speedup installation of a new 64bit version. Run pip freeze > packages. txt on the old installation in order to generate a list of all installed packages and their versions.

Does Python support 64-bit?

Python 64-bit can't load 32-bit libraries without some heavy hacks running another Python, this time in 32-bit, and using IPC. If you have to load DLLs that you compile yourself, you'll have to compile them to 64-bit, which is usually harder to do (specially if using MinGW on Windows).


import platform
platform.architecture()

From the Python docs:

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.


While it may work on some platforms, be aware that platform.architecture is not always a reliable way to determine whether python is running in 32-bit or 64-bit. In particular, on some OS X multi-architecture builds, the same executable file may be capable of running in either mode, as the example below demonstrates. The quickest safe multi-platform approach is to test sys.maxsize on Python 2.6, 2.7, Python 3.x.

$ arch -i386 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 2147483647)
>>> ^D
$ arch -x86_64 /usr/local/bin/python2.7
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform, sys
>>> platform.architecture(), sys.maxsize
(('64bit', ''), 9223372036854775807)