NVDA add-ons and Python 3: proper relative import syntax that works across Python versions

 

Hi all,

 

The following directive is based on more add-on compatibility investigations and a thread on add-ons list regarding Toolbars Explorer (incompatible at the moment). Because many add-ons are affected by this one, please apply fixes as soon as possible (if you intend to maintain your add-ons past 2019):

 

NVDA add-on and Python 3 August 2019 directive no. 2: relative imports

 

Many add-ons are structured like a package. That is, there is an __init__ file that imports modules from the same folder or other packages. If you wish to import a module from the same folder, you would use a relative import.

 

In Python 2, relative import of the form “import something” works, but in Python 3, due to preference for absolute imports, the former form will no longer work.

 

Examples:

 

# Yes:

from . import something # Works across Python 2 and 3

from .module import contents # Works across Python 2 and 3

# Or add the path to the current folder to sys.path and do:

import something # Works across Python 2 and 3

# If aliasing i.e. importing everything from a module:

from .module import * # Works across Python 2 and 3

 

# No:

import something # Not allowed in Python 3 if importing a module from the same folder

from module import contents # Not allowed in Python 3 if importing a module from the same folder

from something import * # Not allowed in Python 3 if aliasing a module from the same folder

 

Affected and compatible add-ons:

 

  • Add-on Updater
  • Character Information
  • Clock and calendar Add-on for NVDA
  • Resource Monitor
  • Station Playlist
  • UnicodeBrailleInput
  • Windows 10 App Essentials
  • Possibly more

 

Affected and incompatible add-ons:

 

  • Clipspeak
  • Dual Voice
  • Lambda Add-On for NVDA
  • NoBeepsSpeechMode
  • TeamTalk Classic
  • Text Information
  • Tip of the Day
  • ToolbarsExplorer
  • Possibly more

 

There are others that are currently incompatible but work is under way to make them compatible (work in progress or planned). These include Place Markers, Clip Contents Designer, Braille Extender and others.

 

Next steps: authors should provide a fix by using relative import statement compatible with both Python 2 and 3.

 

Cheers,

Joseph

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