0

The error is only happened in web. If I run it in console, it is OK.I am working in ubuntu.

I have setup apache2 using CGI for handle python and tried to displayed the python version in the code and can prove web and console both running on same python version and user.

But as long as import lxml.etree, I got the error below.

Traceback (most recent call last):
  File "/usr/lib/cgi-bin/TestPy.py", line 3, in <module>
    import lxml.etree as ET
ImportError: No module named lxml.etree

My CGI config is as below: Filename: serve-cgi-bin.conf

<IfDefine ENABLE_USR_LIB_CGI_BIN>
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Require all granted


        #AddHandler cgi-script .py
        #Order allow,deny
        #Allow from all

    </Directory>
</IfDefine>

My python code is as below:

#!/usr/bin/python

import lxml.etree as ET
import sys

print "Content-Type: text/plain\r\n\r\n"
print "Hello World!"

print "[" + sys.version + "]" + "  [" + sys.prefix + "]"
#xml_filename='/var/www/htmprint/Water.xml'
#xsl_filename='/var/www/htmprint/Water.xsl'
#dom = ET.parse(xml_filename)
#xslt = ET.parse(xsl_filename)
#transform = ET.XSLT(xslt)
#newdom = transform(dom)
#print(ET.tostring(newdom, pretty_print=True))

I got the feeling the permission isn't quite right for the lxml.etree file but I cannot figure out this.

Thanks in advance if anyone can help.

Bin

yangbin990
  • 83
  • 7
  • I also suspect permission problem. First, Apache runs CGI scripts as a different user — www-data on Debian/Ubuntu. Try to run your script at the command line under that user: `su - www-data -c TestPy.py`. – phd Jun 06 '17 at 13:14
  • Hi phd, tried as you said and www-data cannot run my TestPy.py. Could you suggest what I should do. Do I need grant www-data to access '/home/MYNAME/.local/lib/python2.7/site-packages/'? Thanks – yangbin990 Jun 07 '17 at 11:24
  • You better install the package to the global site-packages, not your .local which is your personal space. But if you want Apache to access .local — yes, `chmod -R a+rX /home/MYNAME/.local/lib/python2.7/site-packages; chmod a+rX /home/MYNAME/.local/lib/python2.7; chmod a+rx /home/MYNAME/.local/lib; chmod a+rx /home/MYNAME/.local; chmod a+rx /home/MYNAME; chmod a+rx /home` – phd Jun 07 '17 at 11:28
  • I have reinstalled lxml to global site-packages by using command 'sudo -H pip install lxml' and it is working now. Thanks a lot. Should you put this in reply and I could mark this as answer. – yangbin990 Jun 07 '17 at 22:02

1 Answers1

0

Install the package to the global site-packages, not your .local which is your personal space.

phd
  • 82,685
  • 13
  • 120
  • 165