Archived

This topic is now archived and is closed to further replies.

Embedding Python

This topic is 5113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I''m using Boost.Python and trying to embedde Python into C++ app. Take a look on this minimalistic code: #include <Python.h> #include <boost/python.hpp> using namespace boost:ython; char const* please_work() { return "Thank God!"; } BOOST_PYTHON_MODULE(testme) { def("please_work", please_work); } int main() { Py_Initialize(); PyRun_SimpleString("import testme\n" "print testme.please_work()\n"); Py_Finalize(); return 0; } Compile & run and I''ve got: ImportError: No module named testme Huh ? Did I forget something ? no more war pigs of the power

Share this post


Link to post
Share on other sites
You need to notify python that the testme module is a builtin module.
That is, it doesn't exist as an external script but is part of the binary itself.
(like sys, math... which are compiled into the python interpreter)

To do so, use the PyImport_AppendInittab() function before Py_Initialize().

PyImport_AppendInittab("testme", inittestme);
Py_Initialize();
PyRun_SimpleString("import testme\n"
"print testme.please_work()\n");
Py_Finalize();

initfoo is the standard Python initialisation entry point for the extension module named foo .
Boost.Python should have generated it for you.

Checking the return values from those functions may also be helpful.


[ 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 ]


[edited by - Fruny on November 13, 2003 12:59:03 PM]

Share this post


Link to post
Share on other sites
Thanks Fruny, I needed that.

I have another question. Suppose some script uses Simple Generators. Now, this script would be executed from C++ ... is it possible that after yield statement control would be returned to C++ app ? Or there is some other way to stop script execution, save it state, execute C++ code and next continue at resumed line ?

Share this post


Link to post
Share on other sites
The best way to know what happens is to try it. But I''d think that if a generator is called by your C++ program then eventually it will have to return control to that program - there''s nothing else it can do. Any state in that generator will be preserved unless you call Py_Finalize, I expect.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
quote:
Original post by WarPig
I have another question. Suppose some script uses Simple Generators. Now, this script would be executed from C++ ... is it possible that after yield statement control would be returned to C++ app

I found it easier to write my script scheduler in python, and have this scheduler call out to the generator scripts. The scheduler is called once per frame in c++ (through the very-high-level embedding). It is up to the generator scripts to remember to yield execution- if they don''t keep this in mind, the script will block execution.

Sample code:
http://urbanhunting.com/~dustin/katana2/script.cpp
http://urbanhunting.com/~dustin/katana2/internal.py
http://urbanhunting.com/~dustin/katana2/example.py

In my C++ mainloop I simply call Script::update() once per frame.

Dustin

Share this post


Link to post
Share on other sites
Virtual filesystems ? Python 2.3 can import scripts directly from zip archives (PEP 273). Is that what you want ? Or do you want something like LUFS-Python ?

[edited by - Fruny on December 16, 2003 2:04:11 PM]

Share this post


Link to post
Share on other sites