Error when importing openpyxl

I’m using a 3rd party library called openpyxl to provide an excel spreadsheet interface. All of my custom libraries are stored in a ./Python/ folder local to the layout file for portability reasons.

Here’s my import code

from vcScript import *
import sys
if not sys.path[0] == 'Python':
sys.path.insert(0,'Python')

# Import Local modules here
import openpyxl

If I open a new instance of visual components, and load my layout, the library imports and works exactly as expected. However, sometimes if I recompile a script that imports the openpyxl library, I get an error:

Traceback (most recent call last):
  File "MasterController::PythonScript", line 7, in <module>
  File "Python\openpyxl\__init__.py", line 29, in <module>
    from openpyxl.workbook import Workbook
  File "Python\openpyxl\workbook\__init__.py", line 5, in <module>
    from .workbook import Workbook
  File "Python\openpyxl\workbook\workbook.py", line 8, in <module>
    from openpyxl.worksheet import Worksheet
  File "Python\openpyxl\worksheet\__init__.py", line 4, in <module>
    from .worksheet import Worksheet
  File "Python\openpyxl\worksheet\worksheet.py", line 34, in <module>
    from openpyxl.cell import Cell
  File "Python\openpyxl\cell\__init__.py", line 4, in <module>
    from .cell import Cell, WriteOnlyCell
  File "Python\openpyxl\cell\cell.py", line 45, in <module>
    from openpyxl.styles import numbers, is_date_format
  File "Python\openpyxl\styles\__init__.py", line 5, in <module>
    from .alignment import Alignment
  File "Python\openpyxl\styles\alignment.py", line 6, in <module>
    from openpyxl.descriptors import Bool, MinMax, Min, Alias, NoneSet
  File "Python\openpyxl\descriptors\__init__.py", line 5, in <module>
    from .sequence import Sequence
  File "Python\openpyxl\descriptors\sequence.py", line 5, in <module>
    from openpyxl.xml.functions import Element
  File "Python\openpyxl\xml\functions.py", line 78, in <module>
    register_namespace(DCTERMS_PREFIX, DCTERMS_NS)
  File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\xml\etree\ElementTree.py", line 1025, in register_namespace
    if re.match("ns\d+$", prefix):
AttributeError: 'NoneType' object has no attribute 'match'

What’s even weirder is that if I add whitespace to the ElementTree.py file and then recompile my script, it will throw errors for other parts of ElementTree.py. I haven’t found a way to make this error stop without restarting VC.

I can reliably reproduce the error.

Open the model Digital Factory.vcmx
Clear All
Reopen -> Error

I modified ElementTree.register_namespace method:

def register_namespace(prefix, uri):
  import re
  print re
  if re.match("ns\d+$", prefix):
    raise ValueError("Prefix format reserved for internal use")
  for k, v in _namespace_map.items():
    if k == uri or v == prefix:
      del _namespace_map[k]
  _namespace_map[uri] = prefix

On the first loading of my layout, it prints <module 're' from 'C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\re.py'>

On the second loading it throws an error

...  File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\xml\etree\ElementTree.py", line 1025, in register_namespace
      import re
  TypeError: 'NoneType' object is not callable

Digital-Factory.zip (9.53 MB)

I installed openpyxl using pip to a fresh python install, copied over the lib files to the visual components python folder (C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\lib), and then created a new layout with a new component with a PythonScript behaviour:

from vcScript import *
import openpyxl
def OnSignal( signal ):
  pass

def OnRun():
  pass

Loading this layout once works fine. Loading it a second time in the same instance of visual components produces the same error as before:

Reading model from file: C:\Users\gambordr\Desktop\test.vcmx
  Traceback (most recent call last):
    File "NewComponent::PythonScript", line 2, in <module>
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\__init__.py", line 29, in <module>
      from openpyxl.workbook import Workbook
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\workbook\__init__.py", line 5, in <module>
      from .workbook import Workbook
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\workbook\workbook.py", line 8, in <module>
      from openpyxl.worksheet import Worksheet
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\worksheet\__init__.py", line 4, in <module>
      from .worksheet import Worksheet
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\worksheet\worksheet.py", line 34, in <module>
      from openpyxl.cell import Cell
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\cell\__init__.py", line 4, in <module>
      from .cell import Cell, WriteOnlyCell
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\cell\cell.py", line 45, in <module>
      from openpyxl.styles import numbers, is_date_format
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\styles\__init__.py", line 5, in <module>
      from .alignment import Alignment
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\styles\alignment.py", line 6, in <module>
      from openpyxl.descriptors import Bool, MinMax, Min, Alias, NoneSet
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\descriptors\__init__.py", line 5, in <module>
      from .sequence import Sequence
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\descriptors\sequence.py", line 5, in <module>
      from openpyxl.xml.functions import Element
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\openpyxl\xml\functions.py", line 78, in <module>
      register_namespace(DCTERMS_PREFIX, DCTERMS_NS)
    File "C:\Program Files\Visual Components\Visual Components Professional 4.0\Python\Lib\xml\etree\ElementTree.py", line 1024, in register_namespace
      if re.match("ns\d+$", prefix):
  AttributeError: 'NoneType' object has no attribute 'match'

test.vcmx (5.67 KB)