已安装lxml,但无法在Windows上运行

时间:2015-07-21 18:46:52

标签: python windows pip lxml

我需要针对XSD验证XML文件。为此,我想使用lxml library。问题是即使我有from lxml import etree并已将lxml安装到C:\Python33\Lib\site-packages\lxml\,我收到错误

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

为什么会这样,我该如何解决?我尝试将C:\Python33\Lib\site-packages\lxml\添加到PATH变量,但它没有帮助。我用PIP安装了lxml。

更新:当我通过交互式终端运行脚本时(即在cmd中键入python),它可以import lxml

这是一个简单的脚本

from lxml import etree

def main():
    print('hi')


if __name__ == "__main__":
    main()

在cmd我做

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()

但是,如果我尝试运行它

> ImportLxml.py

然后我得到

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

以下是PATH环境变量

中的所有pythonic条目
C:\Python33\;
C:\Python33\Scripts;
C:\Python33\Lib\site-packages\lxml\
%ARCGISINSALLDIR%\arcpy;

1 个答案:

答案 0 :(得分:2)

Python Launcher for Windows配置为默认使用3.3.5:

py -3

或者 - 假设您选择在上次安装Python时安装Python启动器 - begin your script with a shebang which the Python Launcher will recognize as requesting Python 3

#! python3

如果您在安装Python 3.3时决定不安装Python Launcher for Windows,请参阅the install documentation了解手动步骤:

  

将正确的文件组与.py脚本关联:

assoc .py=Python.File
     

将所有Python文件重定向到新的可执行文件:

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

这可用于为您选择的Python解释器配置Python.File的类型,即。对于3.3.5。 (作为一个良好做法,Python.File应指向py.exepython.exe;上面的pythonw.exe示例是文档的直接引用,但仍然是一个不好的做法)。

或者,如果您在磁盘上有py.exe(使用Python 3.3安装)但未使用它,则可以稍微修改这些说明:

  

将正确的文件组与.py脚本关联:

assoc .py=Python.File
     

将所有Python文件重定向到新的可执行文件:

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

...再次,调整路径以适合安装Python 3.3.x的位置。

相关问题