[Pythonmac-SIG] Mac PyImport_ImportModule("modulename")
Zhi Peng
zhiyong_peng2003 at yahoo.com
Mon Dec 12 18:40:12 CET 2005
Hi! Bob
Following is the main code that I used for test. The
sys.path seems correct because I can import when I use
pythonw and import SringModule. Everytime I run the
program, it said "can not import
StringModule". One might use a simple module with only
one sentance inside as a "StringModule.py" and
"StringModule.pyc".
Thanks
Zhi
======================================================
#include <Python.h>
int main()
{
PyObject
*myMod=NULL,*myClass=NULL,*myModuleInstance=NULL;
Py_Initialize();
PyRun_SimpleString("import sys \n");
PyRun_SimpleString("sys.path.append('.\')\n");
PyRun_SimpleString("print sys.path \n");
//myMod=PyImport_ImportModule("os");
myMod=PyImport_ImportModule("StringModule");
if(!myMod) { printf("cannot import
StringModule\n"); return TRUE; }
return 0;
}
The 'StringModule' is in the right directory as
following:
==================================================
zhi-pengs-power-mac-g5:/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3
zpeng$ find . -name 'StringModule.py' -print
./StringModule.py
zhi-pengs-power-mac-g5:/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3
zpeng$ find . -name 'StringModule.pyc' -print
./StringModule.pyc
--- Bob Ippolito <bob at redivi.com> wrote:
> Well I have no idea whether you're extending or
> embedding Python..
> and if you are embedding, I have no idea what steps
> you've taken to
> initialize the interpreter... so I'm not sure.
>
> Like I said before, what you need to do is make
> absolutely sure
> you're using the correct Python interpreter,
> framework, or dylib and
> also make sure that sys.path is setup properly.
> This might help::
>
> PyRun_SimpleString("import sys\nprint
> sys.path\nprint sys.version
> \nprint sys.executable\n");
>
> -bob
>
> On Dec 9, 2005, at 8:45 PM, Zhi Peng wrote:
>
> > Hi! Bob
> >
> > I have not tried to append current directory in
> sys.path. Maybe I
> > need to do sth
> > sys.path.append("./") and then do import. The
> module is installed
> > in MacPython package. The os module is in
> python23.dylib so it can
> > find it and import it. But my module is just a py
> file.
> >
> > Thanks
> >
> >
> > Zhi
> >
> > Bob Ippolito <bob at redivi.com> wrote:
> >
> > On Dec 9, 2005, at 4:41 PM, Zhi Peng wrote:
> >
> > > In my c code, I have used
> myMod=PyImport_ImportModule("moduleName")
> > > and run on Mac, but it just can not import the
> "moduleName" for
> > > some unknown reason.
> > >
> > > I am sure I install the "moduleName" by "pythonw
> setup install".
> > > And I can import "moduleName" from command lines
> such as
> > >
> > > >pythonw
> > > >import moduleName
> > > >
> > >
> > > It is fine. I wonder why PyImport_ImportModule
> can not import it,
> > > while it can import module os by using
> > > PyImport_ImportModule("os")
> > >
> > > Any suggestion?
> > >
> >
> > Are you sure it's the same version of Python, and
> the module is on
> > sys.path? Check sys.version and sys.path
> >
> > -bob
> >
> >
> >
> > Yahoo! Shopping
> > Find Great Deals on Holiday Gifts at Yahoo!
> Shopping
> > _______________________________________________
> > Pythonmac-SIG maillist -
> Pythonmac-SIG at python.org
> >
>
http://mail.python.org/mailman/listinfo/pythonmac-sig
>
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the Pythonmac-SIG
mailing list