Re: NVDA add-ons and Python 3: proper relative import syntax that works across Python versions
toggle quoted messageShow quoted text
Syntax still works. For relative import, it’ll be “from . import something as somethingelse”.
From: email@example.com <firstname.lastname@example.org> On Behalf Of zvonimir stanecic, 9a5dsz
Sent: Sunday, August 4, 2019 3:43 AM
Subject: Re: [nvda-devel] NVDA add-ons and Python 3: proper relative import syntax that works across Python versions
Can you tell me,
What’s used in python 3 to import something as something?
What is the backward compatibility approach?
In python 2 we had:
Import foots as foo
What’s used in py3?
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.
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
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:
Affected and incompatible add-ons:
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.