bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
937 stars 250 forks source link

[measure] ImportError: cannot import name 'Mapping' from 'collections' #746

Closed RubenKelevra closed 2 years ago

RubenKelevra commented 2 years ago
Traceback (most recent call last):
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure.py", line 21, in <module>
    from light_controller.hue import HueLightController
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/light_controller/hue.py", line 6, in <module>
    from PyInquirer import Separator
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/PyInquirer/__init__.py", line 6, in <module>
    from prompt_toolkit.token import Token
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/__init__.py", line 16, in <module>
    from .interface import CommandLineInterface
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/interface.py", line 19, in <module>
    from .application import Application, AbortAction
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/application.py", line 8, in <module>
    from .key_binding.bindings.basic import load_basic_bindings
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/key_binding/bindings/basic.py", line 9, in <module>
    from prompt_toolkit.renderer import HeightIsUnknownError
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/renderer.py", line 11, in <module>
    from prompt_toolkit.styles import Style
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/styles/__init__.py", line 8, in <module>
    from .from_dict import *
  File "/home/rubenk/dev/homeassistant-powercalc/utils/measure/measure/lib/python3.10/site-packages/prompt_toolkit/styles/from_dict.py", line 9, in <module>
    from collections import Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

I got this error after following the venv setup process. Python version is 3.10.4.

bramstroker commented 2 years ago

Seems to be caused by a change in the collection API in Python 3.10. So currently the measure script will only work with Python 3.9 max. See https://stackoverflow.com/questions/69381312/in-vs-code-importerror-cannot-import-name-mapping-from-collections

I did have a quick look if we can add support for 3.10 however the underlying library I used for the question/answer wizard does not support it and is unmaintained. https://github.com/CITGuru/PyInquirer. I have found a similar library which seems to be a good replacement and has Python 3.10 support and is also maintained. https://github.com/magmax/python-inquirer. But it is no drop in replacement, so we need to refactor some code. I will create an issue to add to the backlog.

For now I would suggest to downgrade to Python 3.9 or run Python 3.9 next to 3.10 when you are using 3.10 for other purposes.

I will also add a note to the readme.

RubenKelevra commented 2 years ago

@bramstroker or you just merge the PyInquirer code into this repo and update the corresponding elements you need?

Not sure what's less work. :)

RubenKelevra commented 2 years ago

For now I would suggest to downgrade to Python 3.9 or run Python 3.9 next to 3.10 when you are using 3.10 for other purposes.

Well, that's not possible – as python 3.10 is shipped in my distro. I guess I just have to use the docker image?

bramstroker commented 2 years ago

@bramstroker or you just merge the PyInquirer code into this repo and update the corresponding elements you need?

Not sure what's less work. :)

Yes that would also be a possible solution.

bramstroker commented 2 years ago

For now I would suggest to downgrade to Python 3.9 or run Python 3.9 next to 3.10 when you are using 3.10 for other purposes.

Well, that's not possible – as python 3.10 is shipped in my distro. I guess I just have to use the docker image?

Yes the docker image is the preferred way of running anyway, but I thought you wanted to do some development on the measure tool and that would be the reason you wanted to run it native.

When you want to edit the code locally and run Python 3.9 you could also run python 3.9 docker image https://hub.docker.com/_/python, and volume map the util measure sources into this container. Then you don't have to install python 3.9 on your system as your distro already ships with 3.10.