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:
- Go to your project folder and delete these files/folders:
prova_cythonn.so(or.pyd)- The
__pycache__folder - The auto-generated
prova_cythonn.cfile (if it exists)
- 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:
- Add a
cimport numpy as npline at the top of your.pyxfile—this helps Cython properly handle numpy types and functions under the hood. - Return a numpy array instead of a raw memory view: Directly returning
Y(adouble[:]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:
- Create a
setup.pyfile 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 ) - Open your Anaconda Prompt (Windows) or terminal (Linux/macOS), activate your environment:
conda activate your_environment_name - Run the compile command:
Any compilation errors (like type mismatches or missing dependencies) will show up here—fix those, then try importing again.python setup.py build_ext --inplace
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

