无法将 cpplist 导入 Cython?

Posted

技术标签:

【中文标题】无法将 cpplist 导入 Cython?【英文标题】:Can't import cpplist into Cython? 【发布时间】:2019-08-06 01:22:17 【问题描述】:

我正在尝试弄清楚如何在 Cython 中使用列表/数组,这看起来非常复杂,所以我更喜欢只使用 C++ 列表,因为我看到有些人在 SO 上使用。但是,当我运行他们的代码时,我在 ipynb 中遇到了 gcc+ 编译错误。 Cython 数据结构令人愤怒。

在单元格中单独运行时出现此错误,我尝试使用和不使用 %%cython 魔术调用进行导入,并且都出现错误...

'''

%%cython

from libcpp.list cimport list as cpplist 

'''
def main(int t):

cdef cpplist[int] temp

for x in range(t):
    if x> 0:
        temp.push_back(x)

cdef int N = temp.size()
cdef list OutputList = N*[0]

for i in range(N):
    OutputList[i] = temp.front()
    temp.pop_front()

return OutputList
'''

'''

 ---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    117             self.spawn(compiler_so + cc_args + [src, '-o', obj] +
--> 118                        extra_postargs)
    119         except DistutilsExecError as msg:

/anaconda3/lib/python3.6/distutils/ccompiler.py in spawn(self, cmd)
    908     def spawn(self, cmd):
--> 909         spawn(cmd, dry_run=self.dry_run)
    910 

/anaconda3/lib/python3.6/distutils/spawn.py in spawn(cmd, search_path, verbose, dry_run)
     35     if os.name == 'posix':
---> 36         _spawn_posix(cmd, search_path, dry_run=dry_run)
     37     elif os.name == 'nt':

/anaconda3/lib/python3.6/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run)
    158                           "command %r failed with exit status %d"
--> 159                           % (cmd, exit_status))
    160             elif os.WIFSTOPPED(status):

DistutilsExecError: command 'gcc' failed with exit status 1

During handling of the above exception, another exception occurred:

CompileError                              Traceback (most recent call last)
<ipython-input-6-70891eecfa66> in <module>()
----> 1 get_ipython().run_cell_magic('cython', '--cplus', '\n# distutils: language = c++\nfor i in range(10):\n    print(i)\n    \n\n\n#from libc.math cimport log\nfrom libcpp.list cimport list as cpplist')

/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2165             magic_arg_s = self.var_expand(line, stack_depth)
   2166             with self.builtin_trap:
-> 2167                 result = fn(magic_arg_s, cell)
   2168             return result
   2169 

<decorator-gen-127> in cython(self, line, cell)

/anaconda3/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/anaconda3/lib/python3.6/site-packages/Cython/Build/IpythonMagic.py in cython(self, line, cell)
    327 
    328         self._build_extension(extension, lib_dir, pgo_step_name='use' if args.pgo else None,
--> 329                               quiet=args.quiet)
    330 
    331         module = imp.load_dynamic(module_name, module_path)

/anaconda3/lib/python3.6/site-packages/Cython/Build/IpythonMagic.py in _build_extension(self, extension, lib_dir, temp_dir, pgo_step_name, quiet)
    437             if not quiet:
    438                 old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
--> 439             build_extension.run()
    440         finally:
    441             if not quiet and old_threshold is not None:

/anaconda3/lib/python3.6/distutils/command/build_ext.py in run(self)
    337 
    338         # Now actually compile and link everything.
--> 339         self.build_extensions()
    340 
    341     def check_extensions_list(self, extensions):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extensions(self)
    446             self._build_extensions_parallel()
    447         else:
--> 448             self._build_extensions_serial()
    449 
    450     def _build_extensions_parallel(self):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in _build_extensions_serial(self)
    471         for ext in self.extensions:
    472             with self._filter_build_errors(ext):
--> 473                 self.build_extension(ext)
    474 
    475     @contextlib.contextmanager

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extension(self, ext)
    531                                          debug=self.debug,
    532                                          extra_postargs=extra_args,
--> 533                                          depends=ext.depends)
    534 
    535         # XXX outdated variable, kept here in case third-part code

/anaconda3/lib/python3.6/distutils/ccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    572             except KeyError:
    573                 continue
