我的目标是创建一个Python库,可以导入到我的任何项目中,我希望通过使用C语言(类似于NumPy的工作方式)更快地创建这个库。
然而,在学习了许多不同的教程和视频之后,我总是会遇到同样的错误,并且无法完成教程中说我应该能够完成的过程。我一直在遵循教程中的这个教程指向:https://www.tutorialspoint.com/python/python_further_extensions
我试过用谷歌搜索错误,但没有任何价值。
我在我的电脑上运行的是windows164位,安装了32位和64位版本的Python(我相信这个问题可能与不正确的Python版本有关)。我完全按照教程的要求做了,但是,在安装(building?)它。
这是我的C代码(名为hello.c),它是从教程中复制和粘贴的:

#include <Python.h>

static PyObject* helloworld(PyObject* self) {
   return Py_BuildValue("s", "Hello, Python extensions!!");
}

static char helloworld_docs[] =
   "helloworld( ): Any message you want to put here!!\n";

static PyMethodDef helloworld_funcs[] = {
   {"helloworld", (PyCFunction)helloworld,
      METH_NOARGS, helloworld_docs},
      {NULL}
};

void inithelloworld(void) {
   Py_InitModule3("helloworld", helloworld_funcs,
                  "Extension module example!");
}

这是我的python setup.py代码,这也是从教程中复制和粘贴的,尽管我已经将目录输入到hello.c文件中:
from distutils.core import setup, Extension
setup(name='helloworld', version='1.0', ext_modules=[Extension('helloworld', ['C:/Users/penci/OneDrive/Python3.7_64bit/Lib/site-packages/PythonLib/CCode/hello.c'])])

完成此操作并将它们全部保存(在同一个文件中)后,教程将运行setup.py脚本,并在其后面加上“install”。在教程中,它说它应该创建所需的scipts和数据,并且我应该能够将python包导入到我的代码中。但是,当我运行这个时,我会得到两个东西之一,这取决于我如何运行它。
我打开命令提示符,运行setup.py及其后的安装,正如教程中所示。这种运行程序的方法要么会给我一个错误代码1120,要么会打印出以下内容:
running install
running build
running build_ext
running install_lib
copying build\lib.win-amd64-3.7\helloworld.cp37-win_amd64.pyd -> C:\Users\penci\OneDrive\Python3.7_64bit\Lib\site-packages
running install_egg_info
Writing C:\Users\penci\OneDrive\Python3.7_64bit\Lib\site-packages\helloworld-1.0-py3.7.egg-info

然而,当我尝试将脚本导入到python代码时,它出现在“代码建议”窗口中,但表示“没有名为helloworld的模块”(使用PyCharm Community)。
我以管理员身份打开命令提示符,并执行完全相同的操作。当我这样做时,它总是给我同样的结果:
running install
running build
running build_ext
building 'helloworld' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -IC:\Users\penci\OneDrive\Python3.7_64bit\include -IC:\Users\penci\OneDrive\Python3.7_64bit\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.22.27905\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.7.2\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /TcC:/Users/penci/OneDrive/Python3.7_64bit/Lib/site-packages/PythonLib/CCode/hello.c /Fobuild\temp.win-amd64-3.7\Release\Users/penci/OneDrive/Python3.7_64bit/Lib/site-packages/PythonLib/CCode/hello.obj
hello.c
C:/Users/penci/OneDrive/Python3.7_64bit/Lib/site-packages/PythonLib/CCode/hello.c(17): warning C4013: 'Py_InitModule3' undefined; assuming extern returning int
C:\Users\penci\OneDrive\Python3.7_64bit\Lib\site-packages\PythonLib\CCode\hello.c : fatal error C1083: Cannot open compiler generated file: 'C:\Windows\system32\build\temp.win-amd64-3.7\Release\Users\penci\OneDrive\Python3.7_64bit\Lib\site-packages\PythonLib\CCode\hello.obj': No such file or directory
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.22.27905\\bin\\HostX86\\x64\\cl.exe' failed with exit status 1

(抱歉,代码太多了,我只是不确定哪一位有助于解决问题)
我已经尝试过Google的“退出状态1”,虽然没有出现有意义的结果。

最佳答案

在你的第一点我没有足够的信息给你任何帮助。事实上,即使查看您间歇性地用1得到的错误,也会显示完全不相关的消息。
1120nd点中,问题似乎是编译器没有找到errlook.exe函数的定义。这导致了这样的结论:您没有链接定义该符号的正确python库。
无需修改代码或2脚本即可实现正确链接的一个简单方法是定义一个环境变量Py_InitModule3,由setup.py调用的microsoft windows链接器将查看该环境变量以查找LIB库。

SET LIB=%LIB%;C:/Users/penci/OneDrive/Python3.7_64bit/libs

然后必须在设置变量的同一控制台中运行cl.exe脚本。这将解决链接问题,如果没有其他问题出现,这应该允许使用您的自定义模块。

10-07 12:11
查看更多