Important: Please read the Qt Code of Conduct -

Embedding a Python script engine - anybody capable of helping me through that? [Open]

  • Hello.
    For a somewhat bigger project in Qt I want to embed a script engine in Qt (I know about QJSEngine), I would prefer it to be Python or -Lua-.
    There are things like PythonQt -and QtLua- - the first one is not up-to-date and it doesn't seem like it is continuously developed, both of them I am incapable of compiling (I already went through a lot of pain and never managed to come as far as to actually integrating the library into my project). Therefore I wanted to go the straight route, doing the embedding myself.

    Can anybody give me valuable clues or maybe even step-by-step instructions on how to embed a Python -or Lua- engine in a project?

    Some basic interactions between script and program should be possible, but it doesn't need to get too fancy.

    I have been trying, on and off, for weeks to include a script engine (also took a look into Perl and Ruby), but there have always been problems: The available wrappers for Qt are not in steady development, have a terrible documentation and/or I just couldn't get them to compile. Trying to make any engine available myself was fruitless, probably due to my limited knowledge.

    There are two requirements here:
    I am using Qt 5.1.1 with QtCreator, and my project has to run on at least Windows and Linux.

    I hope somebody can help me. Thanks in advance. :)

    EDIT: nibbit helped me compiling PythonQt, but I still would be happy to include Python -or Lua- manually.

    EDIT2: Removed the Lua references. I am busy trying to implement Python, Lua will be a completely separate thing somewhere in the near future.

  • PythonQt I think is still developed. Truly there is version for Qt4 on sourceforge page, but from project duscussion you can download beta versions for QT5:
    I was adding them for my project last week and seems to work great.
    Additionaly there is few example projects in PythonQt package. Let's try to use them.

  • Which one did you use? The one directly "linked in the thread": or the one from the "repository": ?

    I already tried the latter one and it didn't compile. Also, these aren't "official" as far as i can tell, the actual developers behind PythonQt won't readily upgrade to a newer Qt version, since they wrote PythonQt as a tool for "MeVisLab": .

    I will give it another shot.

  • I am using first one currently. What compiler are you using? Why can't you compile? Put here the errors.

  • I was just trying the one from the repository, and I am using MinGW (from the Qt installation):

    a) createSolution.bat (contains "qmake -tp vc -r")

     - lots of warnings, cannot generate _any_ output

    b) cmake -G "MinGW Makefiles"

     - complains about qmake reporting the wrong directory for QtCore (it's the lib-dir of Qt, QtCore is there)

  • So, if you are using MinGW instead of VC then you have to delete -tp vc from bat file and then create solution.

    Look here for -tp option:

  • Ok, that was useful, now I can run qmake - thanks :) . Is that supposed to be the first step? And what follows afterwards? Do I have to run a make/cmake, or should I just skip that?

    I tried to run mingw32-make:
    One of the PythonQt packages doesn't find my Python.h - is there some variable I have to set? The other package throws so many warnings and errors that the console loses track of them. Neither of them compiles.

    EDIT 2:
    Tinkering with the *.prf files now.

  • Well, that should be enough. I tried setting environment variables, changing things in the python.prf, but it got me nowhere. Even though the fatal errors for both PythonQt packages differed!
    This package is far from "plug&play" and it probably won't see the support I'd prefer (I don't want to depend on dead libraries), so it probably is just a bad choice.

    Back to the original topic then: Integrating a Python or Lua engine manually. Still on square one with that.

  • You discourage quickly :). Maybe this is far from "plug&play" but this is LGPL. Additionally you are using MinGW when author prepare project for VC.

    1. Yes, you have to set env variables. I have:
      PYTHON_PATH c:\Python26
    2. You have to set proper version in prf files:

    Addionally you have to change extensions in two prf file from '.lib' to '.a' and when these files will be builded then you have to put them in proper lib directory. So there is a bit of work. I can't help any more if you prefer other libs :).

  • As I said: I have been trying to tackle this for several weeks! I don't discourage quickly, but why is there no documentation/instructions on this? It always says "it's simple, just follow these three steps", but should you run into trouble, you are left alone.

    I knew about the two environment variables you mentioned, they are described "here": .

    I will try again...

    EDIT: How do I know which extensions I have to change? Did you mean physical files or a path inside some .prf?

  • So lets change these two variables.

    Next change:
    PythonQt_QtAll.prf -> win32::LIBS += $$PWD/../lib/libPythonQt_QtAll$${DEBUG_EXT}.a

    PythonQt.prf -> win32::LIBS += $$PWD/../lib/libPythonQt$${DEBUG_EXT}.a

    This is because MinGW generate 'a' static libraries. If you will encout linker error with 'libPythonQt.a doesnt exist' then copy it from build dir to pythonqt\lib dir

  • I redownloaded Python (3.3.2), to be sure that's not causing errors. Double-checked every directory that's mentioned in the .prf-files.
    Then I deleted the PythonQt directory and extracted a new one. Changed the three .prf-files, ran qmake (without any errors or warnings this time!), and eventually tried to compile everything (using "mingw32-make"), but it still didn't work. Did I miss anything?

    Prominent error while trying to compile was "base operand of '->' has non-pointer type ..."
    Am I missing some headers or even more environment variables?

  • Was you trying to run one of example projects? For ex. PyScriptEditor?

    Btw I am not sure is python3.x ported to this library.

    bq. but it still didn’t work.

    What do you mean?

  • Compilation failed due to lots of errors - "x was not declared in this scope", "x is of non-pointer type" etc. I am probably missing some include/lib.

    Which version of Python did you have success with? The one from the git repository is supposed to work with version 3.

    EDIT: Just tried the repository version with Python 2.7.5 and got exactly the same errors when trying to compile.

    EDIT2: The other version did indeed compile with Python 2.7.5! And I was able to run some of the examples, PyGettingStarted and NicePyConsole.

    I'll try to find out what to do next. But I'd still be interested in at least knowing about how much work it would be to incorporate a basic scripting engine into my application without the use of any "man in the middle".

    So far: Huge thank you. I am not yet comfortable with this, but at least something finally worked.

  • Still trying to compile PythonQt!
    I finally managed to catch one of the error-message I get:

    bq. parser\binder.cpp:810:17: error: base operand of '->' has non-pointer type 'MemberModelItem {aka CodeModelPointer<_MemberModelItem>}'

    I get about a million of these, all in binder.cpp and all related to objects that seem like they should be pointers, but somehow they aren't recognized as such.
    Does anybody see what's wrong here?

  • I added a detailed description of the errors during compilation as well as small parts of the code "in a new thread": , hoping that somebody can help.

  • bq. EDIT2: The other version did indeed compile with Python 2.7.5! And I was able to run some of the examples, PyGettingStarted and NicePyConsole.

    I don't understand. If you compiled and run these examples then you have success. Them uses two dll's generated by PythonQt project: PythonQt_QtAll.dll and PythonQt.dll . These files and (libPythonQt_QtAll.a and libPythonQt.a) are all what you need.

  • The other version of PythonQt is supposedly compatible with Python 3, so I'd prefer that.

Log in to reply