运行 带有 C 扩展模块的 tox

Running tox with C extension module

我是 tox 的新手。我有一个 python 包,其中有一个为 python2.7 编写的 C 扩展模块。网络告诉我,在这种情况下我需要使用 "tox --wheel"。但是,我无法通过 "tox --wheel" 找到我的 C 源代码。有人可以帮我吗?

我的模块可以通过 python setup.py 构建正常编译。但是,"tox --wheel" 中的 gcc 抱怨找不到我的 C 源代码。缺少什么配置?非常感谢!

我的 setup.py 看起来像这样:

import os
del os.link
from setuptools import setup, Extension, find_packages

module1 = Extension('_HelloWorld_py',
                    sources = ['HelloWorld.c'],
                    include_dirs = ['/usr/lib64/python2.7/site-packages/numpy/core/include/'],
                    extra_compile_args = ['-std=c99','-Wno-error=vla'])

setup (name = 'HelloWorld',
       version = '0.1',
       description = 'Hello World',
       package_data={'': ['*.h', '*.c']},
       ext_modules = [module1])

我的 tox.ini 看起来像这样:

[tox]
envlist =
  py27

[testenv]
wheel=true
basepython=py27: python2.7
deps=
  pytest
  numpy
changedir={envdir}
commands=
  {envpython} {toxinidir}/runtests.py --mode=full {posargs:}

错误消息如下所示:

py27 run-test-pre: PYTHONHASHSEED='2083224069'
py27 run-test: commands[1] | /home/user/helloworld-release/.tox/py27/bin/python /home/user/helloworld-release/setup.py bdist_wheel
running bdist_wheel
running build
running build_ext
building '_HelloWorld_py' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/lib64/python2.7/site-packages/numpy/core/include/ -I/usr/include/python2.7 -c HelloWorld.c -o build/temp.linux-x86_64-2.7/HelloWorld.o -std=c99 -Wno-error=vla
gcc: error: HelloWorld.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
error: command 'gcc' failed with exit status 4
ERROR: InvocationError for command /home/user/helloworld-release/.tox/py27/bin/python /home/user/helloworld-release/setup.py bdist_wheel (exited with code 1)
ERROR: No distributions found in the dist directory found. Please check setup.py, e.g with:
     python setup.py bdist_wheel

删除changedir={envdir}。它将当前目录更改为 .tox/py27,因此安装脚本中的 hello.c 将解析为不存在的 .tox/py27/hello.c

如果需要保持工作目录不变,可以在安装脚本中使用绝对路径:

import os
from setuptools import setup, Extension, find_packages


rootdir = os.path.normpath(os.path.join(__file__, os.pardir))
module1 = Extension('_HelloWorld_py',
                    sources = [os.path.join(rootdir, 'HelloWorld.c')],
                    ...)

setup(...)