Menu

Problems including further Python scripts

Help
superfisi
2011-03-18
2013-04-06
  • superfisi

    superfisi - 2011-03-18

    Hello there,

    a colleague of mine already posted this in the section "Open Discussion".

    Since we still habe this problem, I am gonne repost here. :)

    We already succeeded in calling a python script. In the script "TestScript01.py" we defined a class TestScript. So we first evaluate the script by calling…

    PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule();
    mainModule->evalFile("TestScript01.py");
    PythonQtObjectPtr testClass = mainModule.evalScript("TestScript()\n", Py_eval_input);

    Now we changed our script. Our class TestScript inherits from a new class Test Base in file TestBase.py.

    When we start the system we now get…

    "NameError"
    ": "
    "name 'TestBase' is not defined"

    What can we do to let PythonQt know about our new base class? Meaning sth. like setting an include-path with those paths that contain for example our base class.

    Looking into PythonQt.h we found the following function…

      //! sets the __path__ list of a module to the given list (important for local imports)
      void setModuleImportPath(PyObject* module, const QStringList& paths);

    The comment seems to point into the right direction… but what is the PyObject* here?

    Maybe we are looking in the wrong direction…

    I would be glad for any kind of help.

    Thank you very much

    Alex and Frank

     
  • Chris Bevan

    Chris Bevan - 2011-03-18

    Hi guys,

    Sounds like this is a Python problem rather than one with PythonQt.  The Python interpreter doesn't know about your class.

    Your test script needs to ensure it knows about your TestBase class.  Generally you'd probably want an "import TestBase" in your test script, and then derive from "TestBase.TestBase" or such.  Alternatively you could run the TestBase script in PythonQt's main module before running your test script if you like.

    I'd suggest getting this going in a standard Python shell first, as that would make your life easier.

    - Chris

     
  • Florian Link

    Florian Link - 2011-03-18

    When you run a python script from the shell, it adds the local path of the file that is started to the sys.path and thus allows to import relative to that script.

    If you are embedding Python with PythonQt, you have to take care that the sys.path contains a list of the Python paths from where you want to import your modules. You can do this by calling PythonQt::addSysPath() to add individual directories, or by calling PythonQt::overwriteSysPath and passing in a complete list of directories (including the standard python libs, if you need them). Adding your working/script directory will allow to import files from that directory using "import", as Chris explained above.
    You need to do this before you eval any file, otherwise the paths are not available.

    By the way, instead of

    mainModule.evalScript("TestScript()\n", Py_eval_input);

    you can also do a

    mainModule.call("TestScript")

    which will call the constructor of your TestScript class and return the instance.

    regards,
    Florian

     
  • superfisi

    superfisi - 2011-03-22

    Hi guys,

    first of all: Thank you for your quick replies, you helped us a lot. Using the PythonQt-Method PythonQt::addSysPath() or alternativelly modify the sys-path from within the Python script made the include work.

    Nevertheless we are still struggling with the include paths… because we wanted to use the Qt-Ressource-Management in form of qrc-files. Within Qt we are used to address paths like this…

    :/Scripts/SubDir/AnotherSubDir

    From what we know this means that the Python-scripts are compiled (or somehow put) into the binary - and this is what we want, meaning we don't want our customers to be able to see our scripts in the file system.

    Within Qt the ressource-system works fine, in our case it gets difficult - do you see any possibility to include files from a Qt-Ressource-location?

    Thanks in advance,

    Alex

     
  • Florian Link

    Florian Link - 2011-03-23

    This is supported and works like this:

    // install the default importer that comes with PythonQt, it supports opening files via QFile and thus supports the Qt resource system
    PythonQt::self()->installDefaultImporter()

    // add the root of the Qt resource to sys.path so that you can import from there
    PythonQt::self()->addSysPath(":")

    // you are set, just do "import anything" in Python and it will look at ":/anything.py" or ":/anything/__init__.py"

    You may as well add subdirs of the resource system, e.g.

    PythonQt::self()->addSysPath(":/Scripts")

    to allow imports from there instead of the root directory.

    regards,
    Florian

     
  • superfisi

    superfisi - 2011-03-25

    Thank you, Florian - now we got what we wanted.

    Again: Fantastic piece of software!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.