536

How can I tell what mode the shell is in, from within the shell?

I've tried looking at the platform module, but it seems only to tell you about "the bit architecture and the linkage format used for the executable". My binary is compiled as 64bit (I'm running on OS X 10.6), so it seems to always report 64bit even though I'm using the methods described here to force 32bit mode.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
jkp
  • 78,960
  • 28
  • 103
  • 104
  • 2
    I'm having problems building and loading some modules on OS X 10.6. Specifically pysco, which is complaining I'm running in 64bit mode. This is under a virtualenv as well so there is some extra complications I need to work through... – jkp Sep 10 '09 at 17:24
  • 2
    I'd like to know because when I use something like PyInstaller to create a stand-alone binary distributable (to give to users who might not have (the right version of) Python installed, the binary I produce will be 32/64 bit depending on the Python I run PyInstaller with. Ideally I'm thinking I'd like to automatically name the resulting binary/archive file with '32' or '64' in the filename, rather than having to manually rename the files depending on where I execute the 'make' command from. – Jonathan Hartley Jan 19 '16 at 04:48

17 Answers17

500

One way is to look at sys.maxsize as documented here:

$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)

On Windows, run the same commands formatted as follows:

python -c "import sys;print(\"%x\" % sys.maxsize, sys.maxsize > 2**32)"

sys.maxsize was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64

BTW, you might be tempted to use platform.architecture() for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.

$ 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
Abel
  • 56,041
  • 24
  • 146
  • 247
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • 17
    Using sys.maxint will not work to detect a 64-bit Python when running Windows (see [here](http://stackoverflow.com/questions/3411079/why-does-the-python-2-7-amd-64-installer-seem-to-run-python-in-32-bit-mode)). Instead, use struct.calcsize("P") for a cross-platform solution. – Luke Moore Mar 29 '11 at 17:42
  • 1
    Thanks for checking. I've substantially revised the answer to show the now documented `sys.maxsize` test for Python 2.6+ and the `struct` test used by the `platform` module which also works for older versions of Python 2. – Ned Deily Mar 30 '11 at 17:43
  • This doesn't work in IronPython, sys.maxsize is 2**31 for both 32 bit and 64 bit IronPython – Meh Aug 05 '14 at 15:01
  • Note: sys.maxsize / sys.maxint / strcut.calcsize('P') doesn't help when you run a Python interpreter of 32 bits on 64 bits machine and need to know **the architecture of the machine**. For this platform.machine() is useful - at least for Linux & Windows. – Yinon Ehrlich Jun 23 '15 at 05:30
  • 6
    Yinon, that's true but that's not what the question asked for. And, on those CPUs where it is possible to run, say, either 32-bit or 64-bit binaries, the arch of the machine is usually not all that relevant to a Python program; what matters is what arch the Python interpreter itself is running as. – Ned Deily Jun 23 '15 at 07:18
  • sys.version will also include architecture information of the python binary – Efren Oct 10 '16 at 04:39
  • @Efren, no, `sys.version` is not sufficient. It shows the compiler used to build Python but generally does not show the running architecture. For example, with OS X universal fat binaries, sys.version is identical for all architectures (32-bit, 64-bit) in the same file. – Ned Deily Oct 11 '16 at 01:44
  • Well, it does show it in windows > python -c "import sys;print(sys.version)" 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] > py -3.4 Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AM D64)] on win32 – Efren Oct 11 '16 at 04:07
  • 9
    On windows cmd, you need to put the double quotes on the outside and the single quotes on the inside or else it will produce a syntax error. That's probably because windows thinks spaces between single-quotes are still argument delimiters. It may be helpful to adjust this answer to accommodate that fact. – Steven Bluen Dec 28 '16 at 20:38
  • How distinguish between amd64 and ia64 on Windows? – ale5000 Sep 07 '17 at 22:04
  • 1
    I used `python -c "import sys;print('%x' % sys.maxsize, sys.maxsize > 2**32)"` which works on both Linux and Windows (which requires double quotes for the param) – biscuit314 Oct 13 '20 at 20:51
298

When starting the Python interpreter in the terminal/command line you may also see a line like:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32

