# Plugin with import module

**URL:** https://discuss.tryton.org/t/plugin-with-import-module/2440
**Category:** Developer
**Created:** [March 12, 2020, 9:54am UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440 "2020-03-12T09:54:41Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [March 12, 2020, 9:54am UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/1 "2020-03-12T09:54:41Z")

</div>

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:

```auto
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:

```auto
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…

```auto
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!

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [March 12, 2020, 10:35am UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/2 "2020-03-12T10:35:15Z")

</div>

Indeed it’s working when adding path to sys:

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

```

Is there another method ?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 12, 2020, 12:24pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/4 "2020-03-12T12:24:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [March 12, 2020, 12:49pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/5 "2020-03-12T12:49:03Z")

</div>

> [@ced](#):
>
> 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:

```auto
import request
...

```

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

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 12, 2020, 12:53pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/6 "2020-03-12T12:53:53Z")

</div>

You have to bundle your dependencies inside the plugin.

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [March 12, 2020, 1:03pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/7 "2020-03-12T13:03:05Z")

</div>

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 [Redirecting to Google Groups](https://groups.google.com/forum/#!searchin/tryton-dev/plugins%7Csort:date/tryton-dev/iDnqeD83jJ0/ICL-QuzhBwAJ)

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [March 12, 2020, 1:20pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/8 "2020-03-12T13:20:10Z")

</div>

> [@ced](#):
>
> You have to bundle your dependencies inside the plugin.

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 :

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

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [March 12, 2020, 1:34pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/9 "2020-03-12T13:34:27Z")

</div>

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](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path)

---

<div class="post-metadata">

### Author: ![maxx](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/maxx/32/2320_2.png) [@maxx](https://discuss.tryton.org/u/maxx)
#### Post date: [March 12, 2020, 2:04pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/10 "2020-03-12T14:04:43Z")

</div>

> [@ced](#):
>
> It is probably better to include in the sys.path only the sub-directory containing your plugin.

Could be done this way:

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

```

---

<div class="post-metadata">

### Author: ![htgoebel](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/htgoebel/32/1256_2.png) [@htgoebel](https://discuss.tryton.org/u/htgoebel)
#### Post date: [May 15, 2025, 7:23pm UTC](https://discuss.tryton.org/t/plugin-with-import-module/2440/11 "2025-05-15T19:23:51Z")

</div>

Adding the directory to sys.path is a bad idea, as this add the files in that directory as global modules. Te correct way is using importlib as shown in the stackoverflow answer ced linked.
