简体   繁体   English

没有使用php的python模块命名为lxml

[英]No module named lxml from python using php

I'm having a problem in using python's plugin lxml . 我在使用python的插件lxml时遇到问题。 When I execute python python.py it works in the server but when I use php system("python python.py") it goes blank. 当我执行python python.py它可以在服务器上运行,但是当我使用php system("python python.py")它将变为空白。

Python code: Python代码:

from lxml import etree

# create XML 
root = etree.Element('root')
root.append(etree.Element('child'))
# another child with text
child = etree.Element('child')
child.text = 'some text'
root.append(child)

# pretty string
s = etree.tostring(root, pretty_print=True)
print s

I used the tail -f /var/log/httpd/error_log to know what's happening to my php. 我使用了tail -f /var/log/httpd/error_log来了解我的PHP发生了什么。

I got this error: 我收到此错误:

Traceback (most recent call last):
File "xml.py", line 1, in ?
from lxml import etree
ImportError: No module named lxml

Where is the lxml package installed? lxml软件包安装在哪里?

The error suggests it is not on the PYTHONPATH when the python command is executed. 该错误表明执行python命令时它不在PYTHONPATH上。 You need to ensure lxml is on the PYTHONPATH. 您需要确保lxml在PYTHONPATH上。 This is most likely the case if installed in a non-standard location. 如果安装在非标准位置,则很可能是这种情况。

If lxml is installed in site-packages for Python 2.7 perhaps a mixture of python versions are being used. 如果在python 2.7的site-packages安装了lxml则可能正在使用混合的python版本。 It might be worth qualifying the version of python you expect to use (and have lxml installed for): 可能值得限定您期望使用的python版本(并安装了lxml ):

system("python2.7 python.py")

Try running following command on the terminal: 尝试在终端上运行以下命令:

> whereis python

This will tell you where your python is installed. 这将告诉您python的安装位置。 For example it could be /usr/local/bin for example. 例如,它可以是/ usr / local / bin。 Once you get the correct path you can refer complete path in the system command. 一旦获得正确的路径,就可以在系统命令中引用完整的路径。

If the above doesn't work, try doing this on terminal 如果以上方法均无效,请尝试在终端上执行此操作

python
>>> import lxml
>>> print lxml
<module 'lxml' from '<your_path>/site-packages/lxml-3.2.3-py2.7.egg/lxml/__init__.pyc'>

You should get a response similar to above. 您应该得到与上述类似的响应。 This will tell you from where you lxml package is being loaded. 这将告诉您从哪里装载lxml包。 Based on this path you can check what may be going wrong in loading this package. 根据此路径,您可以检查加载此软件包时可能出了什么问题。

If the above option doesn't work, you can uninstall the lxml package and then reinstall it with following option: 如果以上选项不起作用,则可以卸载lxml软件包,然后使用以下选项将其重新安装:

easy_install -Z lxml

This will give you the source code of lxml package. 这将为您提供lxml包的源代码。 And you can copy its code your source directory, and work further. 您可以将其代码复制到源目录中,然后继续工作。 But I would suggest this to be the last resort to try. 但是我建议这是最后尝试的方法。

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

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