get python bit version as in (32 or 64)

Ned Deily nad at acm.org
Sun Dec 19 19:52:14 EST 2010


In article <nad-84A6B3.18002019102010 at news.gmane.org>,
 Ned Deily <nad at acm.org> wrote:
> In article 
> <AANLkTimuzUByj7R8MBZPSFAS0ComoBACGt8cVFySquvg at mail.gmail.com>,
>  Vincent Davis <vincent at vincentdavis.net> wrote:
> > On Tue, Oct 19, 2010 at 3:55 PM, Philip Semanchuk <philip at semanchuk.com> 
> > wrote:
> > > On Oct 19, 2010, at 5:38 PM, Hexamorph wrote:
> > >> On 19.10.2010 23:18, Vincent Davis wrote:
> > >>> How do I get the bit version of the installed python. In my case, osx
> > >>> python2.7 binary installed. I know it runs 64 bt as I can see it in
> > >>> activity monitor. but how do I ask python?
> > >>> sys.version
> > >>> '2.7 (r27:82508, Jul  3 2010, 21:12:11) \n[GCC 4.0.1 (Apple Inc. build 
> > >>> 5493)]'
> > >>>
> > >>
> > >> In [1]: import platform
> > >>
> > >> In [2]: platform.architecture()
> > >> Out[2]: ('32bit', 'ELF')
> > >>
> > >> In [3]:
> > >
> > >
> > > Looks a lot better than my suggestion!
> 
> It looks better but, unfortunately, it doesn't work correctly on OS X 
> where a universal build can have both 32-bit and 64-bit executables in 
> the same file.
> 
> $ arch -x86_64 /usr/local/bin/python2.7 -c 'import sys,platform; 
> print(sys.maxint,platform.architecture())'
> (9223372036854775807, ('64bit', ''))
> $ arch -i386 /usr/local/bin/python2.7 -c 'import sys,platform; 
> print(sys.maxint,platform.architecture())'
> (2147483647, ('64bit', ''))
> 
> At the moment, the sys.maxint trick is the simplest reliable test for 
> Python 2 on OS X.  For Python 3, substitute sys.maxsize.

For the record, I've supplied a patch to fix platform.architecture() for 
OS X universal builds: http://bugs.python.org/issue10735

The sys.maxsize (or sys.maxint prior to Python 2.6) test is still a 
better cross-platform choice.

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list