1

I have searched a lot of information and seen a lot of c++ methods to call python. But because the specific requirements are a little more complicated than the title, I still haven't found a good way to implement this idea.

Currently I have written a c++ executable program using QT. I want it to be able to call a fixed interface written by the user in python.

I need to make this program release compiled and run on different users' computers. But not inline python interpreter. That is I need to make this program use the python interpreter on the user's computer. How should I make it automatically search for the user's python interpreter version to interpret the user's python function.

Before asking the question, I've used this tutorial from the official python documentation to compile a code that only works on my own computer. It won't work on another computer because the python interpreter and dynamic library are in different locations

Mr Warm
  • 23
  • 3
  • What's wrong with using embedded python? https://docs.python.org/3/extending/embedding.html – mutantkeyboard Mar 04 '22 at 13:26
  • In fact, this is a program similar to a small game. Specifies the interface that the user's python code needs to implement. If the interpreter is embedded at compile time, the user can only write the corresponding version of python code. In case the user uses the syntax of another python version, there will be problems – Mr Warm Mar 04 '22 at 13:34
  • I thought about simply calling `system("python script")` and somehow capturing the output, and googling such construct yielded this: https://stackoverflow.com/a/478960/7151494 – Fureeish Mar 04 '22 at 13:52

1 Answers1

1

You can’t call an arbitrary version of Python from (already) compiled code because the binary interface changes. There is a limited Python API that is guaranteed to remain compatible for longer periods; you’d have to use explicit dynamic loading or perhaps some environment tricks to select a version to use. Alternatively, you can run Python as a separate process and communicate with it via pipes or disk files.

All of this leaves open the question of finding the Python installation to use; PATH might be enough, depending on how your users’ computers are configured.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76