--> 574             self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
    575 
    576         # Return *all* object filenames, not just the ones we just built.

/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    118                        extra_postargs)
    119         except DistutilsExecError as msg:
--> 120             raise CompileError(msg)
    121 
    122     def create_static_lib(self, objects, output_libname,

CompileError: command 'gcc' failed with exit status 1

在单元格中单独运行时出现此错误,我尝试使用和不使用 %%cython 魔术调用进行导入,并且都出现错误...

在 Cython 中,我得到“None”类型的对象没有长度(Cython 语言中唯一的错误消息)

无效语法

请告知,Cython 让我准备好在 2 天后撕掉我的头发。

编辑:我尝试过使用:

%%cython --cplus
#distutils: language = c++

同样的错误信息。 另外,只是运行 '%%cython --cplus' 给我一个错误消息,同一个? 单元格中的任何东西,简单的打印或任何东西。我认为我的 cpp 扩展有问题...我该如何解决?

在终端中(使用 runipy -- 除了通过 setup.py 和 distutils Build 编译之外,不知道如何在终端中运行 ipynb)

zacharys-mbp:Cython zoakes$ runipy CSTL.ipynb
08/08/2019 08:47:47 PM INFO: Reading notebook CSTL.ipynb
08/08/2019 08:47:49 PM INFO: Running cell:
%load_ext cython 


08/08/2019 08:47:49 PM INFO: Cell returned
08/08/2019 08:47:49 PM INFO: Running cell:
%%cython 

#distutils: language = c++
from libcpp.list cimport list as cpplist

    warning: include path for stdlibc++ headers not found; pass '-    stdlib=libc++' on
      the command line to use the libc++ standard library instead
      [-Wstdlibcxx-not-found]
/Users/zoakes/.ipython/cython/_cython_magic_5a0764b273da2aafc5775e4dd20b1249.cpp:592:10: fatal error: 
      'ios' file not found
#include "ios"
         ^~~~~
1 warning and 1 error generated.
08/08/2019 08:47:50 PM INFO: Cell raised uncaught exception: 
---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    117             self.spawn(compiler_so + cc_args + [src, '-o', obj] +
--> 118                        extra_postargs)
    119         except DistutilsExecError as msg:

/anaconda3/lib/python3.6/distutils/ccompiler.py in spawn(self, cmd)
    908     def spawn(self, cmd):
--> 909         spawn(cmd, dry_run=self.dry_run)
    910 

/anaconda3/lib/python3.6/distutils/spawn.py in spawn(cmd, search_path, verbose, dry_run)
     35     if os.name == 'posix':
---> 36         _spawn_posix(cmd, search_path, dry_run=dry_run)
     37     elif os.name == 'nt':

/anaconda3/lib/python3.6/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run)
    158                           "command %r failed with exit status %d"
--> 159                           % (cmd, exit_status))
    160             elif os.WIFSTOPPED(status):

DistutilsExecError: command 'gcc' failed with exit status 1

During handling of the above exception, another exception occurred:

CompileError                              Traceback (most recent call last)
<ipython-input-2-e4f283bb7389> in <module>()
----> 1 get_ipython().run_cell_magic('cython', '', '\n#distutils: language = c++\nfrom libcpp.list cimport list as cpplist')

/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2165             magic_arg_s = self.var_expand(line, stack_depth)
   2166             with self.builtin_trap:
-> 2167                 result = fn(magic_arg_s, cell)
   2168             return result
   2169 

<decorator-gen-127> in cython(self, line, cell)

/anaconda3/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/anaconda3/lib/python3.6/site-packages/Cython/Build/IpythonMagic.py in cython(self, line, cell)
    327 
    328         self._build_extension(extension, lib_dir, pgo_step_name='use' if args.pgo else None,
--> 329                               quiet=args.quiet)
    330 
    331         module = imp.load_dynamic(module_name, module_path)

/anaconda3/lib/python3.6/site-packages/Cython/Build/IpythonMagic.py in _build_extension(self, extension, lib_dir, temp_dir, pgo_step_name, quiet)
    437             if not quiet:
    438                 old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
