lxml:未定义的变量etree [英] lxml: Undefined variable etree

查看:251
本文介绍了lxml:未定义的变量etree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

稍作中断后,我将返回Python.现在某些以前可以正常工作的项目在lxml上有问题.

我从本地安装了来自github的最新源,并将其保存在Eclipse项目中. /p>

此项目在PyDev-PYTHONPATH中具有以下内容:
/$ {PROJECT_DIR_NAME}
/$ {PROJECT_DIR_NAME}/src

在使用lxml的项目中,在项目参考"中,我检查了lxml项目.
该项目中的文件具有:

import lxml

,带有黄色下划线并带有警告:
未使用的导入:lxml

对于此行:

from lxml import etree

它给出了错误:
未解决的导入etree

这样的一行:

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

具有错误:未定义的变量etree

此项目在PyDev-PYTHONPATH中也具有以下内容:
/$ {PROJECT_DIR_NAME}
/$ {PROJECT_DIR_NAME}/src

我已经阅读了这个问题,但是我没有看到答案:
Google App Engine上的Python 2.7,不能使用lxml .etree

在Windows 10 64位计算机上.在Windows 7所在的同一台计算机上,这不是问题.不确定是否是问题所在.不会这样.

我在运行配置中找到了PYTHONPATH的值.它具有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

清理项目,切换到Python 3.5,PYTHONPATH现在看起来像这样:

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

etree仍然出现相同的错误.

答案是不要使用源(对于Windows使用python .exe安装程序),请使用from lxml import etree,而不是import lxml.

解决方案

您没有在模块中将etree模块作为全局名称导入,仅导入了lxml包本身.您需要从lxml包中导入etree模块:

from lxml import etree

请参见 lxml.etree教程.

如果import lxml正常工作,但from lxml import etree失败,则您的路径中还有另一个lxml.py文件,该文件会屏蔽软件包,您正尝试使用未编译的源发行版.使用:

import lxml
print(lxml.__file__)

查找并重命名有问题的文件.

  • 如果它指向lxml.py,请删除或重命名该文件.
  • 如果它指向<PATH>/src/lxml/__init__.py,则您正在尝试使用未编译的源发行版.您将必须编译Python扩展代码,或找到要安装的平台的二进制发行版.

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.

解决方案

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.

这篇关于lxml:未定义的变量etree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