[英]How to solve Python-C-API error "This is an issue with the package mentioned above, not pip."?
我正在尝试在使用 python 编程语言运行的系统中以 C 编程语言的形式实现算法。 我正在尝试实现 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);
}
基本上我实现了一个哈希算法,其中输入是字符串形式,结果 output 也是算法结果字符串的形式。 我尝试使用以下 setup.py 文件使用pip install.
命令
from setuptools import setup, Extension
setup(name = "gimlihash",
version = "0.1",
ext_modules = [Extension("gimli", ["Hash/hashing.c"])])
希望我可以为我的项目创建一个本地库,我可以调用该库。 请帮忙,因为我还是个初学者。
根据[Python.Docs]:用 C 或 C++ 扩展 Python - 模块的方法表和初始化 Function (重点是我的):
反过来,这个结构必须在模块的初始化 function 中传递给解释器。初始化 function必须命名为PyInit_name() ,其中name是模块的名称,并且应该是模块中定义的唯一非
static
项文件:
所以,两者之间存在差异:
模块名称: gimli ( setup.py )
初始化 function 名称: PyInit_ hash ( hashing.c )
为了让一切顺利, 2 必须匹配,所以:
到处使用hash ( Extension("hash", ...
)
在任何地方使用gimli ( PyMODINIT_FUNC PyInit_gimli(...
,正如@RetiredNinja 在评论中所建议的那样)
由于我们讨论的是扩展模块(很可能是包的一部分),因此有一个通用约定(不一定每个人都遵循)它们的名称以下划线( _ ) 开头。 因此,我建议将您的模块命名为_hash 。 所需的更改是:
设置.py :
模块名称
#... ext_modules = [Extension("_hash", ["Hash/hashing.c"])])
散列.c :
Function 姓名:
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", //...
可能还想检查:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.