Importing python modules

Hi out there,

I’m trying to build a SFOS app using QtQuick, Silica and some Python-Modules for the first time now.

My Project has the following structure:

/project
    /qml
        /pages
            page1.qml
            page2.qml
        /cover
            cover.qml
        main.qml
    /icons
    /translation
    /python
        /module1
            module1.py
        /module2
            module2.py

When I try to import the python modules in main.qml like this

addImportPath(Qt.resolvedUrl(’…/python/module1/’));
importModule(‘module1’, function () {});

the IDE tells me “Unhandled PyOtherSide error: Cannot import module: module1 (No module named ‘module1’)”.

Could anyone help me out, please?

Thanks in advance,
BGK

I think the python subdir must be under the qml tree otherwise qtquick can’t locate it.

Or you need to add some magic to the paths Qt.resolvedUrl searches.

Not 100% positive though.

Is that just a typo here or do you have three dots in your source?

There is a tutorial you might have missed Creating an application in Python | Sailfish OS Documentation.

Thank you nephros and martyone.

How do you mean this?

I’m using two dots in my source code as well as in my original post. Seems to be Discourse is changing them to three. Strange…

Well, this was the starting point. But in this example the py-file is placed inside the /qml-folder.

Is there some kind of howto / best practice where to place python components?

None that I am aware of, but a quick search revealed some existing implementations similar to what you are trying to achieve like https://github.com/poetaster/harbour-simplecrop/blob/main/qml/pages/EffectsBench.qml#L42 .

Thanks for that.

In this case (Imageworks) the python stuff is placed in the /py folder within the /qml folder and referenced from a file in /qml/pages/ (by ‘…/py’). That’s why it works.

Silly question: is your “python” folder packaged and deployed at the expected location? :slight_smile: I don’t see much space for issues just because of placing the python folder beside the qml folder instead of placing in under the qml folder.

Yup. :upside_down_face:

Well, I tried several approaches now and it seems that the python folder has to be placed within the qml folder:

/project
    /qml
        /pages
            page1.qml
            page2.qml
        /cover
            cover.qml
        main.qml
        /python
            /module1
                 module1.py
            /module2
                 module2.py
    /icons
    /translation

In contrast to the structure in my first post, the modules can be accessed then without problems.

Why? Only Jolla knows… :wink:

Although I do a bunch (or inherited a bunch) of python, one of the persons better qualified to demonstrate how it’s done is @cypherpunks , see:

TLDR: addImportPath(Qt.resolvedUrl(’…/…/src’));

The src directory is located at the same ‘level’ as the qml directory

ls /usr/share/harbour-musicex/
qml  src

Needless to say, kinda captain obvious.

1 Like