简体   繁体   English

如何在Snow Leopard和其他32位/ 64位问题上强制Python为32位

[英]How do I force Python to be 32-bit on Snow Leopard and other 32-bit/64-bit questions

When I run the following from a bash shell on my Mac: 当我从Mac上的bash shell运行以下代码时:

$ file /usr/bin/python

I get the following three lines: 我得到以下三行:

/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

this would seem to indicate that Python has been compiled for all three architectures or something like that? 这似乎表明Python已经针对所有三种架构进行了编译或类似的东西? I believe, based on some errors I had while trying to setup MySQL, that the version I'm using is the 64-bit version. 我相信,基于我在尝试设置MySQL时遇到的一些错误,我使用的版本是64位版本。 So two questions: 所以有两个问题:

  1. How would I have known that? 我怎么知道的?

  2. How could I change Python to be 32-bit instead? 我怎么能把Python变成32位呢? Something less drastic than re-compile with different compile settings? 用不同的编译设置重新编译的东西不那么激烈?

  3. Why does arch from a bash shell return i386 which would seem to indicate I'm not in "64-bit mode" when I know based on my processor I'm running a 64-bit Mac? 为什么来自bash shell的arch返回i386 ,这似乎表明我不是在“64位模式”,当我知道基于我的处理器我运行的是64位Mac?

Sorry these are probably all newbie questions, the whole 32/64-bit thing is frustrating the crap out of me and I'm sure there are some commands/tools that would make this easier. 对不起,这些可能都是新手问题,整个32/64位的东西令我感到沮丧,我确信有一些命令/工具可以让这更容易。

  1. You can find out a lot about the Python version you're running via the platform module (the sys module also has a few simple helpers) 你可以通过platform模块找到很多关于你运行的Python版本( sys模块也有一些简单的帮助器)

  2. On Mac OS X, you can run a "fat binary" with your chosen architecture with, for example, 在Mac OS X上,您可以使用所选的架构运行“胖二进制”,例如,

    arch -i386 /usr/bin/python arch -i386 / usr / bin / python

I do not recommend altering /usr/lib/python itself (with the lipo command) -- you could easily make your system unusable by tampering with system files. 建议改变/ usr / lib / python本身(使用lipo命令) - 你可以通过篡改系统文件轻松地使你的系统无法使用。 Maybe installing a separate Python from python.org (for application purposes) while leaving the system Python alone is an acceptable strategy to you -- it's definitely safer than altering system files!-) 也许从python.org安装一个单独的Python(用于应用程序)而单独留下系统Python是一个可接受的策略 - 它肯定比改变系统文件更安全! - )

As for your third question, hmmm, this one's a stumper to me -- and definitely a question for superuser.com (as well as completely unrelated to Python, it also seems completely unrelated to programming;-). 至于你的第三个问题,嗯,这个对我来说是一个难题 - 对于superuser.com来说绝对是一个问题(以及与Python完全无关,它似乎与编程完全无关;-)。

http://www.jaharmi.com/2009/08/29/python_32_bit_execution_on_snow_leopard

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

Fix for use with virtualenv on Snow Leopard 修复与Snow Leopard上的virtualenv一起使用

danielrsmith's answer works for me when I am not using virtualenv, but virtualenv makes a copy of the python executable which causes it not to work: 当我不使用virtualenv时,danielrsmith的回答对我有用,但是virtualenv制作了python可执行文件的副本,导致它无法工作:

$ which python
/Users/cogg/.virtualenvs/tweakeats/bin/python

$ python
[...]
>>> import sys
>>> sys.maxint
9223372036854775807

because this is a copy of python, I used lipo on it to remove the 64-bit architecture. 因为这是python的副本,我在它上面使用了lipo来删除64位架构。 This allows me to use 32-bit python 2.6 with virtualenv: 这允许我使用带有virtualenv的32位python 2.6:

$ lipo -info /Users/cogg/.virtualenvs/tweakeats/bin/python
Architectures in the fat file: /Users/cogg/.virtualenvs/tweakeats/bin/python are: x86_64 i386 ppc7400
$ mv /Users/cogg/.virtualenvs/tweakeats/bin/python /Users/cogg/.virtualenvs/tweakeats/bin/python.old
$ lipo -remove x86_64 /Users/cogg/.virtualenvs/tweakeats/bin/python.old -output /Users/cogg/.virtualenvs/tweakeats/bin/python
$ python
[...]
>>> import sys
>>> sys.maxint
2147483647

