使用 .py 文件中的 lxml 时遇到问题。它与口译员一起工作

发布于 2024-10-05 19:47:51 字数 554 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-10-12 19:47:51

终于明白了这一点。 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.

路还长,别太狂 2024-10-12 19:47:51

他们使用哪个口译员运行?

确保 py 文件的第一行是

#!/full/path/to/python2.7

Which interpreter are they running with?

Make sure the first line of your py file is

#!/full/path/to/python2.7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文