Topics

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

zvonimir stanečić, 9a5dsz
 

Ok...

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?

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of Joseph Lee
Sent: Sunday, August 4, 2019 5:48 AM
To: nvda-devel@groups.io
Subject: [nvda-devel] 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

 

Hi,

Syntax still works. For relative import, it’ll be “from . import something as somethingelse”.

Cheers,

Joseph

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of zvonimir stanecic, 9a5dsz
Sent: Sunday, August 4, 2019 3:43 AM
To: nvda-devel@groups.io
Subject: Re: [nvda-devel] NVDA add-ons and Python 3: proper relative import syntax that works across Python versions

 

Ok...

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?

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of Joseph Lee
Sent: Sunday, August 4, 2019 5:48 AM
To: nvda-devel@groups.io
Subject: [nvda-devel] 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

 

Hello all,


I think it is important to add that the "from .something import *" import style is discouraged style. These import statements will cause warnings for the flake8 linter for example, as the linter does not know what names are imported when imported just everything.


Regards,

Leonard

Op 4-8-2019 om 16:31 schreef Joseph Lee:

Hi,

Syntax still works. For relative import, it’ll be “from . import something as somethingelse”.

Cheers,

Joseph

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of zvonimir stanecic, 9a5dsz
Sent: Sunday, August 4, 2019 3:43 AM
To: nvda-devel@groups.io
Subject: Re: [nvda-devel] NVDA add-ons and Python 3: proper relative import syntax that works across Python versions

 

Ok...

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?

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of Joseph Lee
Sent: Sunday, August 4, 2019 5:48 AM
To: nvda-devel@groups.io
Subject: [nvda-devel] 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

derek riemer
 

You can also do import appmodules.exename.foo, or import globalPlugins.myGlobalPluginName.foo, but there's a caviot. If the executable name has characters in it that aren't legal in module names, you can't import them this way and must use relative imports. We're working on a possible solution to this.

On Mon, Aug 5, 2019 at 12:36 AM Leonard de Ruijter <alderuijter@...> wrote:

Hello all,


I think it is important to add that the "from .something import *" import style is discouraged style. These import statements will cause warnings for the flake8 linter for example, as the linter does not know what names are imported when imported just everything.


Regards,

Leonard

Op 4-8-2019 om 16:31 schreef Joseph Lee:

Hi,

Syntax still works. For relative import, it’ll be “from . import something as somethingelse”.

Cheers,

Joseph

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of zvonimir stanecic, 9a5dsz
Sent: Sunday, August 4, 2019 3:43 AM
To: nvda-devel@groups.io
Subject: Re: [nvda-devel] NVDA add-ons and Python 3: proper relative import syntax that works across Python versions

 

Ok...

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?

 

From: nvda-devel@groups.io <nvda-devel@groups.io> On Behalf Of Joseph Lee
Sent: Sunday, August 4, 2019 5:48 AM
To: nvda-devel@groups.io
Subject: [nvda-devel] 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



--
Derek Riemer
Improving the world one byte at a time!        ⠠⠊⠍⠏⠗⠕⠧⠬ ⠮ ⠸⠺ ⠐⠕ ⠃⠽⠞⠑ ⠁⠞ ⠁ ⠐⠞⠖
•    Accessibility enthusiast.
•    Proud user of the NVDA screen reader.
•    Open source enthusiast.
•    Skier.

•    Personal website: https://derekriemer.com