Xcode 如何选择运行哪个 Python?

发布于 2024-10-07 10:35:15 字数 898 浏览 0 评论 0原文

使用 Xcode 的 Python 模板,我有一个简单的项目在一台机器上运行,但由于不同的原因在另外两台机器上运行失败。我解决的失败是,即使 main.m 有:

Py_SetProgramName("/usr/bin/python");

从 main.py 中看到的实际运行的 Python

import sys
print sys.version

是不同的。问题是另一个Python没有安装objc。所以问题是:在不清理我的机器的情况下,如何引导 Xcode 使用系统 Python?

编辑:

这是一个看似微不足道的问题,但事实并非如此。看起来确实如此,因为当我指定 Mac OS X 10.5 作为 Base SDK 时,Xcode 会链接到该 Python.framework,正如我通过双击 Xcode 项目中的“链接框架”中的图标可以看到的那样。

Python.framework 中的每个版本在其 Info.plist 中都有一个键/值对:CFBundleExecutableName/Python。幸运的是(为了取证)2.5 版本框架中的这个 Python 发布了 2.6.1!然而,通过打印 sys.version (来自 main.py)或通过监听加载程序(通过执行 export DYLD_PRINT_LIBRARIES=1),我可以看到 PyObjC 应用程序实际上启动了 Python 2.5.4。

上面的 Py_SetProgramName.. 行是无关紧要的——可以将其注释掉,一切正常。

所以看起来是 Python.framework 控制启动哪个 Python,它并没有按照我预期的方式执行此操作,并且它可能通过不透明的初始化例程进行控制,因为它是由 Apple 控制的。看来“控制”这种情况的最好方法是更改​​ Base SDK。

Using the Python templates for Xcode, I have a simple project running on one machine and failing on two others, for different reasons. The failure I solved is that even though main.m has:

Py_SetProgramName("/usr/bin/python");

The actual running Python as seen from main.py with this:

import sys
print sys.version

is different. And the problem was that this other Python didn't have objc installed. So the question is: short of cleaning up my machine, how can I direct Xcode to use the system Python?

Edit:

This is a problem that seems like it is trivial, but it isn't. It seems so because when I specify Mac OS X 10.5 as the Base SDK, Xcode links against that Python.framework, as I can see by double-clicking the icon in Linked Frameworks in the Xcode project.

Each Version in Python.framework has a key/value pair in its Info.plist: CFBundleExecutableName/Python. Luckily (for the forensics) this Python in the Version 2.5 framework launches 2.6.1! Yet by printing sys.version (from main.py) or by snooping on the loader (by doing export DYLD_PRINT_LIBRARIES=1), I can see that the PyObjC App actually launches Python 2.5.4.

The line above with Py_SetProgramName.. is irrelevant---it can be commented out and everything works fine.

So what it looks like is that Python.framework controls which Python is launched, it doesn't do this in the way I expected, and it might be controlled through an initialization routine that is opaque, since it's controlled by Apple. It seems the best way to "control" the situation is to change the Base SDK.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法言说的痛 2024-10-14 10:35:15

很抱歉回答我自己的问题,但我只是很困惑。这部分是一个错误

2.5 版框架中的 Python 发布 2.6.1

因为我没有意识到这里发生的情况(来自 Version/2.6 目录)

> ./Python
-bash: ./Python: cannot execute binary file
> Python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 

是我们正在搜索 $PATH 并启动 /opt/local/bin/python。
对于造成的混乱表示歉意。

总而言之,设置 SDK 应该可以让所需的 Python 运行,正如我们通过监听加载程序可以看到的那样:

> export DYLD_PRINT_LIBRARIES=1
> ~/Desktop/X/build/Debug/X.app/Contents/MacOS/X
dyld: loaded: /Users/telliott_admin/Desktop/X/build/Debug/X.app/Contents/MacOS/X
dyld: loaded: /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa
dyld: loaded: /System/Library/Frameworks/Python.framework/Versions/2.5/Python
..

Sorry to answer my own question but I was just confused. This part is an error

Python in the Version 2.5 framework launches 2.6.1

caused by my not realizing that what's going on here (from the Version/2.6 directory)

> ./Python
-bash: ./Python: cannot execute binary file
> Python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 

is that we're searching my $PATH and launching /opt/local/bin/python.
Apologies for the confusion.

To summarize, setting the SDK should get the desired Python to run as we can see by snooping on the loader:

> export DYLD_PRINT_LIBRARIES=1
> ~/Desktop/X/build/Debug/X.app/Contents/MacOS/X
dyld: loaded: /Users/telliott_admin/Desktop/X/build/Debug/X.app/Contents/MacOS/X
dyld: loaded: /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa
dyld: loaded: /System/Library/Frameworks/Python.framework/Versions/2.5/Python
..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文