The answer has been accepted. 答案已被接受。 But I think the 3rd question is still unanswered. 但我认为第三个问题仍然没有答案。 I found it to be an interesting question and so did some research for the same. 我发现它是一个有趣的问题,所以也做了一些研究。 Here is the answer I found in another SO forum - 以下是我在另一个SO论坛中找到的答案 -

SnowLeopard runs 32-bit system software and applications on 32-bit Intel machines, and 64-bit system software and 32-bit and 64-bit applications on 64-bit Intel machines. SnowLeopard在32位Intel机器上运行32位系统软件和应用程序,在64位Intel机器上运行64位系统软件和32位和64位应用程序。 The desktop version of Snow Leopard boots a 32-bit kernel for reasons of kext and driver compatibility, but all user space runs 64-bit. 由于kext和驱动程序兼容性,Snow Leopard的桌面版本引导了32位内核,但所有用户空间都运行64位。 Mac OS X Server boots into a 64-bit kernel. Mac OS X Server启动到64位内核。 Core 2 Duo is a 64-bit machine. Core 2 Duo是一款64位机器。 Try $ sysctl hw.cpu64bit_capable or sysctl hw.optional.x86_64 to verify that you have a 64-bit cpu. 尝试使用$ sysctl hw.cpu64bit_capable或sysctl hw.optional.x86_64来验证您是否拥有64位cpu。 arch will always show i386 on Intel hardware in both Leopard and Snow Leopard. arch将始终在Leopard和Snow Leopard的英特尔硬件上显示i386。 – cdespinosa Sep 22 '09 at 4:23 - cdespinosa 09年9月22日4:23

Original link - Mac OSX 10.6 compiler: a puzzling experience with 32 vs 64 bit 原始链接 - Mac OSX 10.6编译器:32位与64位的令人费解的体验

the third question has to do with the 32 / 64 bit kernel 第三个问题与32/64位内核有关

just because you have a 64 bit capable OS and 64 bit capable CPU processor does not mean you are booting up a 64 bit kernel in Mac OSX 仅仅因为你有一个64位的操作系统和64位的CPU处理器并不意味着你在Mac OSX中启动64位内核

If you got a Mac with 10.6 snow leopard on it from the factory, then it shipped in 32 bit mode by default 如果你从工厂买到带有10.6雪豹的Mac,那么它默认以32位模式运送

you would have to edit the system setting to boot 64 bit mode 您必须编辑系统设置以启动64位模式

(not sure on changes to 10.7 or 10.8 as the latest I use is snow leo but I believe they may boot 64 bit kernel by factory-default) (不确定更改为10.7或10.8,因为我最近使用的是雪利奥,但我相信他们可能会按出厂默认启动64位内核)

10.6 - from terminal run the command: 10.6 - 从终端运行命令:

 :*sudo nvram boot-args="arch=x86_64"

OR 要么

10.6 - from terminal run the command: 10.6 - 从终端运行命令:

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.Boot 'Kernel Flags' 'arch=x86_64'

I tried many different ways of executing the Skype4Py test script using, 我尝试了许多不同的方法来执行Skype4Py测试脚本,

arch -i386

What worked in the end was looking at the segfault path for the claimed 64-bit version of python and putting that in my header file: #!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python 最后工作的是查看声称的64位python版本的段错误路径并将其放在我的头文件中:#!/ System / Library / Frameworks / Python.framework / Versions / 2.7 / Resources / Python.app /内容/ MacOS的/ Python的

After I put this line at the top and added execute permissions to my file, I could use, arch -i386 skype.py 将此行放在顶部并为我的文件添加执行权限后,我可以使用arch -i386 skype.py

And it would run without issue. 它会毫无问题地运行。

After a huge amount of trial and error myself trying out some of the suggestions above, 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 ). 我观察到当我运行这个Python时,它以32位模式启动(与/Library/Frameworks/Python.framework/Versions/2.7/bin/python2相反)。 This can be observed in Activity Monitor. 这可以在Activity Monitor中观察到。

Note: as others have pointed out elsewhere, platform.architecture() is not always a good indicator. 注意:正如其他人在其他地方指出的那样, platform.architecture()并不总是一个很好的指标。 It is showing '64-bit' for this 32-bit process. 它为这个32位进程显示'64位'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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