Python嵌入IPython:WindowsError:[Error 193]%1不是有效的Win32应用程序

2024-10-01 09:17:16 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试运行:

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("import IPython\n"
                     "IPython.embed()\n");
  Py_Finalize();
  return 0;
}

在Windwos 7下用mignw64 gcc 4.6.0(g++ -I /c/prog64/Python27/include t.cpp /c/prog64/Python27/libs/libpython27.a)编译时,我得到错误:

^{pr2}$

请注意,PyRun_SimpleString中的其他命令也可以工作;同样,在命令行python会话中:

import IPython
IPython.embed()

有效。在

我认为这个问题与MSVCR90.DLL有关,而MSVCR90.DLL正是这个函数所查找的,而且g++链接到“normal”的事实MSVCRT.DLL. 用VS2008编译不是一个简单的选择。在使用VS2010编译时会看到相同的错误消息。Python设置是Anaconda Python 2.7.8 64位。在


Tags: pyimportincludemain错误ipythonembedpyrun
1条回答
网友
1楼 · 发布于 2024-10-01 09:17:16

这个问题确实和msvcr90.dll有关。解决这个问题的方法是将程序链接到msvcr90.dll。但是,要链接到msvcr90.dll,您需要有一个清单,否则您将得到一个运行时错误。此清单看起来像:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="amd64" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
</assembly>

我从中提取的python.exe使用一个名为msvcr90.manifest的文本编辑器。此清单使用资源文件msvcr90.rc链接到应用程序

^{pr2}$

然后可以使用以下方法将其编译为对象文件:

windres msvcr90.rc msvcr90.o

然后,使用此资源文件和msvcr90.dll编译程序将变为:

g++ -I /c/prog64/Python27/include t.cpp /c/prog64/Python27/libs/libpython27.a msvcr90.o msvcr90.dll

我从c:/Windows/winsxs/amd64复制了msvcr90.dll_微软.vc90.crt_1fc8b3b9ae18e3b_9.0.21022.8_无\u 750b37ff97f4f68b/msvcr90.dll

对此的输入来自

还有一些其他的网页告诉我怎么做。在

相关问题 更多 >