You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Anaconda Spyder中集成Cython与Python时new_sum函数导入出错

Hey there! Let's work through this Cython import issue you're facing—it's totally common when you're just getting started with Cython and Spyder, so don't stress. Let's break down the possible fixes step by step:

First, double-check the basics: filename & spelling

Make sure your .pyx file is exactly named prova_cythonn.pyx—match the spelling, capitalization, and even the extra "n" at the end with what's in your Python import line (from prova_cythonn import...). It sounds silly, but typos here are the #1 cause of this kind of ImportError.

Clear cached files & force a recompile

pyximport stores compiled versions of your .pyx files (like .so on Linux/macOS or .pyd on Windows) and uses those instead of re-reading your updated code. To fix this:

  1. Go to your project folder and delete these files/folders:
    • prova_cythonn.so (or .pyd)
    • The __pycache__ folder
    • The auto-generated prova_cythonn.c file (if it exists)
  2. Update your Python script to force pyximport to recompile everything from scratch:
    import numpy as np
    import pyximport
    # Add these flags to bypass cache and reload changes
    pyximport.install(setup_args={"script_args": ["--force"]}, reload_support=True)
    from prova_cythonn import saluti, new_sum
    

Fix potential issues in your Cython code

Your new_sum function has a couple of small tweaks that might be causing the import failure:

  1. Add a cimport numpy as np line at the top of your .pyx file—this helps Cython properly handle numpy types and functions under the hood.
  2. Return a numpy array instead of a raw memory view: Directly returning Y (a double[:] memory view) can confuse Python's import system. Convert it to a numpy array first.

Here's the updated .pyx code:

import numpy as np
cimport numpy as np  # Add this line

def saluti():
    print('hello world')

def new_sum(double[:] X, double[:] beta, double[:,:] theta):
    cdef int N = X.shape[0]
    cdef double[:] Y = np.zeros(N)
    cdef int i, j
    for i in range(N):
        for j in range(i):
            Y[i] += beta[j] * np.exp(-theta[i, j]*(X[i] - X[j]))
    return np.asarray(Y)  # Convert memory view to numpy array

Manually compile to catch hidden errors

If you still get the error, manually compiling your .pyx file will show you exactly what's going wrong (pyximport sometimes hides compilation errors). Here's how:

  1. Create a setup.py file in your project folder with this code:
    from setuptools import setup
    from Cython.Build import cythonize
    import numpy as np
    
    setup(
        name="prova_cythonn",
        ext_modules=cythonize("prova_cythonn.pyx"),
        include_dirs=[np.get_include()]  # Make sure numpy headers are found
    )
    
  2. Open your Anaconda Prompt (Windows) or terminal (Linux/macOS), activate your environment:
    conda activate your_environment_name
    
  3. Run the compile command:
    python setup.py build_ext --inplace
    
    Any compilation errors (like type mismatches or missing dependencies) will show up here—fix those, then try importing again.

Once you've done these steps, your new_sum function should import correctly. Let me know if you hit any snags along the way!

内容的提问来源于stack exchange,提问作者acini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:16:46