# Best Practice for Add-ons using Third-Party Packages

**URL:** <https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585>\
**Category:** Add-ons\
**Created:** [July 30, 2020, 7:45am UTC](https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585 "2020-07-30T07:45:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tansongchen](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tansongchen](https://forums.ankiweb.net/u/tansongchen)\
**Post date:** [July 30, 2020, 7:45am UTC](https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585/1 "2020-07-30T07:45:22Z")

</div>

I am an add-on developer and recently I would like to use [pygments](https://pygments.org/) to highlight code in Anki within my add-on. In principle I can copy the whole package folder together with my addon and use relative import to achieve this.

However unfortunately, the pygments package is designed to use absolute import within the package. For example, in `pygments/filter/ __init__.py`, I found something like this:

```python
from pygments.plugin import find_plugin_lexers

```

This would suggest that if I use relative import at the top level, I should also modify everywhere in the pygments package to use relative import instead of absolute import, which involves a great number of work and is not scalable.

Is there any better practice to use a “internally absolute import” third-party package?

---

<div class="post-metadata">

**Author:** ![ijgnd](https://avatars.discourse-cdn.com/v4/letter/i/bbe5ce/32.png) [@ijgnd](https://forums.ankiweb.net/u/ijgnd)\
**Post date:** [July 30, 2020, 8:25am UTC](https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585/2 "2020-07-30T08:25:48Z")

</div>

this is not a real answer.

Just a note: glutanimate’s popular [syntax highlighting](https://ankiweb.net/shared/info/1463041493) add-on also uses the pygments package and uses [sys.path.insert](https://github.com/glutanimate/syntax-highlighting/blob/49d93b9e1dca0f2ff9f07cc5c4c873cd3621772e/src/syntax_highlighting/main.py#L25).

---

<div class="post-metadata">

**Author:** ![hkr](https://avatars.discourse-cdn.com/v4/letter/h/c6cbf5/32.png) [@hkr](https://forums.ankiweb.net/u/hkr)\
**Post date:** [July 30, 2020, 1:42pm UTC](https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585/3 "2020-07-30T13:42:32Z")

</div>

As an alternative solution, according to [Python documentation](https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly), you can directly import a local source file using `spec_from_file_location` function.

Let’s say `pygments` folder is located in `libs` folder as follows:

```
+---addons21
| +---your_addon_root
| | +---libs
| | | +---pygments

```

you can import it as follows:

```
import sys
import pathlib
import importlib.util

addon_root = pathlib.Path( __file__ ).resolve().parent
pygments_source = addon_root / 'libs' / 'pygments' / ' __init__.py'
spec = importlib.util.spec_from_file_location('pygments', pygments_source)
module = importlib.util.module_from_spec(spec)
sys.modules['pygments'] = module
spec.loader.exec_module(module)

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter

code = 'print("Hello World")'
print(highlight(code, PythonLexer(), HtmlFormatter()))
```
