Cython pyd 文件未通过 python 导入

Cython pyd files not importing through python

我正在 python 中编写一个模拟粒子反应的程序,目的是向用户传授有关粒子反应的知识。由于 python 在进行必要的处理时速度太慢,我转向 Cython 以提高速度并且它很有效。我可以用一个简单的 import 语句(即 "import module" ).

不过,程序最终还是要运行在别人的电脑上,而在这台电脑上,.pyd文件是不会导入的。当我尝试时收到此错误消息:

"ImportError: DLL load failed: The specified module could not be found."

.pyd 文件在两台计算机上的位置完全相同,但是,我正在 运行 宁 python 3.6,而另一台计算机安装了 python 3.3 .另外,我的电脑安装了 Cython,而另一台电脑没有。两台机器都是 32 位的。

我不能简单地将整个程序编译成一个 .exe 文件,因为另一台计算机在 .exe 个文件上有一个块。

我在 Cython 上浏览了 stack 的问题,也研究了 Cython 文档,但都无济于事。

有人可以向我解释为什么导入不起作用,我怎样才能让它起作用?关于 .pyd 文件究竟是什么以及 python 如何调用它们的一些额外背景也很好。

编辑:我 运行 来自名为 main.py 的文件的程序。在同一目录中,我有一个名为 main 的文件夹,其中存储了我在模块中使用的代码。 main.py 调用 PageManager.py,后者调用 ParticleModel.py,后者调用 MoveParticles.pyd。 (这三个文件存放在文件夹main中)。我使用语句

"import main.MoveParticles" 

导入 .pyd 文件,它可以在我的电脑上运行。

好的,这里的困难来自于对缺少哪个 DLL 的混淆。

我曾经(天真地)假设因为我试图导入一个 .pyd 文件,它本质上是一个 DLL,(Python C extension: Use extension PYD or DLL?),所以这是导致错误的原因。

但是,真正导致错误的原因是缺少 运行 我的 .pyd 文件所需的 DLL。我通过使用不同版本的 python(准确地说是 3.5)解决了这个问题,并且在重新编译我的 .pyd 文件以在 python 3.5 上工作后,应用程序工作正常。

总结:计算机试图 运行 我的文件缺少一些必要的 DLL,因此导致程序崩溃。

根据https://cython-devel.python.narkive.com/gqx0VU3L/importerror-dll-load-failed-the-specified-module-could-not-be-found

Just interpreting the error you're describing (ImportError: DLL load failed: could not be found), the dynamic linker couldn't find a library it needed. Most likely this is either a symptom of missing dependencies or a path problem. Here's my suggestions for diagnosing and fixing the problem:

Missing Dependencies: One very simple way to confirm that all the dependencies of your cython module are available is to point the dependency walker utility[1] at it, and look for missing DLLs.

Directory Structure: Is the .pyd file you built from your cython module in the PYTHONPATH (or your current working directory? If it's not, there's your issue.

如果缺少依赖项,请在此处下载依赖项 walker:http://www.dependencywalker.com/。然后打开您的 .pyd 文件并稍等片刻。查看主要分支中是否缺少文件并将这些文件添加到包含 python38.dll 的目录中(我的情况:C:\Users\PC\AppData\Local\Programs\Python\Python38)。