Python 导入错误:没有名为 lxml 的模块 - 即使安装了 LXML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27008222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:18:27  来源:igfitidea点击:

ImportError: No module named lxml - Even though LXML Is installed

pythonamazonlxmlamazon-product-api

提问by Michael Romrell

I'm getting this error "ImportError: No module named lxml" Even though LXML Is definitely installed. Specifically it's installed within the python Virtualenv for the project. and ultimately I'm working on the Python/Amazon Product API. I get the error after trying to run one of the example scripts for that project from the terminal (mac).

我收到此错误“ ImportError: No module named lxml”,即使 LXML 确实已安装。具体来说,它安装在项目的 python Virtualenv 中。最终我正在研究Python/Amazon Product API。尝试从终端 (mac) 运行该项目的示例脚本之一后,出现错误。

How can I fix this? or further track down the issue?

我怎样才能解决这个问题?或进一步追查问题?

Google searching lead me to:

谷歌搜索引导我:

  • Reintsall LXML
  • Ensure Xcode license was agreed to: sudo xcodebuild -license
  • Updating LXML with: pip install --upgrade lxmlCurrently at ver 3.4.0
  • reinstalled LXML dependencies as outlined here- pip install libxml2-dev libxslt-dev python-dev
  • 重新安装 LXML
  • 确保同意 Xcode 许可: sudo xcodebuild -license
  • 使用以下内容更新 LXML:pip install --upgrade lxml当前版本为 3.4.0
  • 重新安装LXML依赖关系概述这里-pip install libxml2-dev libxslt-dev python-dev

Edit: More detailThe line of code throwing the error is from lxml import etree, objectify, this is in a folder far away from where LXML is installed in my virtual environment (although I am running the script from within my virtualenv in terminal). If the issues is simply a matter of the script not knowing where to find the LXML install, how would I fix that?

编辑:更多细节抛出错误的代码行是from lxml import etree, objectify,它位于远离我的虚拟环境中安装 LXML 的文件夹中(尽管我在终端的 virtualenv 中运行脚本)。如果问题只是脚本不知道在哪里可以找到 LXML 安装的问题,我该如何解决?

采纳答案by Michael Romrell

It turns out that I found my answer here: import lxml fails on OSX after (seemingly) successful install

事实证明,我在这里找到了我的答案:在(看似)成功安装后,在 OSX 上导入 lxml 失败

Apparently, LXML Install on a Mac ends up installing it in the wrong path, so you have to reset the path using: export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"

显然,在 Mac 上安装 LXML 最终会将其安装在错误的路径中,因此您必须使用以下方法重置路径: export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"

回答by Andrew Lazarus

So it turns out that if you're using python via homebrew, things get a little sketchy with apps like Inkscape that have it hardcoded in their mind that a certain version of python is needed.

所以事实证明,如果你通过自制软件使用 python,那么像 Inkscape 这样的应用程序在他们的脑海中硬编码需要某个版本的 python,事情就会变得有点粗略。

Getting the right python version was simply uninstalling brew & installing PIP:

获得正确的 python 版本只需卸载 brew 并安装 PIP:

brew uninstall --ignore-dependencies python

brew uninstall --ignore-dependencies python

sudo easy_install pip

sudo easy_install pip

STATIC_DEPS=true sudo pip install lxml

STATIC_DEPS=true sudo pip install lxml

回答by jerome wong

if your python is python 3.6, try pip3.6 install lxml

如果您的 python 是 python 3.6,请尝试 pip3.6 install lxml