ImportError:动态模块没有定义模块导出函数(PyInit_library)

时间:2018-05-16 16:08:55

标签: python ctypes

我正在尝试编写一个用于Python的C模块,但是我很难编译该程序。这是我的代码

LIBRARY.C

#include <stdio.h>
#include "/usr/include/python3.5/Python.h"
#include "library.h"

bool CaddBook(struct Library *lib,  char *bookname, char * booktype, char *bookauthor, int numOfPages) {

    if (lib->booksStored == 0) {
        lib->Books[0].book_name = bookname;
        lib->Books[0].book_author = bookauthor;
        lib->Books[0].book_type = booktype;
        lib->Books[0].numOfPages = numOfPages;
        lib->booksStored++;
        return true;
    }
    int counter = lib->booksStored;
    lib->Books[counter].book_name = bookname;
    lib->Books[counter].book_author = bookauthor;
    lib->Books[counter].book_type = booktype;
    lib->Books[counter].numOfPages = numOfPages;
    lib->booksStored++;
    return true;
}


char * CsearchForBookByName(struct Library *lib, char *bkname) {

    int storedBks = lib->booksStored;

    for (int i = 0; i < storedBks; i++) {
        if (lib->Books->book_name == bkname) {
            return "Found";
        }
    }
    return "Not Found";
}


char * CsearchForBookByAuthor(struct Library *lib, char *bkauthor) {
    int storedBks = lib->booksStored;

    for (int i = 0; i < storedBks; i++) {
        if (lib->Books->book_author == bkauthor) {
            return "Found";
        }
    }
    return "Not Found";
}

// in python creating a python class(Structure) with attributes which then could be happily passed
// to any of the functions above

// getting return types of SeaFoBook and SeaBookAuthor and printing them

static PyObject* addBook(PyObject* self, PyObject* args)
{
    struct Library *lib;  char *bookname; char * booktype; char *bookauthor; int numOfPages;

    if (!PyArg_ParseTuple(args, "Osssi", &lib, &bookname, &booktype, &bookauthor, &numOfPages))
        return NULL;

    return Py_BuildValue("p", CaddBook(lib, bookname, booktype, bookauthor, numOfPages));
}

static PyObject* searchForBookByName(PyObject* self, PyObject* args)
{
    struct Library *lib; char *bookname;

    if (!PyArg_ParseTuple(args, "O&s", &lib, &bookname))
        return NULL;

    return Py_BuildValue("s", CsearchForBookByName(lib, bookname));
}

static PyObject* searchForBookByAuthor(PyObject* self, PyObject* args)
{
    struct Library *lib; char *bookauthor;

    if (!PyArg_ParseTuple(args, "O&s", &lib, &bookauthor))
        return NULL;

    return Py_BuildValue("s", CsearchForBookByAuthor(lib, bookauthor));
}

static PyObject* version(PyObject* self)
{
    return Py_BuildValue("s", "Version 1.0");
}

static PyMethodDef myMethods[] = {
        {"addBook",               addBook,               METH_VARARGS, "This is adding a book to the library"},
        {"searchForBookByName",   searchForBookByName,   METH_VARARGS, "Returns whether book name is found or not."},
        {"searchForBookByAuthor", searchForBookByAuthor, METH_VARARGS, "Returns whether book is found or not based on author."},
        {"version", (PyCFunction)version, METH_NOARGS, "Returns the version."},
        { NULL, NULL, 0, NULL}
};

static struct PyModuleDef myModule = {
        PyModuleDef_HEAD_INIT,
        "library",
        "library Module",
        -1,
        myMethods
};

PyMODINIT_FUNC PyInit_myModule(void)
{
    Py_Initialize();
    return PyModule_Create(&myModule);
}

Library.h 

#ifndef LIBRARY_LIBRARY_H
#define LIBRARY_LIBRARY_H
#include <stdbool.h>

typedef struct Book {

    char *book_name;
    char *book_type;
    char *book_author;
    int numOfPages;
};

typedef struct Library {
    struct Book Books[100];
    int booksStored;
};


bool CaddBook(struct Library *lib,  char *bookname, char * booktype, char *bookauthor, int numOfPages);
char * CsearchForBookByName(struct Library *lib, char *bkname);
char * CsearchForBookByAuthor(struct Library *lib, char *bkauthor);


#endif //LIBRARY_LIBRARY_H

setup.py

from distutils.core import setup, Extension

module = Extension('library', sources = ['library.c'])

setup (name = 'PackageName',
       version = '1.0',
       description = 'This is a package for library',
       ext_modules = [module])

然后使用sudo python3 setup.py build_ext --inplace从cmd行运行,然后使用cd build /(so_location)然后使用python3 - &gt;导入库

老实说,我不完全明白任何建议都会有用。

0 个答案:

没有答案