设置Python Thrift模块时出错

1 投票
4 回答
3910 浏览
提问于 2025-04-15 20:03

我正在尝试设置thrift,目的是为了和Cassandra结合使用,所以当我运行了

setup.py

命令行输出了这个信息

running build
running build_py
running build_ext
building 'thrift.protocol.fastbinary' extension
C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python26\include -IC:\Pytho
n26\PC -c src/protocol/fastbinary.c -o build\temp.win32-2.6\Release\src\protocol
\fastbinary.o
src/protocol/fastbinary.c:24:24: netinet/in.h: No such file or directory
src/protocol/fastbinary.c:85:4: #error "Cannot determine endianness"
src/protocol/fastbinary.c: In function `writeI16':
src/protocol/fastbinary.c:295: warning: implicit declaration of function `htons'

src/protocol/fastbinary.c: In function `writeI32':
src/protocol/fastbinary.c:300: warning: implicit declaration of function `htonl'

src/protocol/fastbinary.c: In function `readI16':
src/protocol/fastbinary.c:688: warning: implicit declaration of function `ntohs'

src/protocol/fastbinary.c: In function `readI32':
src/protocol/fastbinary.c:696: warning: implicit declaration of function `ntohl'

error: command 'gcc' failed with exit status 1

我在这个问题上需要一些帮助。我已经安装了MigW32。

谢谢。

4 个回答

0

安装 python-dev

你可以运行以下命令:

sudo apt-get install python-dev

1

通过稍微调整源文件,可以在Windows上使用MINGW安装它。我使用的是thrift 0.9.1和Python 2.7。

我按照以下步骤操作:

  1. 如果你使用的是Python 2.7,按照正常的安装步骤和MINGW的解决办法进行。特别是你可能需要打开这个文件:C:\Python27\Lib\distutils\cygwinccompiler.py,然后修改Mingw32CCompiler这个类,去掉所有关于-mno-cygwin选项的内容。这个选项已经不再推荐使用,如果不去掉,会导致编译器出错并停止。

  2. 打开fastbinary.c文件,添加以下包含语句:

#include <stdbool.h>

这个语句是为了引入true和false的定义,否则编译时会出错。(我猜在MSVC中这些是默认包含的?)

3) 修改setup.py文件,告诉链接器要链接ws2_32.lib。这个在MSVC中是通过一个特殊的注释来实现的,但gcc不支持这个选项。所以你的ext_modules应该看起来像这样:

ext_modules = [
                 Extension('thrift.protocol.fastbinary',
                       sources = ['src/protocol/fastbinary.c'],
                       libraries=['ws2_32'],
                    include_dirs = include_dirs,
                )
            ],

4) 按照正常方式使用setup.py进行构建。

在我的环境中,使用C扩展而不是纯Python并没有带来太大的速度提升(大约5%的差别),所以除非在极端情况下,这样做的努力可能不太值得。

0

我只成功用MSVC安装了Thrift。

  • 首先安装MSVC
  • 然后获取Thrift
  • 接着应用thrift-252-python-msvc-1.diff补丁(可以在网上搜索一下)

fastbinary.c会被补丁修改,但setup.py的补丁可能会失败,你需要根据setup.py.rej里的提示手动更新,这里有一个(看起来)正确的版本:

from distutils.core import setup, Extension
import sys

libraries = []

if sys.platform == 'win32':
    libraries.append('ws2_32')

fastbinarymod = Extension('thrift.protocol.fastbinary',
                          sources = ['src/protocol/fastbinary.c'],
                          libraries = libraries, 
        )

setup(name = 'Thrift',
      version = '0.1',
      description = 'Thrift Python Libraries',
      author = 'Thrift Developers',
      author_email = 'thrift-dev@incubator.apache.org',
      url = 'http://incubator.apache.org/thrift/',
      license = 'Apache License 2.0',
      packages = [
        'thrift',
        'thrift.protocol',
        'thrift.transport',
        'thrift.server',
      ],
      package_dir = {'thrift' : 'src'},
      ext_modules = [fastbinarymod],
      )

Endian测试会失败,你需要修改fastbinary.c(大约在第68行):

#ifdef _MSC_VER
  #define __BYTE_ORDER __LITTLE_ENDIAN
#endif

之后运行 python setup.py install,希望你能得到你需要的东西。

撰写回答