Where [MSC v.1500 64 bit (AMD64)] means 64-bit Python. Works for my particular setup.

jchanger
  • 739
  • 10
  • 29
Dustin
  • 3,021
  • 1
  • 13
  • 2
  • 6
    so this is what? 64bit python or 32bit python? – phpJs Feb 28 '13 at 05:06
  • 10
    @phpJs 64 bit because of `[MSC v.1500 64 bit (AMD64)]` – Eduard Florinescu Aug 05 '13 at 12:34
  • 26
    Unfortunately only works for Windows versions of Python. My OSX installation returns `Python 2.7.8 (v2.7.8:ee879c0ffa11, Jun 29, 2014, 21:07:35) [GCC 4.2.1 (Apple In. build 5666) (dot 3)] on darwin` – aodj Aug 13 '14 at 16:12
  • 7
    On cygwin, you get this answer: `Python 2.7.8 (default, Jul 25 2014, 14:04:36) [GCC 4.8.3] on cygwin` – Jonno_FTW Nov 10 '14 at 04:15
  • Answered my question (Windows) which was I'm going to see that I'm on x64 hardware anyway. since I know that its 64-bit, the question I believe the OP had was is the interpreter the 32 or 64-bit console? –  Feb 08 '16 at 21:37
  • 3
    This info can be found out in code by calling `sys.version`. I get for example `('3.4.4 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:54:04) [MSC ' 'v.1600 64 bit (AMD64)]')` or `2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]` – otterb Mar 31 '16 at 15:50
  • @otterb wouldnt that return the system version and not necessarily the version of python that is installed and running. You could have windows 64 bit but may have installed the 32 bit python – Wesley Smith Nov 10 '16 at 01:45
  • 2
    The one-liner is `python -c "import sys; print(sys.version)"` – Slate Jan 13 '20 at 09:33
232

Basically a variant on Matthew Marshall's answer (with struct from the std.library):

import struct
print struct.calcsize("P") * 8
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
101

Try using ctypes to get the size of a void pointer:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

It'll be 4 for 32 bit or 8 for 64 bit.

Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90
Matthew Marshall
  • 5,793
  • 2
  • 21
  • 14
  • 1
    That works, too, although it does have the possible slight disadvantage of an unnecessary import and module load if you don't otherwise need ctypes: the sys module, otoh, is compiled into the interpreter. – Ned Deily Sep 10 '09 at 16:32
  • please help me understand: on my 64b installation `python -c 'import ctypes; print ctypes.sizeof(ctypes.c_voidp)'` returns **8**. Or should it be `python -c 'import ctypes; print ctypes.sizeof(ctypes.c_voidp) * 8'` ? – lukmdo Jul 14 '12 at 11:11
  • The function returns the size in [bytes](http://en.wikipedia.org/wiki/Byte) (4 or 8). If you need the size in [bits](http://en.wikipedia.org/wiki/Bit) (32 or 64) you have to multiply with 8. b_is_python_64bit = (ctypes.sizeof(ctypes.c_voidp) == 8) – phobie Sep 26 '12 at 12:44
  • 1
    Just call `python -c "import ctypes; print(32 if ctypes.sizeof(ctypes.c_voidp)==4 else 64, 'bit CPU')"` – vitiral Oct 27 '14 at 20:22
88

To paraphrase the answer at https://stackoverflow.com/a/1405971

Open python console:

import platform
platform.architecture()[0]

it should display the '64bit' or '32bit' according to your platform.

Alternatively( in case of OS X binaries ):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • patform module is not always reliable see docs https://docs.python.org/2/library/platform.html this is true also for some Windows application – G M Jun 13 '19 at 20:48
35

On my Centos Linux system I did the following:

1) Started the Python interpreter (I'm using 2.6.6)
2) Ran the following code:

import platform
print(platform.architecture())

and it gave me

(64bit, 'ELF')
itsmejoeeey
  • 302
  • 3
  • 11
rekabbnad
  • 389
  • 3
  • 2
19

Grouping everything...

Considering that:

  • The question is asked for OSX (I have an old (and cracked) VM with an ancient Python version)

  • My "main" env is Win

  • I only have the 032bit (Python) version installed on Win (and I built a "crippled" one on Linux (Ubuntu))

