python 2.7.10(./Watcher/epMain.py
):
import subprocess
import hashlib
import os
import sys
import zipfile
import httplib
#import urllib
#import urllib2
def letsbegin():
subprocess.call('a.exe')
httpClient = httplib.HTTPConnection('www.google.com', 80, timeout=30)
httpClient.request('GET', '/updata/Client_V.html')
response = httpClient.getresponse()
targetV = response.read()
letsbegin()
C ++:
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./Watcher')");
PyObject *pyMain = PyImport_ImportModule("epMain")
pyMain
总是NULL
,但在我将我的python代码更改为:
import subprocess
import hashlib
import os
import sys
import zipfile
#import httplib
#import urllib
#import urllib2
def letsbegin():
subprocess.call('a.exe')
httpClient = httplib.HTTPConnection('www.google.com', 80, timeout=30)
httpClient.request('GET', '/updata/Client_V.html')
response = httpClient.getresponse()
targetV = response.read()
letsbegin()
然后可以在我的c ++代码中加载这个模块 但我真的想在这个项目中使用httplib,怎么样?我不能用:
PyImport_ImportModule("httplib")
因为python代码可能经常更新。 此外,当我使用
d:\pros\go\Watcher>python epMain.py
它有效!
urllib
和urllib2
也有这样的问题。
答案 0 :(得分:1)
看起来你用Python 3.x包含/ libs而不是2.x编译。
在Python 3.x中,httplib
,urllib2
不可用。 (它们被重命名为http.client' and
urllib.request ,
urllib.error`)
将编译选项更改为包含,链接Python 2.x。
<强>更新强>
要检查C ++程序使用的版本,请尝试以下代码:
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("print(sys.version)");
...