简体   繁体   中英

lxml installed, but not working on Windows

I need to validate an XML file against an XSD. To accomplish this I would like to use the lxml library . The problem is that even though I have from lxml import etree and have installed lxml to C:\\Python33\\Lib\\site-packages\\lxml\\ , I'm getting the error

Traceback (most recent call last):
  File "C:\Users\asmithe\Documents\dev1\TestParse.py", line 3, in <module>
    from lxml import etree as ET_l
ImportError: No module named lxml

Why is this and how do I fix it? I tried adding C:\\Python33\\Lib\\site-packages\\lxml\\ to the PATH variable and it didn't help. I had installed lxml using PIP.

UPDATE: When I run the script through the interactive terminal (ie typing python in cmd) it CAN import lxml

Here is a simple script

from lxml import etree

def main():
    print('hi')


if __name__ == "__main__":
    main()

In cmd I do

C:\Users\dev1\Documents\test>python
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>>
>>> def main():
...     print('hi')
...
>>>
>>> if __name__ == "__main__":
...     main()
...
hi
>>> exit()

However if I try to run it

> ImportLxml.py

then I get

C:\Users\dev1\Documents\test>ImportLxml.py
Traceback (most recent call last):
  File "C:\Users\dev1\Documents\XML test\TestImport.py", line 1, in <module>

    from lxml import etree
ImportError: No module named lxml

Here are all of the pythonic entries in the PATH environment variable

C:\Python33\;
C:\Python33\Scripts;
C:\Python33\Lib\site-packages\lxml\
%ARCGISINSALLDIR%\arcpy;

Configure the Python Launcher for Windows to use 3.3.5 as default:

py -3

Alternately -- assuming you chose to install the Python Launcher when last installing Python -- begin your script with a shebang which the Python Launcher will recognize as requesting Python 3 :

#! python3

If you decided not to install the Python Launcher for Windows when installing Python 3.3, see the install documentation for manual steps:

Associate the correct file group with .py scripts:

 assoc .py=Python.File 

Redirect all Python files to the new executable:

 ftype Python.File=C:\\Path\\to\\pythonw.exe "%1" %* 

This can be used to configure the type for Python.File to the Python interpreter of your choice, ie. that for 3.3.5. (As a matter of good practices, Python.File should be pointed at py.exe or python.exe ; the pythonw.exe example above is a direct quote from the docs, but a bad practice nonetheless).


Alternately, if you have a py.exe on disk (installed with Python 3.3) but it isn't being used, you can modify those instructions a bit:

Associate the correct file group with .py scripts:

 assoc .py=Python.File 

Redirect all Python files to the new executable:

 ftype Python.File=C:\\Path\\to\\py.exe "%1" %* 

...again, adjusting the path to be appropriate for where you installed Python 3.3.x.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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