概述
Trying to run:
#include
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("import IPythonn"
"IPython.embed()n");
Py_Finalize();
return 0;
}
compiled with mignw64 gcc 4.6.0 ( g++ -I /c/prog64/Python27/include t.cpp /c/prog64/Python27/libs/libpython27.a ) under Windwos 7 I get the error:
$ a.exe
Traceback (most recent call last):
File "", line 2, in
File "c:prog64python27libsite-packagesIPythonterminalembed.py", line 290, in embed
shell = InteractiveShellEmbed.instance(**kwargs)
File "c:prog64python27libsite-packagesIPythonconfigconfigurable.py", line 354, in instance
inst = cls(*args, **kwargs)
File "c:prog64python27libsite-packagesIPythonterminalembed.py", line 92, in __init__
display_banner=display_banner
File "c:prog64python27libsite-packagesIPythonterminalinteractiveshell.py", line 328, in __init__
**kwargs
File "c:prog64python27libsite-packagesIPythoncoreinteractiveshell.py", line 483, in __init__
self.init_readline()
File "c:prog64python27libsite-packagesIPythoncoreinteractiveshell.py", line 1817, in init_readline
import IPython.utils.rlineimpl as readline
File "c:prog64python27libsite-packagesIPythonutilsrlineimpl.py", line 21, in
_rl = __import__(_rlmod_name)
File "c:prog64python27libsite-packagesreadline.py", line 6, in
from pyreadline.rlmain import Readline
File "c:prog64python27libsite-packagespyreadline__init__.py", line 11, in
from . import unicode_helper, logger, clipboard, lineeditor, modes, console
File "c:prog64python27libsite-packagespyreadlineconsole__init__.py", line 15, in
from .console import *
File "c:prog64python27libsite-packagespyreadlineconsoleconsole.py", line 610, in
msvcrt = cdll.LoadLibrary(ctypes.util.find_msvcrt())
File "C:prog64Python27Libctypes__init__.py", line 443, in LoadLibrary
return self._dlltype(name)
File "C:prog64Python27Libctypes__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 applicationTraceback (most recent call last):
Note that other commands in the PyRun_SimpleString do work; also, in a command line python session:
import IPython
IPython.embed()
works.
I expect the problem is related to MSVCR90.DLL where the _dlopen looks for, and the fact that g++ links to the "normal" MSVCRT.DLL. Compiling with VS2008 is no easy option. Same error message is seen when compiling with VS2010. Python setup is Anaconda Python 2.7.8 64 bit.
解决方案
Problem indeed is related to msvcr90.dll. Way to solve it is to link the program against msvcr90.dll. However, to link against msvcr90.dll you need to have a manifest or you will get a runtime error. This manifest looks like:
which I extracted from python.exe with a text editor and named msvcr90.manifest. This manifest is linked into the application using the resource file msvcr90.rc
#include "winuser.h"
1 RT_MANIFEST msvcr90.manifest
which in turned can be compiled into an object file using:
windres msvcr90.rc msvcr90.o
Than, compilation of the program with this resource file and msvcr90.dll becomes:
g++ -I /c/prog64/Python27/include t.cpp /c/prog64/Python27/libs/libpython27.a msvcr90.o msvcr90.dll
where I copied msvcr90.dll from c:/Windows/winsxs/amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.21022.8_none_750b37ff97f4f68b/msvcr90.dll
Input for this came from
and a couple of other web pages which explained me how to do this.
最后
以上就是舒心柜子为你收集整理的python调用动态库出现error193,使用IPython进行Python嵌入:WindowsError:[错误193]%1不是有效的Win32应用程序...的全部内容,希望文章能够帮你解决python调用动态库出现error193,使用IPython进行Python嵌入:WindowsError:[错误193]%1不是有效的Win32应用程序...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复