Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to deploy Qt application with embedded Python?
Forum Updated to NodeBB v4.3 + New Features

How to deploy Qt application with embedded Python?

Scheduled Pinned Locked Moved Solved Installation and Deployment
6 Posts 3 Posters 2.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roni219
    wrote on 3 Jun 2016, 03:33 last edited by roni219 6 Mar 2016, 03:35
    #1

    Hello,

    My Qt application is in C++, and calls the embedded python API. Suppose the application source code is /my/dir/foo.cc, and the python code is /my/dir/bar.py.

    foo.cc contains the following (simplified) lines:

    // Add /my/dir/ - full path to bar.py - to python module search path
    PyRun_SimpleString("import sys\nsys.path.append('/my/dir')");
    
    // Generate name for bar.py module:
    PyObject * file = PyString_FromString("bar");
    
    // Import Python module bar:
    PyObject * module = PyImport_Import(file);
    

    The application builds and runs successfully as long as bar.py is located in /my/dir/.

    The question is: how to include the python file into the installation package?

    Copying bar.py to the destination directory is not an option because it will be readable by users.

    I tried to make bar.py the application resource:

    In foo.pro:

    RESOURCES = foo.qrc
    

    In foo.qrc:

    <qresource>
    <file>bar.py</file>
    </qresource>
    

    And instead of adding /my/dir to sys.path, I added /full/path/to/binary (i.e destination directory).

    The method did not work - PyImport_Import() could not find the bar module.

    Any suggestions - either on how to make the "resource" approach work, or an alternative approach - will be greatly appreciated.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Jun 2016, 05:04 last edited by
      #2

      Do I understand you right: you don't want your users to be able to read the Python code?
      Embedding the Python code into your executable will not help in this case: user can just view your executable in an hex-editor or extract strings from it.
      I can only think of two solutions:

      • Obfuscate the Python code and put it in installation directory
      • Encrypt Python code

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roni219
        wrote on 3 Jun 2016, 05:25 last edited by
        #3

        Thank you for the prompt response, @jsulm! Yes, the python code can be extracted from the executable, but this is not a big concern. Everything can be reverse-engineered... I do not want the .py files sitting in the directory, where everybody can easily access them. But if it is possible to embed them into the executable, this will be good enough for me.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Jun 2016, 22:09 last edited by
          #4

          Hi,

          The most simple solution that comes to mind is to copy the file from the resource to the hard drive using e.g. a QTemporaryFile or put it in an adequate folder provided by QStandardPaths. Then you can setup your python path properly.

          Hope it helps

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply 4 Jun 2016, 05:51
          0
          • S SGaist
            3 Jun 2016, 22:09

            Hi,

            The most simple solution that comes to mind is to copy the file from the resource to the hard drive using e.g. a QTemporaryFile or put it in an adequate folder provided by QStandardPaths. Then you can setup your python path properly.

            Hope it helps

            R Offline
            R Offline
            roni219
            wrote on 4 Jun 2016, 05:51 last edited by roni219 6 Apr 2016, 05:52
            #5

            Thank you, @SGaist, this is a good solution!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 4 Jun 2016, 19:57 last edited by
              #6

              You're welcome !

              If that answers your question, then please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              3 Jun 2016, 03:33

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved