简体   繁体   中英

Enforce the use of 32-bit python

I am having some trouble triggering the 32-bit python for my macports python2.7

calvins-MacBook ttys003 Tue Nov 01 01:04:23 |~|
calvin$ arch -arch x86_64 python
Python 2.7.2 (default, Oct 31 2011, 20:10:35) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform; platform.architecture()
('64bit', '')
>>> exit()
calvins-MacBook ttys003 Tue Nov 01 01:04:49 |~|
calvin$ arch -arch i386 python
Python 2.7.2 (default, Oct 31 2011, 20:10:35) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform; platform.architecture()
('64bit', '')
>>> 

How should I go about triggering the use of 32-bit python?

arch -i386 python

Will run the binary in 32-bit mode (which is what you did).

If you installed Python via MacPorts, check that it was actually built with 32-bit and 64-bit (universal binary).

file `which python`

This is output on mine:

λ > file /usr/local/bin/python
/usr/local/bin/python: Mach-O universal binary with 2 architectures
/usr/local/bin/python (for architecture i386):  Mach-O executable i386
/usr/local/bin/python (for architecture x86_64):    Mach-O 64-bit executable x86_64

If you don't see the i386 , then you your version doesn't have a 32-bit build.

Though if you can run arch -i386 python you should be fine, since you will get an error if your binary can't run 32-bit mode.


Also, do not rely on platform.architecture() to tell you if it's 32-bit, because universal binaries will report 64-bit mode even if you're in 32-bit. It's better to rely on sys.maxsize , which changes depending on if you're in 32-bit or 64-bit mode.

Python in 32-bit mode, notice sys.maxsize > 2**32 :

λ > arch -i386 python
Python 2.7.2 (default, Oct 31 2011, 00:51:07) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.maxsize > 2**32
False
>>> sys.maxsize
2147483647
>>> import platform
>>> platform.architecture()
('64bit', '')

Python in 64-bit mode:

λ > python
Python 2.7.2 (default, Oct 31 2011, 00:51:07) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.maxsize
9223372036854775807
>>> sys.maxsize > 2**32
True
>>> import platform
>>> platform.architecture()
('64bit', '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM