【问题标题】:Running tox with C extension module使用 C 扩展模块运行 tox
【发布时间】:2019-08-09 08:39:07
【问题描述】:

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

我的模块可以用 python setup.py build 很好地编译。但是,“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

【问题讨论】:

  • 你直接使用了python setup.py build,它(AFAIK)和python setup.py bdist_wheel不一样,你试过直接运行吗?
  • "python setup.py bdist_wheel" 也有效。按照 hoefling 的建议删除“changedir={envdir}”解决了我的问题。

标签: python tox


【解决方案1】:

删除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(...)

【讨论】:

  • 非常感谢!有效。没想到这样一个非主流问题会得到快速回复。
  • 很高兴能帮上忙!
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
相关资源
最近更新 更多