🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

python & C++ & modules

Started by
6 comments, last by athos_musketeer 20 years, 11 months ago
Hi there. i spend all the night figuring out how to import a module written by me into a PyObject. that''s the problem: the PyImport_Import() doesn''t find (or that is what i believe) my stup** module. maybe it''s have something to do with the PATH or something. thanks.
Advertisement
I had trouble with this problem just yesterday in fact, but if you read the manuals (I mean, sit down with a cup of coffee and REEEEAD) it explains how. First off, Python only reads modules located in its System directories. So when you say PyImport_Import( ... ), it searches ALL its directories (that it already knows about) and if it doesn''t find it, tough luck. The reason for this is that in the script, you can write ''import ...'' and it will know right away whether or not that module exists, without having to search your computer. Now, the Lib folder in the Python directory (or maybe ''Libs'', not sure) has a ton of modules in it. You could put a module (say "foo.py") inside that directory, and call PyImport_Import( convert_to_PyObject*( "foo" ) ) and it would read it. Now, if this isn''t good enough for you (who wants to put their own scripts in their Python directory every time??), before you call Py_Initialize(), add on a directory to Pythons System path list, like so:

strcat( Py_GetPath(), ";C:/MyScripts/" );

Anyway, the '';'' in the directory is a delimiter, so Python knows that this is a new directory, and the rest is just the directory you want to add! I''m not sure how many characters are free in the list, so watch out, and make your directories short and sweet. Hope this helps!

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
thanks a lot. i''ll try it
but i reeeeead it too, (with a cup of coffe, too).

and i found other way to do it.

there it goes, for all the people with the same problems that us.
despues de Py_Initialize()
PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append('''')"); 


with that python knows that it has to find the modules in the current directory as well.

thanks a lot man.
Awesome, I think you''re way is a cleaner. I''ve been having some problems with my implementation though...it appears that PyImport_Import() is having a problem (it returns NULL with no errors set or anything). Before, whenever it couldn''t find my module (the same problem you had before), it would always output a message such as "Couldn''t Find Module Specified" or something like that. Now it just crashes. :S Anyway, I''ll use your method, see if it clears up. Thanks

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
Oh my God! Python''s REALLY gotta help us out here. If your modules aren''t syntax perfect, it won''t load them and it won''t output a debug message! Now I test if the return value is NULL and leave it up to the user to discern whether or not it see''s the file or whether it just won''t compile!

Chris Pergrossi
< ctoan >
My Realm
Chris PergrossiMy Realm | "Good Morning, Dave"
Yes, you have to check if the import function failed (raised an exception), then you can call PyErr_Print() (or other functions) to print the error to stderr.

That should include details about any syntax error.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
yes, when python can''t load a module cause sintexis error, it doesn''t load it.

remember when python return NULL (a c NULL) is because an error happens.
python has a own PyNull Object to return NULL.
I'm using the Boost library, and apparently it doesn't like NULL objects because it would keep crashing. However, whenever a syntax error occured (I would check for !PyImport_Import()), there would be no recorded error (PyErr_Occured()) so I couldn't call PyErr_Print(). I just have a message like:
if( !PyImport_Import( modName ) ){   fprintf( stderr, "Module '%s' Couldn't Be Loaded (Check Syntax)\n", modName );}

It works and I'm happy.

Chris Pergrossi
< ctoan >
My Realm


[edited by - c t o a n on July 17, 2003 12:10:47 AM]
Chris PergrossiMy Realm | "Good Morning, Dave"

This topic is closed to new replies.

Advertisement