gpt4 book ai didi

cython - 如何在 Cython 中正确包含头文件(setup.py)?

转载 作者:行者123 更新时间:2023-12-02 07:26:10 28 4
gpt4 key购买 nike

如何在包 my_math 中正确包含 my_math/clog.h 以使 tox 传递 py .test 套件?

<小时/>

项目文件结构:

my_math/__init__.py
my_math/clog.h
my_math/clog.pxd
my_math/integrate.pyx
setup.py
tests/__init__.py
tests/test_log.py
tox.ini

my_math/clog.h

#include <python.h>
#include <math.h>

#if PY_VERSION_HEX < 0x3050000 && (defined(_WIN32) || defined(_WIN64))
// Calculates log2 of number
double log2(double n);
{
// log(n)/log(2) is log2
double log_2 = 0.693147180559945309417232121458176568075500134360255254120680;
return log(n) / log_2;
}
#endif

my_math/integrate.pyx

def extern from "clog.h":
double log2(double)

def call_log2(n):
return log2(n)

设置.py

import os

from setuptools import setup, Extension
from setuptools.dist import Distribution
from Cython.Distutils import build_ext

Distribution(dict(setup_requires='Cython'))

ext_modules = [
Extension("my_math.integrate", ["my_math/integrate.pyx"],
include_dirs=['my_math'], # path to .h file(s)
library_dirs=['my_math'], # path to .a or .so file(s)
# libraries=['clog'],
extra_compile_args = ['-I/usr/local/include',
'-L/usr/local/lib'],
depends=['my_math/clog.h']
)
]

setup(
name='my_math',
package_dir={'my_math': 'my_math'},
packages=['my_math'],
include_package_data=True,
package_data={'': ['*.pyx', '*.pxd', '*.h', '*.c']},
cmdclass = {'build_ext': build_ext},
ext_modules=ext_modules
)

测试/test_log.py

from pytest import approx

from my_math.integrate import call_log2

def test_call_log2():
assert approx(call_log2(100)) == 6.64386

毒物.ini

[tox]
envlist = py36

[testenv]
deps = cython
pytest
commands = python setup.py build_ext --inplace
py.test tests

my_math/__init__.pytests/__init__.py 为空。

<小时/>

我运行python3 setup.py build_ext --inplace,没有任何故障。

我可以直接调用函数call_log2:

python3 -c 'from my_math.integrate import call_log2; print(call_log2(100))'
6.643856189774724

但不适用于tox:

clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Imy_math -I/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c my_math/integrate.c -o build/temp.macosx-10.11-x86_64-3.6/my_math/integrate.o -I/usr/local/include -L/usr/local/lib
clang: warning: argument unused during compilation: '-L/usr/local/lib'
my_math/integrate.c:432:10: fatal error: 'clog.h' file not found
#include "clog.h"
^
1 error generated.
error: command 'clang' failed with exit status 1

如果我取消注释#libraries = ['clog'],一个新的python3 setup.py build_ext --inplace将产生一个ld:找不到库对于 -lclog 错误。

最佳答案

通过更正的MANIFEST.in解决:

全局包含 *.txt *.rst *.pyx *.pxd *.c *.h

setup.py:

'': ['data/*', '*.pyx', '*.pxd', '*.c', '*.h']

MANIFEST.insetup.py 必须涵盖所有文件模式以支持 bdistsdist

相关:How to include package data with setuptools/distribute?

关于cython - 如何在 Cython 中正确包含头文件(setup.py)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com