[nvda-addons] Re: Working on a new addon, problems with some imports

  • From: James Teh <jamie@xxxxxxxxxxxx>
  • To: nvda-addons@xxxxxxxxxxxxx
  • Date: Thu, 30 Jul 2015 20:57:39 +1000

The problem is that NVDA itself includes some (but not all) modules in the xml package. This means that NVDA's copy of the xml package will take precedence. You should be able to do something like this before what you're already doing:
import xml
xml.__path__.append(os.path.join(PLUGIN_DIR, "lib", "xml"))
This makes Python search your copy of the xml package when looking for modules in the xml package.

Jamie

On 30/07/2015 7:26 PM, Hrvoje Katić wrote:

Hi all,
I'm working on a new addon that will download youtube videos and save them into chosen video or audio format automatically with a single keystroke. It works as follows: You open a URL with a video, press nvda+shift+y and ther URL will be autodetected and the download begins automatically, so you can download while listening video with no need to paste url or switch to another window.
To achieve this, I'm using youtube_dl which works both as a single app and a Python module. I don't wanna run it as an app, because it'll pop-up command prompt while downloading, and also I'll not be able to catch exceptions that may eventually happen, so I'm running it as a Python module. Unfortunately, youtube_dl uses some python modules not included with NVDA by default, so I copied manually all required external modules from python27/lib into a lib subfolder inside the globalPlugin's folder, and I added this subfolder to the sys.path inside the addon's __init__.py as follows:
PLUGIN_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(PLUGIN_DIR, "lib"))
import youtube_dl
del sys.path[-1]
This allowed youtube_dl to import all required modules including urllib2, json and others, but the problem begins when it comes to import xml.etree.ElementTree. Here's what I get when i open log viewer:
ERROR - globalPluginHandler.listPlugins (11:06:50):
Error importing global plugin youtube
Traceback (most recent call last):
File "globalPluginHandler.pyc", line 22, in listPlugins
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\__init__.py", line 31, in <module>
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\youtube_dl\__init__.py", line 16, in <module>
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\youtube_dl\options.py", line 8, in <module>
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\youtube_dl\downloader\__init__.py", line 3, in <module>
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\youtube_dl\downloader\common.py", line 9, in <module>
File "C:\Users\pc3\AppData\Roaming\nvda\addons\Youtube\globalPlugins\youtube\youtube_dl\utils.py", line 32, in <module>
ImportError: No module named etree.ElementTree
So, the utils.py submodule of youtube_dl cannot find xml.etree, although I've double checked that xml folder is copied properly with all the subfolders.
I tried to fix this in many ways, so I really have no more ideas on how to get all external submodules imported properly when running the plugin.
Thanks for any help!


--
James Teh
Executive Director, NV Access Limited
Ph +61 7 3149 3306
www.nvaccess.org
Facebook: http://www.facebook.com/NVAccess
Twitter: @NVAccess
SIP: jamie@xxxxxxxxxxxx

----------------------------------------------------------------
NVDA add-ons: A list to discuss add-on code enhancements and for reporting bugs.
Community addons are available from: http://addons.nvda-project.org
To send a message to the list: nvda-addons@xxxxxxxxxxxxx
To change your list settings/unsubscribe:
//www.freelists.org/list/nvda-addons
To contact list moderators: nvda-addons-moderators@xxxxxxxxxxxxx

Other related posts: