Plugin with import module

Hi,
I’m testing plugin. Quite easy to implement at the beginning.
Just creating a folder in .config/tryton/x.x/plugins/ and then adding an init.py file like this:

import gettext

_ = gettext.gettext


def test(data):
    print("My first plugin")

def get_plugins(model):
    return [
        (_('Test Plugin'), test),
        ]

Now, i’ve some code in another file test.py:

class Test():
    def __init__(self):
        print("Hello World!")

I try to import this file in my init.py but then my plugin is no more working…

import gettext
from . import text

_ = gettext.gettext


def test(data):
    print("My first plugin")
    test.Test()

def get_plugins(model):
    return [
        (_('Test Plugin'), test),
        ]

I’m thinking about path problem… Thanks for help!

Indeed it’s working when adding path to sys:

import gettext
import sys
sys.path.insert(0, "/home/user/.config/tryton/x.x/plugins")
from myplugin.test import Test
...

Is there another method ?

You could use the same machinery as in tryton/plugins/__init__.py but I would not recommend it because it is too complex.
But I would not recommend neither your solution because you may let tryton client load any “unsafe” module from this user directory. I think the best is to keep plugin into a single file.

Hum, my plugin is quite complex. Code will be unreadable if i have to include all classes in the same file.

Another question linked to plugin: how to include external modules used in plugin ? mainly for Windows client…
I mean, in my module i’m doing:

import request
...

How to have “request” module in the Tryton Windows environment ? (could apply to any external module used by plugin)

You have to bundle your dependencies inside the plugin.

I find this really weird … importing standard libraries is working, but not importing files in the same folder.

What I did for Windows is using a subprocess to do things. Otherwise you have to build the client yourself. I needed for example win32com.client. Because Python was installed with Anaconda, the library was already there.

The plugin execute a report in Tryton, stored the data and used a subprocess to call an external windows application (label program), put the report data into that application and print a label.

(edit) Just found the thread on the old mailinglist https://groups.google.com/forum/#!searchin/tryton-dev/plugins|sort:date/tryton-dev/iDnqeD83jJ0/ICL-QuzhBwAJ

So it means to have each dependencies inside a directory in the plugin directory ?
And then i need to add this code in my init.py :

sys.path.insert(0, "/home/user/.config/tryton/x.x/plugins")

It is probably better to include in the sys.path only the sub-directory containing your plugin.
Or I think the safest way it to follow https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path

Could be done this way:

import gettext
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import test # NOQA
...