Python CExtension给出ModuleNotFoundError

Python CExtension给出ModuleNotFoundError,python,distutils,python-c-api,cextension,Python,Distutils,Python C Api,Cextension,我正在CentOS 7上使用Python 3和Anaconda Spyder。我正试图通过以下视频使Python CExtension正常工作。 我的所有文件都在PYTHONPATH中的/home/peter/pythonCExtensions中 我有一个文件myModule.c,它包含以下内容 #include <stdio.h> #include <stdlib.h> #include "/home/peter/anaconda3/include/python3.7

我正在CentOS 7上使用Python 3和Anaconda Spyder。我正试图通过以下视频使Python CExtension正常工作。

我的所有文件都在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);
}
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])
我跑

得到

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)时,我得到

上市

其核心是打字错误(大写与小写),加上在Nix上,文件名区分大小写(很可能在Win上无法复制)

因此,您的模块名为myModule(函数PyInitmyModule),但它位于名为myModule.cpython-37m-x86_64-linux-gnu.So的文件中,这是不正确的,因为这两个名称必须匹配
通过在setup.py中指定正确的扩展名来更正此错误:

module=Extension(“myModule”,sources=[“myModule.c”])

顺便说一句,不要给模块命名为myModule(一般来说,不要给东西命名为myStuff——就我个人而言,我觉得它很难看:)。例如,您可以使用fibonacci模块作为名称。

静态结构PyModuleDef myModule={PyModuleDef\u HEAD\u INIT,“myModule”,…
PyMODINIT\u FUNC PyInit\u myModule…
(myModule)?或者更好,另一种方法是:
模块=扩展(“myModule”,sources=[“myModule.c”)…
你能帮我一个忙吗?
在你的路由目录中找到安装树
并运行树。然后粘贴输出。只需编辑你的帖子并向我们展示你的结构。非常感谢,@CristiFati。这确实是个问题。非常感谢。@daunadeem。我已将结果添加到我的问题中。谢谢,
import myModule
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
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!