lxml: Undefined variable etree

吃可爱长大的小学妹 提交于 2020-01-12 10:29:06

问题


I'm returning to Python after a little hiatus. Some projects that used to work now have a problem with lxml.

I have the latest source from github installed locally and have it in an Eclipse project.

This project has the following in PyDev-PYTHONPATH:
/${PROJECT_DIR_NAME}
/${PROJECT_DIR_NAME}/src

In a project that uses lxml, in the Project References, I have the lxml project checked.
A file in this project has:

import lxml

which is underlined in yellow with the warning:
Unused import: lxml

For this line:

from lxml import etree

it gives the error:
Unresolved import etree

A line like so:

kml = etree.Element("kml", nsmap = namespaces) 

has the error: Undefined variable etree

This project also has the following in PyDev-PYTHONPATH:
/${PROJECT_DIR_NAME}
/${PROJECT_DIR_NAME}/src

I've read this question, but I don't see an answer there:
Python 2.7 on Google App Engine, cannot use lxml.etree

On a Windows 10, 64 bit machine. This was not a problem on the same machine with Windows 7. Not sure if that is the problem. Wouldn't think so.

I found the value for PYTHONPATH in the Run Configuration. It has lxml:

D:\Program Files\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydev_sitecustomize;
D:\My Documents\eclipse\workspace2\StateDivision;
D:\My Documents\eclipse\workspace2\StateDivision\src;
C:\Python27\podbc64;
D:\My Documents\eclipse\workspace2\lxml;
D:\My Documents\eclipse\workspace2\lxml\src;
D:\My Documents\eclipse\workspace2\XlsxWriter;
C:\Python27\ArcGISx6410.3\DLLs;
C:\Python27\ArcGISx6410.3\lib;
C:\Python27\ArcGISx6410.3\lib\lib-tk;C:\Python27\ArcGISx6410.3;
C:\Python27\ArcGISx6410.3\lib\site-packages;
C:\Program Files (x86)\ArcGIS\Desktop10.3\bin64;
C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy;
C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcToolBox\Scripts

Cleaned up the project, switched to Python 3.5, PYTHONPATH now looks like this:

D:\Program Files\eclipse\plugins\org.python.pydev_3.9.2.201502050007\pysrc\pydev_sitecustomize;
D:\My Documents\eclipse\workspace2\StateDivision;
D:\My Documents\eclipse\workspace2\StateDivision\src;
D:\My Documents\eclipse\workspace2\lxml;
D:\My Documents\eclipse\workspace2\lxml\src;
D:\My Documents\eclipse\workspace2\lxml\src\lxml;
C:\Python35-32\DLLs;
C:\Python35-32\lib;
C:\Python35-32;C:\Python35-32\lib\site-packages

Still get same error with etree.

Edit:

Answer is to not use source (use python .exe installer for windows) and use from lxml import etree, not import lxml.


回答1:


You did not import the etree module as a global name in your module, only the lxml package itself. You need to import the etree module from the lxml package:

from lxml import etree

See the lxml.etree tutorial.

If import lxml works but from lxml import etree fails, you either have another lxml.py file in your path that masks the package, or you are trying to use a uncompiled source distribution. Use:

import lxml
print(lxml.__file__)

to find and rename the offending file.

  • If it points to lxml.py remove or rename that file.
  • If it points to <PATH>/src/lxml/__init__.py you are trying to use an uncompiled source distribution. You'll have to compile the Python extension code, or find a binary distribution for your platform to install.


来源:https://stackoverflow.com/questions/34617067/lxml-undefined-variable-etree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!