使用 .py 文件中的 lxml 时遇到问题。它与口译员一起工作
我的 Snow Leopard 上安装了 python.org v2.7。还有一个 Apple 2.6 Python。我使用“sudo pip install lxml”命令 pip 安装了 lxml。我确保我是在 python.org 2.7 版本的 bin 目录中执行此操作的。
当我发出命令时:
from lxml import etree
在 IDLE 解释器中,一切都按预期工作,我可以继续使用所有 lxml 方法。但是,如果我用一行创建一个 .py 文件:
from lxml import etree
并且尝试运行它(无论是在 IDLE RUN/F5 中还是通过命令行 '$python testlxml.py'),我都会收到错误: ImportError: 无法导入名称 etree
如果我尝试导入其他 lxml 模块(例如“from lxml import html”),情况也是如此,它会在解释器中产生奇迹,但不会在 .py 文件中产生奇迹。我用谷歌搜索但找不到这个特定问题的解决方案。有人有线索吗?谢谢!
I have python.org v2.7 installed on my Snow Leopard. There's also a Apple 2.6 Python onboard. I pip installed lxml with the 'sudo pip install lxml' command. I made sure I was doing this from within the bin directory of my python.org 2.7 version.
When I give the command:
from lxml import etree
within the IDLE interpreter everything works as expected and I can go on an use all the lxml methods. However if I create a .py file with a single line:
from lxml import etree
and I try to run it (be it within IDLE RUN/F5 or via command line '$python testlxml.py') I get the error: ImportError: cannot import name etree
The same holds true if I try importing other lxml modules like 'from lxml import html', it will work wonders in the interpreter but it won't in a .py file. I googled around but could not find a solution to this specific issue. Anybody got a clue? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
终于明白了这一点。 PATH 中有一个名为 lxml.py 的文件,它不是 lxml 模块。实际上,我在几天前开始使用 lxml 教程时创建了该文件,并且没有注意文件名匹配。我完全忘记了。我傻了。
Finally figured this out. There was a file named lxml.py in the PATH which wasn't the lxml module. Actually, I created that file when I started playing with the lxml tutorial a couple days ago and didn't pay attention to the filename match. I totally forgot about it. Dumb me.
他们使用哪个口译员运行?
确保 py 文件的第一行是
Which interpreter are they running with?
Make sure the first line of your py file is