Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml: Undefined variable etree

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.

like image 942
Al Lelopath Avatar asked Jan 29 '26 21:01

Al Lelopath


1 Answers

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.
like image 118
Martijn Pieters Avatar answered Feb 03 '26 06:02

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!