PyImport_ImportModule bug in 3.1.2

B

bob.wang

Hi:
My Python version is 3.1.2.
I am programming embedding c with python in windows.
When I imported urllib.request in my test py file,
PyImport_ImportModule always return NULL.
But I imported re or cmd ,they work fine.
I found urllib is folder, and request seems to be submodule.
I don't know how to import it. I confirmed myutil.py and application
are in same folder.
Could you give me some advice?
Thank you.

myutil.py

import sys
import urllib.request


CRLF = '\r\n'

def addValue(oper1,oper2):
return (oper1 + oper2);


def ConnectURL(url):
try:
req = urllib.request.urlopen(url)
response = req.read()
req.close()
return (req.getcode(),req.geturl())

except BaseException as exp:
sys.stdout.write(exp)


native code:



int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
PyObject* pPyUtil = NULL;
PyObject* pPypfn = NULL;
PyObject* pPyArgs = NULL;
PyObject* pPyRet = NULL;
int nRet = 0;
int nValue = 0;


Py_Initialize();
PyImport_ImportModule("sys"); // ok
PyImport_ImportModule("csv"); // ok

PyImport_ImportModule("urllib"); // ok

pPyUtil = PyImport_ImportModule("myutil"); // failed ,
if(pPyUtil != NULL)
{

Py_DECREF(pPyUtil);
}
Py_Finalize();



return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top