--> 439             build_extension.run()
    440         finally:
    441             if not quiet and old_threshold is not None:

/anaconda3/lib/python3.6/distutils/command/build_ext.py in run(self)
    337 
    338         # Now actually compile and link everything.
--> 339         self.build_extensions()
    340 
    341     def check_extensions_list(self, extensions):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extensions(self)
    446             self._build_extensions_parallel()
    447         else:
--> 448             self._build_extensions_serial()
    449 
    450     def _build_extensions_parallel(self):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in _build_extensions_serial(self)
    471         for ext in self.extensions:
    472             with self._filter_build_errors(ext):
--> 473                 self.build_extension(ext)
    474 
    475     @contextlib.contextmanager

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extension(self, ext)
    531                                          debug=self.debug,
    532                                          extra_postargs=extra_args,
--> 533                                          depends=ext.depends)
    534 
    535         # XXX outdated variable, kept here in case third-part code

/anaconda3/lib/python3.6/distutils/ccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    572             except KeyError:
    573                 continue
--> 574             self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
    575 
    576         # Return *all* object filenames, not just the ones we just built.

/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    118                        extra_postargs)
    119         except DistutilsExecError as msg:
--> 120             raise CompileError(msg)
    121 
    122     def create_static_lib(self, objects, output_libname,

CompileError: command 'gcc' failed with exit status 1
08/08/2019 08:47:50 PM INFO: Shutdown kernel
08/08/2019 08:47:50 PM WARNING: Exiting with nonzero exit status

【问题讨论】:

我真的对cython一无所知,但是有可能看到编译器错误吗?我尝试在我的系统上通过 Cython(通过命令行)运行您的代码,但我收到有关缺少包含文件的错误。似乎其他人也遇到了同样的错误:github.com/cython/cython/issues/2694 您是否尝试运行“%%cython?”并读取输出以了解如何为单元激活 c++? Wrapping C++ Standard library with Cython in ipython的可能重复 作为参考,更有用的 gcc 错误消息出现在您运行 jupyter 而不是 jupyter notebook 的终端中(这真的没有帮助)。 %%cython 魔法运行良好,是否有可能我需要指定为 c++ 而不是 c?我没有在终端中运行,我使用的是 Ipy 魔术方法,除了与魔术方法相关的自动化编译器之外,没有其他正式编译器。我将包含完整的错误 【参考方案1】:

我认为这是一个 Mac 问题,这限制了我提供帮助的能力。然而,关键的错误信息似乎是:

warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on
      the command line to use the libc++ standard library instead
      [-Wstdlibcxx-not-found]

如果您搜索(部分)此消息,它看起来与 XCode 相关。在某些时候,Apple 将编译器从 GCC 切换到 Clang,这改变了它使用的 C++ 标准库的实现。

我认为最好的解决方案是在 XCode 上安装“stdlibc++”。不幸的是,我不知道你实际上是如何做到这一点的。

第二好的解决方案是为 Cython 添加建议的命令行参数 - 我认为这是第二好的,因为它使用了稍微不匹配的 C++ 标准库实现。

%%cython --compile-args=-stdlib=libc++ --link-args=-stdlib=libc++

我不确定它是否需要在编译和链接 args 中,或者只是编译 args。

【讨论】:

我能够让它工作:%%cython --compile-args=-stdlib=libc++ --link-args=-stdlib=libc++ --cplus #from libc.math cimport从 libcpp.list cimport list 登录为 cpplist 谢谢!你认为卸载 Xcode/Reinstall 会有帮助吗? (我认为这可能与 Atom 有关,因为它使用 Clang 版本来运行 C++) 老实说,我从未使用过 Mac,所以我猜想重新安装 XCode - 它可能会有所帮助,但也可能会导致更多问题。很高兴你让它工作了!

以上是关于无法将 cpplist 导入 Cython?的主要内容,如果未能解决你的问题,请参考以下文章

在 Cython 中调用点积和线性代数运算?

如何将 Cython 生成的模块从 python 导入 C/C++ 主文件? (用 C/C++ 编程)[关闭]

python导入的cython错误

cx_Freeze 无法包含 Cython .pyx 模块

长运通CYT3000B/CYT1000B线性恒流

如何在pytest中使用Cython?