Best way to go about embedding python

Michael Torrie torriem at gmail.com
Sun Nov 13 20:15:43 EST 2016


On 11/13/2016 12:10 AM, kfjwheeler at gmail.com wrote:
> 2.  How would I go about including python scripts to run with the
> engine.  Would the .py files from the standard libraries be
> necessary, or is anything built in?

It's usually just a matter of asking the python instance to load and run
the script for you.  You'll need to be familiar with the libpython C
API.  This document is a good starting point:
https://docs.python.org/3/extending/extending.html

One of the interesting things about libpython and the API is that
there's really little difference between extending Python with C and
embedding Python in C.  The principles are the same.

The hardest part about integrating Python is that you have to consider
what kinds of primitives from your C program you want to expose to
Python and how you intend to expose them.  You'll have to use the C api
to wrap your native structures and functions so that they can be
interacted with from Python.  And such bindings may need to be two-way.
For example in Python code you may want to instantiate some new instance
of game object, and you'll need to make sure that the wrapper code will
create the appropriate in-game object. This aspect is probably going to
be the most time-consuming and hardest.

I'm glad to hear of someone wanting to embed Python.  Seems like so many
projects are using Javascript (yuck).  The Epiphany browser, for example
used to use Python for creating extensions but years ago they tossed it
out in favor of javascript which was a step backwards I thought.  Of
course Lua is probably the most popular embedded scripting language,
particularly with gaming engines.



More information about the Python-list mailing list