I'm having a problem in using python's plugin 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 code:

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.

I got this error:

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

解决方案

Try running following command on the terminal:

> whereis python

This will tell you where your python is installed. For example it could be /usr/local/bin for example. 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. 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:

easy_install -Z lxml

This will give you the source code of lxml package. And you can copy its code your source directory, and work further. But I would suggest this to be the last resort to try.

其他提示

Where is the lxml package installed?

The error suggests it is not on the PYTHONPATH when the python command is executed. You need to ensure lxml is on the 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. It might be worth qualifying the version of python you expect to use (and have lxml installed for):

system("python2.7 python.py")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top