I'm going to exemplify on all 3 platforms, using Python 3 and Python 2.

1. Check [Python.Docs]: sys.maxsize

Compare value to 0x100000000 (4294967296 or 2 ** 32):

  • Greater for 064bit (pc064)

  • Smaller for 032bit (pc032)

Example:

  • OSX 9 pc064:

    • Python 2.7.10 pc064:

      >>> import sys
      >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
      'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \n[GCC 4.8.2] on darwin'
      >>> hex(sys.maxsize), sys.maxsize > 0x100000000
      ('0x7fffffffffffffff', True)
      
  • Ubuntu 16 pc064:

    • Python 3.5.2 pc064:

      >>> import sys
      >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
      'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \n[GCC 5.4.0 20160609] on linux'
      >>> hex(sys.maxsize), sys.maxsize > 0x100000000
      ('0x7fffffffffffffff', True)
      
    • Python 3.6.4 pc032:

      >>> import sys
      >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
      'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \n[GCC 5.4.0 20160609] on linux'
      >>> hex(sys.maxsize), sys.maxsize > 0x100000000
      ('0x7fffffff', False)
      
  • Win 10 pc064:

    • Python 3.5.4 pc064:

      >>> import sys
      >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
      'Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32'
      >>> hex(sys.maxsize), sys.maxsize > 0x100000000
      ('0x7fffffffffffffff', True)
      
    • Python 3.6.2 pc032:

      >>> import sys
      >>> "Python {:s} on {:s}".format(sys.version, sys.platform)
      'Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32'
      >>> hex(sys.maxsize), sys.maxsize > 0x100000000
      ('0x7fffffff', False)
      

2. Use [Python.Docs]: struct.calcsize(format)

Determine the object size produced by the (pointer) format. In other words, determines the pointer size (sizeof(void*)).

Example:

  • OSX 9 pc064:

    • Python 2.7.10 pc064:

      >>> import struct
      >>> struct.calcsize("P") * 8
      64
      
  • Ubuntu 16 pc064:

    • Python 3.5.2 pc064:

      >>> import struct
      >>> struct.calcsize("P") * 8
      64
      
    • Python 3.6.4 pc032:

      >>> import struct
      >>> struct.calcsize("P") * 8
      32
      
  • Win 10 pc064:

    • Python 3.5.4 pc064:

      >>> import struct
      >>> struct.calcsize("P") * 8
      64
      
    • Python 3.6.2 pc032:

      >>> import struct
      >>> struct.calcsize("P") * 8
      32
      

3. Use [Python.Docs]: ctypes - A foreign function library for Python

It also boils down to determining the size of a pointer (sizeof(void*)).

CTypes side notes:

Example:

  • OSX 9 pc064:

    • Python 2.7.10 pc064:

      >>> import ctypes
      >>> ctypes.sizeof(ctypes.c_void_p) * 8
      64
      
  • Ubuntu 16 pc064:

    • Python 3.5.2 pc064:

      >>> import ctypes
      >>> ctypes.sizeof(ctypes.c_void_p) * 8
      64
      
    • Python 3.6.4 pc032:

      >>> import ctypes
      >>> ctypes.sizeof(ctypes.c_void_p) * 8
      32
      
  • Win 10 pc064:

    • Python 3.5.4 pc064:

      >>> import ctypes
      >>> ctypes.sizeof(ctypes.c_void_p) * 8
      64
      
    • Python 3.6.2 pc032:

      >>> import ctypes
      >>> ctypes.sizeof(ctypes.c_void_p) * 8
      32
      

4. Check [Python.Docs]: platform.architecture(executable=sys.executable, bits='', linkage='')

