37

I am not able to find any command to check if my python is compiled for 32bit system or 64bit system.

I tried

python

and it only tells the version

Also when I go to python download site they have one version of python for linux but two versions for mac i.e 32bit and 64bit.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Mirage
  • 30,868
  • 62
  • 166
  • 261
  • Which download site? The python.org download site (http://www.python.org/download/) does not provide binary installers for Linux, only Mac OS X and Windows. – Ned Deily May 24 '11 at 08:59
  • i was talking bout that site , i downloaded the tar.bz2 file. i was thinking that for 64bit i will have diff tar file or its same – Mirage May 24 '11 at 09:04
  • 1
    Just a reminder. Most Linux distros have python installed by default, if it's not the version you want, you can always get a different one with your package manager. Building from scratch might break your default Python installation and impale system functions. – Wang Dingwei May 24 '11 at 09:19
  • Possible duplicate of [How do I determine if my python shell is executing in 32bit or 64bit mode on OS X?](https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode-on-os) – CristiFati Apr 25 '18 at 19:04

4 Answers4

56

For Python 2.6 and above, you can use sys.maxsize as documented here:

import sys
is_64bits = sys.maxsize > 2**32

UPDATE: I notice that I didn't really answer the question posed. While the above test does accurately tell you whether the interpreter is running in a 32-bit or a 64-bit architecture, it doesn't and can't answer the question of what is the complete set of architectures that this interpreter was built for and could run in. As was noted in the question, this is important for example with Mac OS X universal executables where one executable file may contain code for multiple architectures. One way to answer that question is to use the operating system file command. On most systems it will report the supported architectures of an executable file. Here's how to do it in one line from a shell command line on most systems:

file -L $(python -c 'import sys; print(sys.executable)')

Using the default system Python on OS X 10.6, the output is:

/usr/bin/python: Mach-O universal binary with 3 architectures
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc

On one Linux system:

/usr/bin/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, stripped

BTW, here's an example of why platform is not reliable for this purpose. Again using the system Python on OS X 10.6:

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False
John Y
  • 14,123
  • 2
  • 48
  • 72
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 6
    I think checking the size of a pointer (in bytes) is the most reliable/consistent way to test this across platforms - just `import ctypes`, and check the output of `ctypes.sizeof(ctypes.c_void_p)` (which will be `4` bytes for 32-bit, and `8` for 64-bit systems). – Breakthrough Jun 05 '16 at 08:27
  • sys is a built in module and always present. Importing and using ctypes is generally more expensive and might fail on some platforms. – Ned Deily Jun 05 '16 at 09:22
  • WARNING! If you on win, 100% sureyou got the proper wheel and still getting the error, then try to run pip as a module: `python -m pip install your.whl` - don't know why, but works. Also, update pip(as a module) and `setuptools`(needs administrator privilege) – frenzy Sep 27 '20 at 17:11
18
import platform
platform.architecture()[0]
#'32bit'
kostmo
  • 6,222
  • 4
  • 40
  • 51
Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • 6
    Sorry, as noted in the current Python documentation for the `platform` command, the platform.architecture test is *not* reliable at execution time on all platforms, in particular, on OS X universal builds with both 32-bit and 64-bit archs as asked by the OP. It also doesn't answer the question the OP posed as I've noted in my updated answer. – Ned Deily Aug 21 '12 at 18:47
6

First, open cmd and type in

$ python

Then, type in the following two lines

>>> import platform

>>> platform.architecture()
Boooooooooms
  • 306
  • 4
  • 21
Kamal El-Saaid
  • 145
  • 2
  • 11
1

Type in Linux console:

  1. In case when you want check whether an application has 64 bit or 32 bit architecture by using its command for run:
type -p <command_to_run_application> | xargs readlink -f | xargs file -b | sed 's/, /\n/g' | sed -n 2p
  1. In case when you want check whether an application has 64 bit or 32 bit architecture by using full path to the application:
file -b <full_path_to_an_application> | sed 's/, /\n/g' | sed -n 2p

For example, for Python 3 corresponding commands can be:

type -p python3 | xargs readlink -f | xargs file -b | sed 's/, /\n/g' | sed -n 2p
file -b /usr/bin/python3.5 | sed 's/, /\n/g' | sed -n 2p

Possible output:

x86-64

or

Intel 80386

or

ARM

or other.

If output is "Intel 80386" than the application has 32 bit architecture.

If output is "x86-64" than the application has 64 bit architecture.