c# 如何解决Python-C-API错误“这是上面提到的包的问题,而不是pip "?

zzlelutf  于 2023-02-01  发布在  C#
关注(0)|答案(1)|浏览(282)

我试图在我的系统中实现一个C语言形式的算法,这个系统使用python编程语言运行。我试图实现Python C API,目的是让我的算法在python环境中运行。结果它产生了一个错误,我已经尝试修复了几天,但仍然找不到它。下面是我得到的错误结果:

$ pip install .
Processing c:\users\angga danar\documents\skripsi\project\gimli
  Preparing metadata (setup.py) ... done
Installing collected packages: gimlihash
  DEPRECATION: gimlihash is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for gimlihash ... error
  error: subprocess-exited-with-error
  
  × Running setup.py install for gimlihash did not run successfully.
  │ exit code: 1
  ╰─> [17 lines of output]
      running install
      C:\python3.10\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      running build
      running build_ext
      building 'gimli' extension
      creating build
      creating build\temp.win-amd64-cpython-310
      creating build\temp.win-amd64-cpython-310\Release
      creating build\temp.win-amd64-cpython-310\Release\Hash
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\python3.10\include -IC:\python3.10\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /TcHash/hashing.c /Fobuild\temp.win-amd64-cpython-310\Release\Hash/hashing.obj
      hashing.c
      creating C:\Users\Angga Danar\Documents\skripsi\Project\Gimli\build\lib.win-amd64-cpython-310
      "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\python3.10\libs /LIBPATH:C:\python3.10 /LIBPATH:C:\python3.10\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64" /EXPORT:PyInit_gimli build\temp.win-amd64-cpython-310\Release\Hash/hashing.obj /OUT:build\lib.win-amd64-cpython-310\gimli.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-310\Release\Hash\gimli.cp310-win_amd64.lib
      LINK : error LNK2001: unresolved external symbol PyInit_gimli
      build\temp.win-amd64-cpython-310\Release\Hash\gimli.cp310-win_amd64.lib : fatal error LNK1120: 1 unresolved externals
      error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\link.exe' failed with exit code 1120
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> gimlihash

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

下面是hashing. c文件的代码:

char print_tex(char* string, char* hex_string) {
    uint8_t output[32];
    size_t l = strlen(string);
    Gimli_hash(string, strlen(string), output, 32);
    int i;
    for (i = 0;i < 32;++i)
        fprintf( stderr, "%02x",output[i]);
        hex_string[i] = (char)output[i];
    return 0;
}

static PyObject* hash(PyObject *self, PyObject *args) {
    char *str;
    char final[32];
    if (!PyArg_ParseTuple(args, "s", &str)){
        return NULL;
    }
    print_tex(str, final);
    return PyUnicode_FromString(final);
}

static PyMethodDef hashMethods[] = {
    {"gimli", hash, METH_VARARGS, "Gimli Algorithm Hash"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef hashModule = {
    PyModuleDef_HEAD_INIT,
    "hashModule", 
    "hash module",
    -1,
    hashMethods
};

PyMODINIT_FUNC PyInit_hash(void) {
    return PyModule_Create(&hashModule);
}

基本上,我实现了一个散列算法,其中输入是字符串形式,结果输出也是算法的结果字符串形式。我尝试使用pip install .命令安装以下setup.py文件

from setuptools import setup, Extension

setup(name = "gimlihash",
        version = "0.1",
        ext_modules = [Extension("gimli", ["Hash/hashing.c"])])

希望我可以为我的项目创建一个本地库,我可以调用库。请帮助,因为我仍然是一个初学者。

ht4b089n

ht4b089n1#

根据[Python. Docs]:用C或C++扩展Python-模块的方法表和初始化函数(强调是我的):
该结构必须依次传递给模块初始化函数中的解释器。初始化函数必须命名为 * PyInit_name()*,其中 * name * 是模块的名称,并且应该是模块文件中定义的唯一非static项:
因此,存在以下差异:

  • 模块名称:gimli(* 设置. py *)
  • 初始化函数名称:* PyInit_哈希*(* 哈希. c *)

为了使事情 * OK *,2必须匹配,因此:

  • 在所有位置使用***哈希***(Extension("hash", ...
  • 在任何地方使用***gimli***(PyMODINIT_FUNC PyInit_gimli(...,正如@RetiredNinja在评论中建议的那样)

因为我们讨论的是扩展模块(很可能是包的一部分),所以有一个惯例(不一定每个人都遵循),就是它们的名字以 * UnderScore _)开头。因此,我建议将您的模块命名为**_hash***。

    • 设置. py *:
  • 模块名称
# ...
ext_modules = [Extension("_hash", ["Hash/hashing.c"])])
    • 散列. c *:
  • 功能名称:
PyMODINIT_FUNC PyInit__hash(void) {  // @TODO - cfati: Notice the double UnderScore: __
// ...
  • 模块名称(可选):
// ...
PyModuleDef_HEAD_INIT,
"_hash",  // @TODO - cfati: This is what `_hash.__name__` will return
"hash module",
// ...

可能还需要检查:

相关问题