!!! NOT reliable on OSX !!! due to multi arch executable (or .dylib) format (in some cases, uses #2.).

Example:

  • OSX 9 pc064:

    • Python 2.7.10 pc064:

      >>> import platform
      >>> platform.architecture()
      ('64bit', '')
      
  • Ubuntu 16 pc064:

    • Python 3.5.2 pc064:

      >>> import platform
      >>> platform.architecture()
      ('64bit', 'ELF')
      
    • Python 3.6.4 pc032:

      >>> import platform
      >>> platform.architecture()
      ('32bit', 'ELF')
      
  • Win 10 pc064:

    • Python 3.5.4 pc064:

      >>> import platform
      >>> platform.architecture()
      ('64bit', 'WindowsPE')
      
    • Python 3.6.2 pc032:

      >>> import platform
      >>> platform.architecture()
      ('32bit', 'WindowsPE')
      

5. Invoke an external command

Example:

  • OSX 9 pc064:

    • Python 2.7.10 pc064:

      >>> import os
      >>> os.system("file {:s}".format(os.path.realpath(sys.executable)))
      /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64
      
  • Ubuntu 16 pc064:

    • Python 3.5.2 pc064:

      >>> import os
      >>> os.system("file {:s}".format(os.path.realpath(sys.executable)))
      /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped
      
    • Python 3.6.4 pc032:

      >>> import os
      >>> os.system("file {:s}".format(os.path.realpath(sys.executable)))
      /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped
      
  • Win 10 pc064:

    • file utility is not present, there are other 3rd-party tools (Nix emulators: MSYS2, Cygwin, ...) that can be used, but I'm not going to insist on them

6. Check env vars

Example:

  • Win 10 pc064:

    • Python 3.5.4 pc064:

      >>> import os
      >>> os.environ["PROCESSOR_ARCHITECTURE"]
      'AMD64'
      
    • Python 3.6.2 pc032:

      >>> import os
      >>> os.environ["PROCESSOR_ARCHITECTURE"]
      'x86'
      

7. Check [Python.Docs]: sys.version

  • Also displayed in the 1st line when starting the interpreter

    • On some platforms, it might contain (textual) architecture information:

      Img0

  • Check #1.

CristiFati
  • 38,250
  • 9
  • 50
  • 87
17

For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
13

On Windows 10

Open the cmd termial and start python interpreter by typing >python as shown in the below image

Img

If the interpreter info at start contains AMD64, it's 64-bit, otherwise, 32-bit bit.

DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98
11

platform.architecture() notes say:

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:

import sys
is_64bits = sys.maxsize > 2**32
phoenix
  • 7,988
  • 6
  • 39
  • 45
Shannon Mann
  • 111
  • 1
  • 3
11

Do a python -VV in the command line. It should return the version.

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
user12549815
  • 175
  • 1
  • 2
9

Based on the top answer,

import sys
n_bits = 32 << bool(sys.maxsize >> 32)

n_bits will have 32 or 64 bits.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
7

struct.calcsize("P") returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.

So the following would return 32 if you're running 32-bit python and 64 if you're running 64-bit python:

Python 2

import struct;print struct.calcsize("P") * 8

Python 3

import struct;print(struct.calcsize("P") * 8)
kimbaudi
  • 13,655
  • 9
  • 62
  • 74
4
C:\Users\xyz>python

Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
32**
Type "help", "copyright", "credits" or "license" for more information.
>>>

after hitting python in cmd

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Nikhil
  • 300
  • 1
  • 5
  • 18
4
import sys
print(sys.version)

3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)]

betontalpfa
  • 3,454
  • 1
  • 33
  • 65
0

Platform Architecture is not the reliable way. Instead us:

$ 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)
Prakhar Agarwal
  • 2,724
  • 28
  • 31
0

platform.architecture() is problematic (and expensive).

Conveniently test for sys.maxsize > 2**32 since Py2.6 .

This is a reliable test for the actual (default) pointer size and compatible at least since Py2.3: struct.calcsize('P') == 8. Also: ctypes.sizeof(ctypes.c_void_p) == 8.

Notes: There can be builds with gcc option -mx32 or so, which are 64bit architecture applications, but use 32bit pointers as default (saving memory and speed). 'sys.maxsize = ssize_t' may not strictly represent the C pointer size (its usually 2**31 - 1 anyway). And there were/are systems which have different pointer sizes for code and data and it needs to be clarified what exactly is the purpose of discerning "32bit or 64bit mode?"

kxr
  • 4,841
  • 1
  • 49
  • 32