Best way to go about embedding python

Chris Angelico rosuav at gmail.com
Sun Nov 13 08:23:39 EST 2016


On Sun, Nov 13, 2016 at 6:10 PM,  <kfjwheeler at gmail.com> wrote:
>
> (Sorry about the last post, my finger slipped before I was finished, and I can't delete it for some reason.)

(That's because this is a mailing list and newsgroup gatewayed to each
other. Once sent, a message cannot be edited or deleted.)

> A friend of mine is developing a game engine, and I would like to extend the engine by adding some scripting functionality.  However, there's a lot of information out there, and I have a lot of questions before I get started.
>

The very first question to ask is: Are you intending to let untrusted
or less trusted people write the scripts? If so, *do not* use Python.
Embedding Python can be done to make your work easier, but it isn't
sandboxed.

> 1.  What's the best implementation to use?
>         - The engine itself is written in pure C, and the compiler being used is clang.  Eventually, we'd like it to target multiple platforms.
>

Sounds like the standard CPython interpreter will suit you just fine.

> 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?
>

You'll probably want to link against libpython, which (AIUI) will
connect you to an installed Python, complete with the standard
library. Python without its stdlib is a disappointingly featureless
language :)

> 3.  How could I go about adding a repl to the engine application?  Preferably, I'd be able to connect to it with sublimerepl.
>

Hmm, that depends a bit on the way your app works. Most likely, a
classic REPL won't work, as the console won't be directly available.
You'll probably end up with an internal console of some sort. You
should be able to either build your own REPL on top of that, or tie in
with something from Idle's code.

> 4.  Would it be possible to attach any python debuggers to the application and step through python code?
>

Again, this depends on how your app is architected, but it should be possible.

Embedding Python isn't too hard. The best place to start is here:

https://docs.python.org/3/extending/index.html

You'll find it's easy enough to get a simple "Hello, world" going, and
after that, it's a matter of figuring out what you need.

Have fun with it!

ChrisA



More information about the Python-list mailing list