# 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:** 1\
**Showing post:** 3

<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()))
```

---

_[View the full topic](https://forums.ankiweb.net/t/best-practice-for-add-ons-using-third-party-packages/1585)._
