简体   繁体   中英

Force Python to be 32 bit on OS X Lion

I'm trying to use CPLEX within Python on Mac OS 10.7.5. CPLEX appears to only support a 32 bit python. I'm using this in a python shell to check if it's 32 bit:

import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32

I've tried these 2 commands as suggested in man 1 python, but neither seem to force 32 bit:

export VERSIONER_PYTHON_PREFER_32_BIT=yes
defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

The only thing that seems to work is this:

arch -i386 python

However, if I run a script using arch which calls other scripts, they all seem to start up in 64 bit mode. Is there another system wide variable to force it into 32 bit mode?

You can use the lipo command to create a copy of the Python interpreter with only i386 support.

:; file /usr/bin/python2.7
/usr/bin/python2.7: Mach-O universal binary with 2 architectures
/usr/bin/python2.7 (for architecture i386): Mach-O executable i386
/usr/bin/python2.7 (for architecture x86_64):   Mach-O 64-bit executable x86_64

:; lipo -thin i386 -output python-i386 /usr/bin/python2.7

:; file python-i386 
python-i386: Mach-O executable i386

:; ./python-i386
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Then arrange for your existing scripts to call python-i386 , or (if they use the /usr/bin/env trick) rename it to python and put it in a directory that's in your PATH somewhere before /usr/bin .

Note that looking at platform.architecture() or even platform.machine() doesn't actually tell you if the current process is 32-bit or 64-bit. Those always give 64-bit answers for me. But when I check in Activity Monitor, I can see that my stripped binary is not marked “(64-bit)”, while other processes are.

See the official word from Apple's developer library on how to select an interpreter on a per user or systemwide basis.

Sets a system version

% defaults write com.apple.versioner.python Version 2.7

Sets the default system version to 32 bit

% defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

USING A SPECIFIC VERSION

Rather than using the python command, one can use a specific version directly. For example, running python2.7 from the command line will run the 2.7 version of Python, independent of what the default version of Python is.

One can use a specific version of Python on the #! line of a script, but that may have portability and future compatibility issues.

Note that the preference files and environment variable that apply to the python command, do not apply when running a specific version of Python. In particular, running python2.6 will always default to 64-bit execution (unless one uses the arch(1) command to specifically select a 32-bit architecture).

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/python.1.html

After a huge amount of trial and error myself trying out some of the suggestions above, as well as here , I stumbled upon the following symlink:

/usr/local/bin/python2-32

pointing to:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2-32

And I observe that when I run this Python, it starts in 32-bit mode (in contrast to /Library/Frameworks/Python.framework/Versions/2.7/bin/python2 ). This can be observed in Activity Monitor.

Note: as others have pointed out elsewhere, platform.architecture() is not always a good indicator. It is showing '64-bit' for this 32-bit process.

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