标签归档:xcode

将Python嵌入iPhone应用程序

问题:将Python嵌入iPhone应用程序

因此,这是一个新的千年。苹果摇了摇手。现在在iPhone(App Store)应用程序中包含Python解释器是合法的。

如何做到这一点?现有的所有讨论(毫无疑问)都涉及越狱。(旧问题:我可以使用Python编写本机iPhone应用程序

我的目标不是编写PyObjC应用程序,而是编写将Python作为嵌入式库运行的常规ObjC应用程序。然后,Python代码将回调到本机可可代码。这是“控制逻辑就是Python代码”模式。

是否有使用XCode内置Python的指南,以便我的iPhone应用程序可以链接它?最好是精简的Python,因为我不需要90%的标准库。

我大概可以弄清楚线程和Python扩展API。我已经在MacOS上做到了。但仅使用命令行编译器,而不使用XCode。

So it’s a new millennium; Apple has waved their hand; it’s now legal to include a Python interpreter in an iPhone (App Store) app.

How does one go about doing this? All the existing discussion (unsurprisingly) refers to jailbreaking. (Older question: Can I write native iPhone apps using Python)

My goal here isn’t to write a PyObjC app, but to write a regular ObjC app that runs Python as an embedded library. The Python code will then call back to native Cocoa code. It’s the “control logic is Python code” pattern.

Is there a guide to getting Python built in XCode, so that my iPhone app can link it? Preferably a stripped-down Python, since I won’t need 90% of the standard library.

I can probably figure out the threading and Python-extension API; I’ve done that on MacOS. But only using command-line compilers, not XCode.


回答 0

如何构建Python其实并不重要-例如,您不需要在Xcode中构建它-但重要的是该构建的产品。

即,您将需要构建类似libPython.a的东西,该东西可以静态链接到您的应用程序中。有了.a后,可以将其添加到您的应用程序的Xcode项目中,然后从那里将其链接和签名,就像您应用程序的其余部分一样。

IIRC(自从我手工构建python以来已经有一段时间了),如果配置正确,现成的python将构建libPython.a(以及其他库)。

当然,您的第二个问题将是从86交叉编译适用于ARM的python盒。Python是一个基于autoconf的项目,而autoconf则是交叉编译的难题。

正如您正确指出的那样,减小尺寸至关重要。

同样,不足为奇的是您不是第一个想要这样做的人,但对于iOS而言却不是。Python已被挤入功能不如运行iOS的设备中。我在搜寻时发现了一个带有一堆链接的线程;这可能有用

另外,您可能想加入pyobjc-dev列表。虽然您的目标不是基于PyObjC的应用程序(顺便说一句,这是一个好主意-PyObjC在与iOS友好之前还有很长的路要走),但PyObjC社区一直在与Ronald讨论这个问题,在这个特定领域可能是最有知识的人。请注意,在移植PyObjC之前,PyObjC必须解决iOS上的嵌入式Python问题。他们的先决条件就是您的要求。

It doesn’t really matter how you build Python — you don’t need to build it in Xcode, for example — but what does matter is the product of that build.

Namely, you are going to need to build something like libPython.a that can be statically linked into your application. Once you have a .a, that can be added to the Xcode project for your application(s) and, from there, it’ll be linked and signed just like the rest of your app.

IIRC (it has been a while since I’ve built python by hand) the out-of-the-box python will build a libPython.a (and a bunch of other libraries), if you configure it correctly.

Of course, your second issue is going to be cross-compiling python for ARM from your 86 box. Python is an autoconf based project and autoconf is a pain in the butt for cross-compilation.

As you correctly state, making it small will be critical.

Not surprising, either, is that you aren’t the first person to want to do this, but not for iOS. Python has been squeezed into devices much less capable than those that run iOS. I found a thread with a bunch of links when googling about; it might be useful.

Also, you might want to join the pyobjc-dev list. While you aren’t targeting a PyObjC based application (which, btw, is a good idea — PyObjC has a long way to go before it’ll be iOS friendly), the PyObjC community has been discussing this and Ronald, of anyone, is probably the most knowledgeable person in this particular area. Note that PyObjC will have to solve the embedded Python on iOS problem prior to porting PyObjC. Their prerequisite is your requirement, as it were.


回答 1

我在github上放了一个非常粗糙的脚本,该脚本为iPhone和模拟器获取并构建python2.6.5。

http://github.com/cobbal/python-for-iphone

工作正在进行中

大约2年后的更新令人沮丧:(从README复制在github上)

这个项目从来没有真正让python在iPhone上运行过,我不满意,因此我不建议在此阶段将它用于任何严肃的项目。

最值得注意的是缺少pyobjc支持(事实证明,由于它依赖于更多平台特定的代码,因此很难移植到iPhone)

还缺少静态编译模块的能力(目前所有模块都以dylib形式构建,可用于开发,但据我所知App Store不允许这样做)

在这一点上,这个项目的主要目的是成为比我更聪明并且想要解决上述问题的任何人的起点。

我真的希望完全用Python编写应用程序是可行的,但目前看来这是不可能的。

I’ve put a very rough script up on github that fetches and builds python2.6.5 for iPhone and simulator.

http://github.com/cobbal/python-for-iphone

Work in progress

Somewhat depressing update nearly 2 years later: (copied from README on github)

This project never really got python running on the iPhone to my satisfaction, and I can’t recommend using it for any serious project at this stage.

Most notably missing is pyobjc support (which turns out to be much harder to port to iPhone since it relies on more platform-specific code)

Also missing is the ability to statically compile modules, (all are currently built as dylibs which works for development, but to my knowledge wouldn’t be allowed in the App Store)

At this point this project is mostly meant to be a starting point for anyone smarter than me who wants to and can tackle the above issues.

I really wish it were practical to write apps entirely in Python, but at this point it seems impossible.


回答 2

我也开始了这样的项目。它带有自己的简化编译脚本,因此无需弄乱autoconf即可获得交叉编译的静态库。它能够使用一些通用模块构建完全无依赖的Python静态库。它应该易于扩展。

https://github.com/albertz/python-embedded/

I also started such a project. It comes with its own simplified compile script so there is no need to mess around with autoconf to get your cross compiled static library. It is able to build a completely dependency-free static library of Python with some common modules. It should be easily extensible.

https://github.com/albertz/python-embedded/


Xcode 4+中的Python?

问题:Xcode 4+中的Python?

如何在Xcode 4、5、6或7中创建Python友好环境?

How does one create a Python friendly environment in Xcode 4, 5, 6 or 7?


回答 0

我想到了!这些步骤使它看起来比实际需要花费更多的精力。

这些说明用于从头开始创建项目。如果您希望将现有的Python脚本包含在此项目中,则显然需要稍微偏离这些说明。

如果您发现这些说明不再起作用或由于Xcode更新的更改而不清楚,请通知我。我将进行必要的更正。

  1. 打开Xcode。两者的说明相同。
  2. 在菜单栏中,单击“文件”→“新建”→“新建项目…”。
  3. 在左窗格中选择“其他”,然后在右页面中选择“外部构建系统”,然后单击“下一步”。
  4. 输入产品名称,组织名称或组织标识符。
  5. 在“构建工具”字段中,输入/usr/local/bin/python3Python 3或/usr/bin/pythonPython 2,然后单击“下一步”。请注意,这假设您具有解析为Python可执行文件的符号链接(默认情况下已设置)。如果不确定Python可执行文件的位置,请在Terminal:which python3和中输入以下命令之一which python
  6. 点击下一步”。
  7. 选择保存位置,然后单击“创建”。
  8. 在菜单栏中,单击“文件”→“新建”→“新建文件…”。
  9. 在“ OS X”下选择“其他”。
  10. 选择“空”,然后单击“下一步”。
  11. 导航到项目文件夹(否则它将不起作用),输入Python文件的名称(包括“ .py”扩展名),然后单击“创建”。
  12. 在菜单栏中,单击“产品”→“方案”→“编辑方案…”。
  13. 单击左窗格中的“运行”。
  14. 在“信息”选项卡中,单击“可执行文件”字段,然后单击“其他…”。
  15. 从步骤5导航到可执行文件。您可能需要使用 ⇧⌘G。如果目录是隐藏的,则来键入目录。
  16. 选择可执行文件,然后单击“选择”。
  17. 取消选中“调试可执行文件”。如果跳过此步骤,Xcode将尝试调试Python可执行文件本身。我不知道将外部调试工具集成到Xcode中的方法。
  18. 单击“启动时传递的参数”下的“ +”图标。您可能需要通过单击指向右侧的三角形来扩展该部分。
  19. 输入$(SRCROOT)/(或$(SOURCE_ROOT)/),然后输入您要测试的Python文件的名称。请记住,Python程序必须位于项目文件夹中。否则,您将必须在此处输入完整路径(或相对路径(如果位于项目文件夹的子文件夹中))。如果完整路径中的任何地方都有空格,则必须在此开头和结尾处都使用引号。
  20. 点击“关闭”。

请注意,如果在“显示文件检查器”选项卡处于活动状态的情况下打开“实用程序”面板,则文件类型将自动设置为“默认-Python脚本”。随意查看它具有的所有文件类型选项,以了解其所有功能。上面的方法可以应用于任何解释语言。到目前为止,我还没有弄清楚如何使它与Java一起工作。再说一次,我还没有做太多的研究。当然,网上有一些关于这一切的文件。

在没有管理特权的情况下运行:

如果您没有管理特权或不在Developer组中,则仍可以使用Xcode进行Python编程(但仍然无法使用需要编译的语言进行开发)。在菜单栏中单击“产品”→“执行操作”→“无需构建即可运行”,或仅使用键盘快捷键来代替播放按钮^⌘R

其他说明:

要更改文本编码,行尾和/或缩进设置,请打开“实用程序”面板,然后单击“显示文件检查器”选项卡。在这里,您将找到这些设置。

有关Xcode的构建设置的更多信息,没有比更好的资源了。我希望听到有人将其与不受支持的编译语言一起使用的信息。此过程应适用于任何其他解释语言。只需确保相应地更改了步骤5和步骤16。

I figured it out! The steps make it look like it will take more effort than it actually does.

These instructions are for creating a project from scratch. If you have existing Python scripts that you wish to include in this project, you will obviously need to slightly deviate from these instructions.

If you find that these instructions no longer work or are unclear due to changes in Xcode updates, please let me know. I will make the necessary corrections.

  1. Open Xcode. The instructions for either are the same.
  2. In the menu bar, click “File” → “New” → “New Project…”.
  3. Select “Other” in the left pane, then “External Build System” in the right page, and next click “Next”.
  4. Enter the product name, organization name, or organization identifier.
  5. For the “Build Tool” field, type in /usr/local/bin/python3 for Python 3 or /usr/bin/python for Python 2 and then click “Next”. Note that this assumes you have the symbolic link (that is setup by default) that resolves to the Python executable. If you are unsure as to where your Python executables are, enter either of these commands into Terminal: which python3 and which python.
  6. Click “Next”.
  7. Choose where to save it and click “Create”.
  8. In the menu bar, click “File” → “New” → “New File…”.
  9. Select “Other” under “OS X”.
  10. Select “Empty” and click “Next”.
  11. Navigate to the project folder (it will not work, otherwise), enter the name of the Python file (including the “.py” extension), and click “Create”.
  12. In the menu bar, click “Product” → “Scheme” → “Edit Scheme…”.
  13. Click “Run” in the left pane.
  14. In the “Info” tab, click the “Executable” field and then click “Other…”.
  15. Navigate to the executable from Step 5. You might need to use ⇧⌘G to type in the directory if it is hidden.
  16. Select the executable and click “Choose”.
  17. Uncheck “Debug executable”. If you skip this step, Xcode will try to debug the Python executable itself. I am unaware of a way to integrate an external debugging tool into Xcode.
  18. Click the “+” icon under “Arguments Passed On Launch”. You might have to expand that section by clicking on the triangle pointing to the right.
  19. Type in $(SRCROOT)/ (or $(SOURCE_ROOT)/) and then the name of the Python file you want to test. Remember, the Python program must be in the project folder. Otherwise, you will have to type out the full path (or relative path if it’s in a subfolder of the project folder) here. If there are spaces anywhere in the full path, you must include quotation marks at the beginning and end of this.
  20. Click “Close”.

Note that if you open the “Utilities” panel, with the “Show the File inspector” tab active, the file type is automatically set to “Default – Python script”. Feel free to look through all the file type options it has, to gain an idea as to what all it is capable of doing. The method above can be applied to any interpreted language. As of right now, I have yet to figure out exactly how to get it to work with Java; then again, I haven’t done too much research. Surely there is some documentation floating around on the web about all of this.

Running without administrative privileges:

If you do not have administrative privileges or are not in the Developer group, you can still use Xcode for Python programming (but you still won’t be able to develop in languages that require compiling). Instead of using the play button, in the menu bar, click “Product” → “Perform Action” → “Run Without Building” or simply use the keyboard shortcut ^⌘R.

Other Notes:

To change the text encoding, line endings, and/or indentation settings, open the “Utilities” panel and click “Show the File inspector” tab active. There, you will find these settings.

For more information about Xcode’s build settings, there is no better source than this. I’d be interested in hearing from somebody who got this to work with unsupported compiled languages. This process should work for any other interpreted language. Just be sure to change Step 5 and Step 16 accordingly.


回答 1

我创建了Xcode 4模板来简化Tyler提供的步骤。
结果是Xcode 4的Python项目模板。

现在,您需要做的就是下载模板,将其移动到/Developer/Library/Xcode/Templates/Project Templates/Mac/Others/,然后使用Xcode 4新建一个Python项目。

它仍然需要手动配置方案(您可以参考Tyler提供的步骤12–20 。)

I’ve created Xcode 4 templates to simplify the steps provided by Tyler.
The result is Python Project Template for Xcode 4.

Now what you need to do is download the templates, move it to /Developer/Library/Xcode/Templates/Project Templates/Mac/Others/ and then new a Python project with Xcode 4.

It still needs manual Scheme setup (you can refer to steps 12–20 provided by Tyler.)


回答 2

使Python在XCode 7中工作的过程

步骤1:使用外部构建系统设置项目

步骤1.1:编辑项目计划

步骤2:将 Python指定为项目的可执行文件(shift-command-g),路径应为/ usr / bin / python

步骤3:指定您的自定义工作目录

步骤4:将命令行参数指定为python文件的名称。(在此示例中为“ test.py”)

步骤5:谢天谢地,就是这样!

(在OSX支持python调试器之前无法添加调试功能?)

Procedure to get Python Working in XCode 7

Step 1: Setup your Project with a External Build System

Step 1.1: Edit the Project Scheme

Step 2: Specify Python as the executable for the project (shift-command-g) the path should be /usr/bin/python

Step 3: Specify your custom working directory

Step 4: Specify your command line arguments to be the name of your python file. (in this example “test.py”)

Step 5: Thankfully thats it!

(debugging can’t be added until OSX supports a python debugger?)


回答 3

您应该尝试使用Eclipse的PyDev插件。我尝试了很多与python一起使用的编辑器/ IDE,但是我最喜欢的唯一一个是Eclipse的PyDev插件。它具有代码完成,调试器和许多其他不错的功能。加上两者都是免费的。

You should try PyDev plug in for Eclipse. I tried alot of editors/IDE’s to use with python, but the only one i liked the most is the PyDev plugin for Eclipse. It has code completion, debugger and many other nice features. Plus both are free.


回答 4

我在XCode3中使用了一段时间的另一种方法是:

请参阅上面的步骤1-15。

  1. 选择/ bin / bash作为可执行文件
  2. 对于“调试器”字段,选择“无”。
  3. 在“参数”选项卡中,单击“基础扩展为”字段,然后选择您之前创建的目标。
  4. 单击“启动时传递的参数”下的“ +”图标。您可能需要通过单击指向右侧的三角形来扩展该部分。
  5. 输入“ -l”。这将告诉bash使用您的登录环境(PYTHONPATH等)。
  6. 再次执行步骤19。
  7. 输入“ -c’$(SOURCE_ROOT)/。py’”
  8. 点击“确定”。
  9. 开始编码。

这种方式的好处是,它将使用与在XCode外部运行(使用bash .profile进行设置)相同的环境进行开发。

它也足够通用,可以让您开发/运行任何类型的文件,而不仅仅是python。

Another way, which I’ve been using for awhile in XCode3:

See steps 1-15 above.

  1. Choose /bin/bash as your executable
  2. For the “Debugger” field, select “None”.
  3. In the “Arguments” tab, click the “Base Expansions On” field and select the target you created earlier.
  4. Click the “+” icon under “Arguments Passed On Launch”. You may have to expand that section by clicking on the triangle pointing to the right.
  5. Type in “-l”. This will tell bash to use your login environment (PYTHONPATH, etc..)
  6. Do step #19 again.
  7. Type in “-c ‘$(SOURCE_ROOT)/.py'”
  8. Click “OK”.
  9. Start coding.

The nice thing about this way is it will use the same environment to develop in that you would use to run in outside of XCode (as setup from your bash .profile).

It’s also generic enough to let you develop/run any type of file, not just python.


回答 5

Apple Developer Library的技术说明TN2328对我使用Xcode 5.0嵌入Python 所做的更改提供了很多帮助。

This Technical Note TN2328 from Apple Developer Library helped me a lot about Changes To Embedding Python Using Xcode 5.0.


回答 6

该线程很旧,但为了配合Xcode版本8.3.3,Tyler Crompton在接受的答案中的方法仍然有效(某些名称稍有不同,但无足轻重)。

我稍微挣扎的2点:

第16步:如果所需的python可执行文件显示为灰色,请右键单击它,然后选择快速查找。然后关闭快速查看窗口,现在它应该是可选的。

步骤19:如果这不适合您,则可以在“参数”选项卡中仅输入python文件的名称,然后在“工作目录”下的“选项”选项卡中明确输入项目根目录-选中“使用自定义工作方式目录”框,然后在其下方的字段中输入您的项目根目录。

This thread is old, but to chime in for Xcode Version 8.3.3, Tyler Crompton’s method in the accepted answer still works (some of the names are very slightly different, but not enough to matter).

2 points where I struggled slightly:

Step 16: If the python executable you want is greyed out, right click it and select quick look. Then close the quick look window, and it should now be selectable.

Step 19: If this isn’t working for you, you can enter the name of just the python file in the Arguments tab, and then enter the project root directory explicitly in the Options tab under Working Directory–check the “Use custom working directory” box, and type in your project root directory in the field below it.


回答 7

试用Editra它是免费的,具有许多很酷的功能和插件,可以在大多数平台上运行,并且是用Python编写的。我在家中以及在工作于Windows / Linux的所有非XCode开发中都使用它。

Try Editra It’s free, has a lot of cool features and plug-ins, it runs on most platforms, and it is written in Python. I use it for all my non-XCode development at home and on Windows/Linux at work.


无法在Mac OS X 10.9上安装Lxml

问题:无法在Mac OS X 10.9上安装Lxml

我想安装Lxml,以便随后可以安装Scrapy。

今天更新Mac时,不允许我重新安装lxml,但出现以下错误:

In file included from src/lxml/lxml.etree.c:314:
/private/tmp/pip_build_root/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
#include "libxml/xmlversion.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

我尝试使用brew安装libxml2和libxslt,两者都安装良好,但仍然无法安装lxml。

上次安装时,我需要在Xcode上启用开发人员工具,但是由于将其更新为Xcode 5,因此不再提供该选项。

有人知道我需要做什么吗?

I want to install Lxml so I can then install Scrapy.

When I updated my Mac today it wouldn’t let me reinstall lxml, I get the following error:

In file included from src/lxml/lxml.etree.c:314:
/private/tmp/pip_build_root/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
#include "libxml/xmlversion.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

I have tried using brew to install libxml2 and libxslt, both installed fine but I still cannot install lxml.

Last time I was installing I needed to enable the developer tools on Xcode but since its updated to Xcode 5 it doesnt give me that option anymore.

Does anyone know what I need to do?


回答 0

您应该为xcode安装或升级命令行工具。在终端上尝试一下:

xcode-select --install

You should install or upgrade the commandline tool for xcode. Try this in a terminal:

xcode-select --install

回答 1

我通过安装和链接libxml2以及libxslt通过brew 在优胜美地上解决了这个问题:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

如果使用此方法解决了问题,但稍后又弹出,则可能需要以上四行之前运行它:

brew unlink libxml2
brew unlink libxslt

如果您在Homebrew上遇到权限错误,尤其是在El Capitan上,则这是一个有用的文档。本质上,无论OS X是什么版本,都请尝试运行:

sudo chown -R $(whoami):admin /usr/local

I solved this issue on Yosemite by both installing and linking libxml2 and libxslt through brew:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

If you have solved the problem using this method but it pops up again at a later time, you might need to run this before the four lines above:

brew unlink libxml2
brew unlink libxslt

If you are having permission errors with Homebrew, especially on El Capitan, this is a helpful document. In essence, regardless of OS X version, try running:

sudo chown -R $(whoami):admin /usr/local

回答 2

您可以通过在命令行上运行此命令来解决问题:

 STATIC_DEPS=true pip install lxml

它肯定帮助了我。文档说明

You may solve your problem by running this on the commandline:

 STATIC_DEPS=true pip install lxml

It sure helped me. Explanations on docs


回答 3

我尝试了上面的大多数解决方案,但没有一个对我有用。我正在运行Yosemite 10.10,唯一适用于我的解决方案是在终端中键入以下内容:

sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

编辑:如果您使用virtualenv,则不需要开头的sudo。

I tried most of the solutions above, but none of them worked for me. I’m running Yosemite 10.10, the only solution that worked for me was to type this in the terminal:

sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

EDIT: If you are using virtualenv, the sudo in beginning is not needed.


回答 4

这也困扰了我一段时间。我对python distutils等的内部知识不了解,但是这里的include路径是错误的。我进行了以下丑陋的修改,直到python lxml人员可以进行适当的修复为止。

sudo ln -s  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2/libxml/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml

This has been bothering me as well for a while. I don’t know the internals enough about python distutils etc, but the include path here is wrong. I made the following ugly hack to hold me over until the python lxml people can do the proper fix.

sudo ln -s  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2/libxml/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml

回答 5

全局安装… OS X 10.9.2

xcode-select --install
sudo easy_install pip
sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

Installing globally… OS X 10.9.2

xcode-select --install
sudo easy_install pip
sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

回答 6

http://lxml.de/installation.html上的安装说明说明:

为了加快测试环境(例如,在持续集成服务器上)的构建速度,请通过设置CFLAGS环境变量来禁用C编译器优化:

CFLAGS="-O0" pip install lxml

instalation instructions on http://lxml.de/installation.html explain:

To speed up the build in test environments, e.g. on a continuous integration server, disable the C compiler optimisations by setting the CFLAGS environment variable:

CFLAGS="-O0" pip install lxml

回答 7

上面的这些在10.9.2上都不适合我,因为编译因以下错误而失败:

clang: error: unknown argument: '-mno-fused-madd' 

这实际上导致最干净的解决方案(请参见[1]中的更多详细信息):

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip install lxml

或跟随如果全球安装

sudo pip install lxml

[1] lang错误:未知参数:’-mno-fused-madd’(python软件包安装失败)

None of the above worked for me on 10.9.2, as compilation bails out with following error:

clang: error: unknown argument: '-mno-fused-madd' 

Which actually lead to cleanest solution (see more details in [1]):

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip install lxml

or following if installing globally

sudo pip install lxml

[1] clang error: unknown argument: ‘-mno-fused-madd’ (python package installation failure)


回答 8

xcode-select --install
sudo easy_install pip
sudo pip install lxml
xcode-select --install
sudo easy_install pip
sudo pip install lxml

回答 9

Yosemite通过运行以下命令解决了这个问题:

xcode-select install #this may take several minutes.
pip install lxml

I solved this issue on Yosemite by running the following commands:

xcode-select install #this may take several minutes.
pip install lxml

回答 10

对于自制软件,libxml2被隐藏以不干扰系统libxml2,因此必须对pip有所帮助才能找到它。

用bash:

LDFLAGS=-L`brew --prefix libxml2`/lib CPPFLAGS=-I`brew --prefix libxml2`/include/libxml2 pip install --user lxml

与鱼:

env LDFLAGS=-L(brew --prefix libxml2)/lib CPPFLAGS=-I(brew --prefix libxml2)/include/libxml2 pip install --user lxml

With homebrew, libxml2 is hidden to not interfere with the system libxml2, so pip must be helped a little in order to find it.

With bash:

LDFLAGS=-L`brew --prefix libxml2`/lib CPPFLAGS=-I`brew --prefix libxml2`/include/libxml2 pip install --user lxml

With fish:

env LDFLAGS=-L(brew --prefix libxml2)/lib CPPFLAGS=-I(brew --prefix libxml2)/include/libxml2 pip install --user lxml

回答 11

OSX 10.9.2

sudo env ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future STATIC_DEPS=true pip install lxml

OSX 10.9.2

sudo env ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future STATIC_DEPS=true pip install lxml

回答 12

我尝试了此页面上的所有答案,但没有一个对我有用。我正在运行OS X版本10.9.2

但这绝对有效….就像一个魅力:

ARCHFLAGS = -Wno-error =未使用的命令行参数-将来的硬错误pip install lxml

I tried all the answers on this page, none of them worked for me. I’m running OS X Version 10.9.2

But this definitely works….like a charm:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install lxml


回答 13

不幸的是xcode-select --install,由于我已经拥有最新版本,因此不适用于我。

这很奇怪,但是我通过打开XCode并接受条款与条件解决了这个问题。重新运行pip install lxml后未返回任何错误。

Unfortunately xcode-select --install did not work for me as I already had the latest version.

It’s very strange but I solved the issue by opening XCode and accepting the Terms & Conditions. Re-running pip install lxml returned no errors after.


回答 14

从pip(lxml 3.6.4)成功安装后,导入lxml.etree模块时出现错误。

我一直在不停地搜索以将其安装为scrapy的必要条件,并尝试了所有选项,但最终这对我有用(mac osx 10.11 python 2.7):

$ STATIC_DEPS=true sudo easy_install-2.7 "lxml==2.3.5"

较旧的lxml版本似乎可以与etree模块一起使用。

Pip通常可以忽略软件包的指定版本,例如,当您在pip缓存中有较新的版本时,即为easy_install。该'-2.7'选项适用于python版本,如果要安装python 3.x,则忽略此选项。

After successful install from pip (lxml 3.6.4) I was getting an error when importing the lxml.etree module.

I was searching endlessly to install this as a requisite for scrapy, and tried all the options, but finally this worked for me (mac osx 10.11 python 2.7):

$ STATIC_DEPS=true sudo easy_install-2.7 "lxml==2.3.5"

The older version of lxml seem to work with etree module.

Pip can often ignore the specified version of a package, for example when you have the newer version in the pip cache, thus the easy_install. The '-2.7' option is for python version, omit this if you are installing for python 3.x.


回答 15

就我而言,在通过以下方式安装lxml之前,必须关闭Kaspersky Antivirus

pip install lxml

In my case, I must shutdown Kaspersky Antivirus before installing lxml by:

pip install lxml

回答 16

我正在使用OSX 10.9.2,但出现相同的错误。

XCode命令行工具的安装不适用于此特定版本的OSX。

我认为解决此问题的更好方法是使用以下命令进行安装:

$ CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 pip install lxml

这类似于jdkoftinoff的修复程序,但不会永久更改系统。

I am using OSX 10.9.2 and I get the same error.

Installation of the XCode command line tools does not help for this particular version of OSX.

I think a better approach to fix this is to install with the following command:

$ CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 pip install lxml

This is similar to jdkoftinoff’ fix, but does not alter your system in a permanent way.


回答 17

我遇到了同样的问题,经过几天的工作,我在OS X 10.9.4上使用Python 3.4.1解决了这个问题。

这是我的解决方案,

根据从lxml.de 安装lxml

lxml的macport可用。尝试类似port install py25-lxml的操作

如果您没有MacPort,请从MacPort.org安装它。这很容易。您可能还需要编译器来安装XCode编译工具,使用xcode-select --install

首先,我通过将该端口更新为最新版本sudo port selfupdate

然后我输入sudo port install libxml2,几分钟后,您应该看到libxml2安装成功。可能您可能还需要libxslt安装lxml。要安装libxslt,请使用:sudo port install libxslt

现在,只需键入pip install lxml,它应该可以正常工作。

I met the same question and after days of working I resolved this problem on my OS X 10.9.4, with Python 3.4.1.

Here’s my solution,

According to installing lxml from lxml.de,

A macport of lxml is available. Try something like port install py25-lxml

If you do not have MacPort, install it from MacPort.org. It’s quite easy. You may also need a compiler, to install XCode compiling tools, use xcode-select --install

Firstly I updated my port to the latest version via sudo port selfupdate,

Then I just type sudo port install libxml2 and several minutes later you should see libxml2 installed successfully. Probably you may also need libxslt to install lxml. To install libxslt, use:sudo port install libxslt.

Now, just type pip install lxml, it should work fine.


回答 18

在编译之前,将xmlversion.h的路径添加到您的环境中。

$ set INCLUDE=$INCLUDE:/private/tmp/pip_build_root/lxml/src/lxml/

但是请确保我提供的路径中包含xmlversion.h文件。然后,

$ python setup.py install

before compiling add the path that to xmlversion.h into your environment.

$ set INCLUDE=$INCLUDE:/private/tmp/pip_build_root/lxml/src/lxml/

But make sure the path I’ve provided has the xmlversion.h file located inside. Then,

$ python setup.py install

回答 19

pip没有为我工作。我去了 https://pypi.python.org/pypi/lxml/2.3 并下载了macosx .egg文件:

https://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-macosx-10.6-intel.egg#md5=52322e4698d68800c6b6aedb0dbe5f34

然后使用命令行easy_install安装.egg文件。

pip did not work for me. I went to https://pypi.python.org/pypi/lxml/2.3 and downloaded the macosx .egg file:

https://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-macosx-10.6-intel.egg#md5=52322e4698d68800c6b6aedb0dbe5f34

Then used command line easy_install to install the .egg file.


回答 20

这篇文章链接到一个适用于我的解决方案,在Mac OS X 10.9上适用于 Python3,lxml和“未找到符号:_lzma_auto_decoder”

hth


回答 21

在头发多撕裂和咬牙切齿之后,我用pip卸载了xcode并运行:

easy_install lxml

一切都很好。

After much tearing of the hair and gnashing of the teeth, I uninstalled xcode with pip and ran:

easy_install lxml

And all was well.


回答 22

尝试:

% STATIC_DEPS=true pip install lxml

要么:

% STATIC_DEPS=true sudo pip install lxml

有用!

Try:

% STATIC_DEPS=true pip install lxml

Or:

% STATIC_DEPS=true sudo pip install lxml

It works!