在Jupyter与Spyder中导入.pyx文件失败,寻求解决方法
问题描述
正在将Python 2代码迁移至Python 3,其中包含一个.pyx扩展名的Cython模块。尝试使用以下代码在Jupyter Notebook和Spyder中导入该模块时均出现错误:Jupyter提示option -f not recognized,Spyder提示no commands supplied。
用户使用的代码:
from distutils.core import setup, Extension from Cython.Build import cythonize import numpy package = Extension('deepnovo_cython_modules', ['deepnovo_cython_modules.pyx'], include_dirs=[numpy.get_include()]) setup(ext_modules=cythonize([package]))
Jupyter Notebook报错信息
Traceback (most recent call last): File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\fancy_getopt.py", line 233, in getopt opts, args = getopt.getopt(args, short_opts, self.long_opts) File "C:\Users\minto\anaconda3\envs\tf_env\lib\getopt.py", line 95, in getopt opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) File "C:\Users\minto\anaconda3\envs\tf_env\lib\getopt.py", line 195, in do_shorts if short_has_arg(opt, shortopts): File "C:\Users\minto\anaconda3\envs\tf_env\lib\getopt.py", line 211, in short_has_arg raise GetoptError(_('option -%s not recognized') % opt, opt) getopt.GetoptError: option -f not recognized During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\core.py", line 134, in setup ok = dist.parse_command_line() File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\dist.py", line 475, in parse_command_line args = parser.getopt(args=self.script_args, object=self) File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\fancy_getopt.py", line 235, in getopt raise DistutilsArgError(msg) distutils.errors.DistutilsArgError: option -f not recognized During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\minto\anaconda3\envs\tf_env\lib\site-packages\IPython\core\interactiveshell.py", line 3552, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "C:\Users\minto\AppData\Local\Temp\ipykernel_19296\1354990193.py", line 6, in <cell line: 6> setup(ext_modules=cythonize([package])) File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\core.py", line 136, in setup raise SystemExit(gen_usage(dist.script_name) + "\nerror: %s" % msg) SystemExit: usage: ipykernel_launcher.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: ipykernel_launcher.py --help [cmd1 cmd2 ...] or: ipykernel_launcher.py --help-commands or: ipykernel_launcher.py cmd --help error: option -f not recognized During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\minto\anaconda3\envs\tf_env\lib\site-packages\IPython\core\ultratb.py", line 1101, in get_records return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset) File "C:\Users\minto\anaconda3\envs\tf_env\lib\site-packages\IPython\core\ultratb.py", line 248, in wrapped return f(*args, **kwargs) File "C:\Users\minto\anaconda3\envs\tf_env\lib\site-packages\IPython\core\ultratb.py", line 281, in _fixed_getinnerframes records = fix_frame_records_filenames(inspect.getinnerframes(etb, context)) File "C:\Users\minto\anaconda3\envs\tf_env\lib\inspect.py", line 1541, in getinnerframes frameinfo = (tb.tb_frame,) + getframeinfo(tb, context) AttributeError: 'tuple' object has no attribute 'tb_frame'
Spyder报错信息
runfile('C:/Users/minto/Desktop/DeepNovo-DIA-master/deepnovo_cython_setup.py', wdir='C:/Users/minto/Desktop/DeepNovo-DIA-master') An exception has occurred, use %tb to see the full traceback. Traceback (most recent call last): File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\core.py", line 134, in setup ok = dist.parse_command_line() File "C:\Users\minto\anaconda3\envs\tf_env\lib\distutils\dist.py", line 501, in parse_command_line raise DistutilsArgError("no commands supplied") DistutilsArgError: no commands supplied During handling of the above exception, another exception occurred: SystemExit: usage: deepnovo_cython_setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: deepnovo_cython_setup.py --help [cmd1 cmd2 ...] or: deepnovo_cython_setup.py --help-commands or: deepnovo_cython_setup.py cmd --help error: no commands supplied
解决方案
方法1:命令行编译安装
将上述代码保存为setup.py,打开终端(命令提示符)切换到该文件所在目录,执行以下命令:
python setup.py build_ext --inplace
命令执行后会在当前目录生成编译好的扩展模块(Windows为.pyd文件,Linux/macOS为.so文件),之后即可直接在Python代码中导入deepnovo_cython_modules。
方法2:在Jupyter/Spyder中直接编译(规避参数冲突)
Jupyter和Spyder运行时会自动传入命令行参数,导致setup()函数解析错误。可以手动指定编译命令参数解决:
from distutils.core import setup, Extension from Cython.Build import cythonize import numpy package = Extension('deepnovo_cython_modules', ['deepnovo_cython_modules.pyx'], include_dirs=[numpy.get_include()]) # 手动指定编译命令,避免读取环境参数 setup(ext_modules=cythonize([package]), script_args=['build_ext', '--inplace'])
运行代码后会自动生成编译模块,之后就能正常导入使用。
方法3:使用pyximport快速导入
如果只是临时测试,无需编写完整setup脚本,可以用Cython的pyximport工具自动编译导入:
import pyximport import numpy # 指定numpy头文件路径,避免编译报错 pyximport.install(pyimport=True, setup_args={'include_dirs': [numpy.get_include()]}) import deepnovo_cython_modules
这种方式会自动完成编译并导入模块,适合快速验证功能。
内容的提问来源于stack exchange,提问作者Minto Mathew
相关产品推荐
相关产品推荐

