Re: Python dependencies in add-ons


Andy B.
 

Hi,

 

This generally works. However, it has one major problem that NVDA won’t allow add-on authors to overcome. If a dependency requires modules/libraries that NVDA removed from the standard Python library, all of the included Python libraries that attempt to import such removed libraries will fail. For example, trying to import bs4, lxml, or cssutils will terminally fail because NVDA removed html.parser and xml.parser from the standard library while maintaining the rest of the html and xml modules. Attempting to reimport the original html or xml standard Python libraries into an add-on also fails because it will cause a namespace conflict with the NVDA provided html and xml libraries. As a result, either one can exist, but not both. I attempted this and was forced to choose from a few different options:

 

  • Use NVDA’s version of html and xml with parsers removed.
  • Use my version of html or xml and ignore NVDA’s version.
  • Rename the original html or xml standard Python libraries and manage 3rd party dependencies myself.
  • Give up and consider it a lost cause.

I took the last option: give up because it is a lost cause. Using NVDA’s version of html and xml breaks the imported libraries that require html.parser or xml.parser. On the other hand, using the original Python provided versions breaks NVDA. Renaming the originals to something different seems like a ton of unwanted work because I would have to trudge through all the bs4/cssutils/lxml code and rename html.parser to something else, and after all of that, we can’t be sure it will work. If someone has a fix or workaround for this problem, I am all ears. It would bring a whole new level to my add-on.

 

 

Sent from Mail for Windows 10

 

From: Sean
Sent: Monday, June 8, 2020 1:40 PM
To: nvda-addons@nvda-addons.groups.io
Subject: Re: [nvda-addons] Python dependencies in add-ons

 

example code:

addonNamePathxX = os.path.abspath(os.path.dirname(__file__)) ^ #addon folder path
sys.path.append(addonNamePathX)

import numpy

import opencv #example modules, Not available in NVDA.

del sys.path[-1]

 

......

#content code

 

On 08/06/2020 20:33, Sean via groups.io wrote:

Hi  Subham,

I think your work will be a revolutionary thing.
Because screen readers have been working with the same logic for 20 years.
Good luck.

There is no separate tool to manage the dependencies of NVDA addons.

In addon's __init__.py file, the path of the modules is added with sys.path.append.
Addon  writers often use this.

One thing I want to add is;
All required modules should be in the same folder.

You can write C ++ codes to Python with a tool like Cython.
Or the code section written in C ++ can be compiled as a DLL.
In this way, we can use the functions of the DLL with Ctypes.
Ctypes is more commonly used in NVDA source.

On 08/06/2020 14:19, Shubham Jain wrote:

Hello!

As part of my GSoC project, I am writing an add-on that allows users to get descriptions of images. To work, the ML models depend on some python libraries like Numpy, Pillow, onnxruntime and OpenCv. My questions are:

  • Is it possible to package these libraries in an add-on?
  • Since I only require a few specific functions from these libraries, is it possible to only package those parts into the add-on?


Alternatively, the models could be converted to run using native C++ by depending on the LibTorch library. Is it possible to write add-ons in native C++?

regards,
Shubham Jain

--

Sean

👨🦯 I’m student and programmer. I write often Python, sometimes Go and rarely C++.

--

Sean

👨🦯 I’m student and programmer. I write often Python, sometimes Go and rarely C++.

 

Join nvda-addons@nvda-addons.groups.io to automatically receive all group messages.