【问题标题】:No module named thrift in Python scriptPython 脚本中没有名为 thrift 的模块
【发布时间】:2014-11-11 15:04:50
【问题描述】:

我已经使用 Homebrew 在我的 Mac 上安装了 Thrift:brew install thrift --with-python

这做了一些工作并完成了,没有报告错误。我的道路上有节俭。

我根据教程编写了一个简单的python客户端:(gen-py中有一些python节俭代码)

#!/usr/bin/python

import sys
sys.path.append("./gen-py")

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

# rest of program...

运行时出现此错误:

Traceback (most recent call last):
  File "./hey.py", line 8, in <module>
    from thrift import Thrift
ImportError: No module named thrift

我是否使用 Homebrew 正确安装了 python?有没有办法验证 python/thrift 集成是否正确安装?

【问题讨论】:

  • 我不是 python 人,但您是否也包含了 Thrift 库代码?分两部分,gen-py中生成的代码和库代码。

标签: python homebrew thrift


【解决方案1】:

事实证明(在撰写本文时当前)用于 Thrift 安装的 Homebrew 配方并没有完全安装 Python 的所有内容,特别是库。 (它确实成功安装了 thrift 命令行工具。)

我通过下载 python 发行版并在我的 Mac 上构建它来解决这个问题——但由于我已经完成了 brew 安装,所以没有安装。我只想要python库。

我听从了这里的建议:http://thrift-tutorial.readthedocs.org/en/latest/usage-example.html 我按照描述运行了“sudo python setup.py install”,这正确(显然)安装了所需的库,之后一切正常。

【讨论】:

  • 您能在 Thrift 邮件列表中提出这个问题吗?也许已经用 0.9.2 解决了,但我不确定。谢谢!
  • 仍然遇到同样的问题
【解决方案2】:

我在 macOS Mojave 10.14.1 上也遇到了这个问题。原因似乎是--with-python 将在其中安装python@2thrift 模块。但是,您使用的系统pythonpython@2 不同。您可以通过brew list | grep python 验证这一点,您应该会看到以下结果。

python
python@2

删除python@2 (brew uninstall --ignore-dependencies python@2) 并从源代码安装 python 模块后应该可以工作。

git clone https://github.com/apache/thrift.git
cd thrift/lib/py
sudo python setup.py install

详细原因可以参考this post

【讨论】:

    【解决方案3】:

    删除 python@2 (brew uninstall --ignore-dependencies python@2) 对我不起作用。其实我不得不重装brew install python@2

    pip install thrift 为我工作。

    我的系统是 OSX Mojave 和 python 2.7。 brew list | grep python 有以下结果: python python@2

    【讨论】:

      【解决方案4】:

      我没有使用 Mac / Homebrew,但这是我解决此问题的方法:

      我使用的是 Linux,我的 Thrift 服务器在 PyCharm 中运行良好,但从命令行运行时出现 ModuleNotFoundError: No module named 'thrift' 错误。

      事实证明,虽然我在我的系统上安装了 Thrift,但我并没有使用 Pip 安装它。一旦我跑了:

      pip install thrift

      ...一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 2014-02-11
        • 1970-01-01
        • 2021-05-11
        • 2018-03-07
        • 1970-01-01
        • 2020-01-30
        相关资源
        最近更新 更多