最近我开始研究Cython,Anaconda发行版有这个方便的脚本cythonize
允许这样做
>cythonize -i foo.pyx
来编译文件。
我的问题是我如何使用该脚本来包含numpy以便cimport numpy
正常工作?
我试过
>cythonize -X include_path=C:\Anaconda3\lib\site-packages\numpy\core\include -i foo.pyx
>cythonize -s include_path=C:\Anaconda3\lib\site-packages\numpy\core\include -i foo.pyx
同时使用include_dir=...
,include_dirs=...
同时使用-X和-s
如果我使用-X,我会
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\cythonize-script.py", line 5, in <module>
sys.exit(Cython.Build.Cythonize.main())
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 185, in main
options, paths = parse_args(args)
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 172, in parse_args
options, args = parser.parse_args(args)
File "C:\Anaconda3\lib\optparse.py", line 1387, in parse_args
stop = self._process_args(largs, rargs, values)
File "C:\Anaconda3\lib\optparse.py", line 1431, in _process_args
self._process_short_opts(rargs, values)
File "C:\Anaconda3\lib\optparse.py", line 1536, in _process_short_opts
option.process(opt, value, values, self)
File "C:\Anaconda3\lib\optparse.py", line 785, in process
self.action, self.dest, opt, value, values, parser)
File "C:\Anaconda3\lib\optparse.py", line 805, in take_action
self.callback(self, opt, value, parser, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 38, in parse_directives
value, relaxed_bool=True, current_settings=old_directives)
File "C:\Anaconda3\lib\site-packages\Cython\Compiler\Options.py", line 424, in parse_directive_list
raise ValueError('Unknown option: "%s"' % name)
ValueError: Unknown option: "include_path"
用-s得到
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\cythonize-script.py", line 5, in <module>
sys.exit(Cython.Build.Cythonize.main())
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 196, in main
cython_compile(path, options)
File "C:\Anaconda3\lib\site-packages\Cython\Build\Cythonize.py", line 90, in cython_compile
**options.options)
File "C:\Anaconda3\lib\site-packages\Cython\Build\Dependencies.py", line 809, in cythonize
ctx = c_options.create_context()
File "C:\Anaconda3\lib\site-packages\Cython\Compiler\Main.py", line 581, in create_context
self.cplus, self.language_level, options=self)
File "C:\Anaconda3\lib\site-packages\Cython\Compiler\Main.py", line 90, in __init__
self.include_directories = include_directories + [standard_include_path]
TypeError: unsupported operand type(s) for +: 'bool' and 'list'
是的,我可以创建一个setup.py,但是我发现它非常烦人,它会将那个构建文件夹留在那里,而cythonize则不会这样做。
答案 0 :(得分:2)
看起来无法通过cythonize
的命令行版本传入include dirs。
问题在于查看cython源代码,它期望选项是布尔值:
https://github.com/cython/cython/blob/master/Cython/Build/Cythonize.py#L48
因此,当您包含路径时,它会转换为True
:
cythonize -i -s include_path=path_to_numpy_include test.pyx
并将选项解析为:
{'directives': {}, 'options': {'include_path': True}, 'python3_mode': None, 'annotate': None, 'excludes': [], 'build': True, 'build_inplace': True, 'parallel': 12, 'force': None, 'quiet': None, 'lenient': None, 'keep_going': None}
会导致您报告类型错误:
TypeError: unsupported operand type(s) for +: 'bool' and 'list'
我认为你最好的选择就是使用合适的setup.py
。