3

I have a Delphi Win32 program. I want to "expose" somehow app structures and procedures via Python module. E.g. module my_api must expose public items for my app structures/methods. This module must "sit" in memory only.

Then I want, in the same app, to call Python scripts (using Python dll) which can import my_api and call my app methods. How to do it.

2
  • I just noticed that you mentioned Delphi here. You will be going through two bridges to talk between Delphi and Python. For example, to call a Python function from Delphi, you need to use the Delphi FFI to access the C function PyObject_Call; to expose a Delphi function to Python, you need to first wrap it up to be callable from C, then wrap that up for Python. I haven't used Delphi in a very long time, but if this is a hassle, you might want to consider using Delphi.NET (that's a thing, right?) and IronPython instead. Commented Jul 12, 2013 at 18:17
  • Tks, I ll use Delphi way. Commented Jul 12, 2013 at 19:57

2 Answers 2

3

You're asking for two things here, which often go together.

First, you want to extend the Python interpreter, adding types and functions and so forth that Python code can use.

Second, you want to embed the Python interpreter in your app, so it can run Python scripts (which can use your extension modules).

Assuming you want to use CPython (the usual Python interpreter), the tutorial Extending and Embedding the Python Interpreter is part of the docs.

You may want to look at other options that help make the extending side easier—for example, you can use Cython to write the bridge code in a near-Python language instead of C, or Boost.Python to write it in nice C++ that takes care of most of the boilerplate for you, or SWIG to try to generate it automatically, or ctypes to avoid writing a bridge in the first place. But it's worth learning the underlying mechanism first.

Sign up to request clarification or add additional context in comments.

Comments

2

You may have heard of Python 4 Delphi by now, and if you haven't you can look it up here. https://code.google.com/p/python4delphi/. There are quite a few tutorials on the internet e,g http://www.atug.com/andypatterns/pythonDelphiTalk.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.