我在 CentOS 7 上使用 Python 3 和 Anaconda Spyder。我试图按照此视频让 Python CExtension 正常工作。 https://www.youtube.com/watch?v=a65JdvOaygM
我的所有文件都位于 PYTHONPATH 中的/home/peter/pythonCExtensions 中
我有一个包含以下内容的文件 myModule.c。
#include <stdio.h>
#include <stdlib.h>
#include "/home/peter/anaconda3/include/python3.7m/Python.h"
int Cfib(int n)
{
if (n<2){
return n;
} else {
return Cfib(n-1)+Cfib(n-2);
}
}
static PyObject* fib(PyObject* self, PyObject* args)
{
int n;
if (!PyArg_ParseTuple(args, "i", &n))
return NULL;
return Py_BuildValue("i", Cfib(n));
}
static PyObject* version(PyObject* self){
return Py_BuildValue("s", "Version 1.0");
}
static PyMethodDef myMethods[] = {
{"fib", fib, METH_VARARGS, "Calculates the Fibronacci numbers"},
{"version", (PyCFunction)version, METH_NOARGS, "Returns the version"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Fibronacci Module",
-1,
myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
return PyModule_Create(&myModule);
}
我制作了另一个文件 setup.py,其中包含以下内容。
from distutils.core import setup, Extension
module = Extension("MyModule", sources = ["myModule.c"])
setup(name="PackageName",
version="1.0",
description="This is a package for myModule",
ext_modules = [module])
我跑了
python3 setup.py install
得到了
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-3.7/MyModule.cpython-37m-x86_64-linux-gnu.so -> /home/peter/anaconda3/lib/python3.7/site-packages
running install_egg_info
Writing /home/peter/anaconda3/lib/python3.7/site-packages/PackageName-1.0-py3.7.egg-info
然后,我将 MyModule.cpython-37m-x86_64-linux-gnu.so 和 myModule.o 复制到/home/peter/pythonCExtensions 中。
然后我打开 Spyder 并创建一个文件 CInterface.py,其中仅包含
import myModule
但是,当我运行此文件(F5)时,我得到
Traceback (most recent call last):
File "<ipython-input-1-c29fad851da0>", line 1, in <module>
runfile('/home/peter/Simple3dShapes/CInterface.py', wdir='/home/peter/Simple3dShapes')
File "/home/peter/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "/home/peter/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/peter/Simple3dShapes/CInterface.py", line 19, in <module>
import myModule
ModuleNotFoundError: No module named 'myModule'
sudo yum install tree
结果
安装 1 个软件包
Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]: y
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1
Installed:
tree.x86_64 0:1.6.0-10.el7
Complete!
最佳答案
列表[Python.Docs]: Building C and C++ Extensions .
从本质上讲,这是一个拼写错误(大写与小写),再加上在Nix上,文件名区分大小写(最有可能的是,它无法在 Win 上重现)。
因此,您的模块名为 myModule (函数 PyInit_myModule),但它驻留在名为 MyModule.cpython-37m-x86_64-linux-gnu.so 的文件,OK,因为这两个名称必须匹配。
通过在setup.py中指定正确的扩展名称来更正该问题:
module = Extension("myModule", sources = ["myModule.c"])
顺便说一句,不要将模块命名为 myModule (一般不要命名为 myStuff - 就我个人而言,我真的很讨厌它:)),例如名字表明其目的尚不清楚。您可以使用例如 fibonacci_module 作为名称。
关于Python CExtension 给出 ModuleNotFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58769390/