microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
149 stars 35 forks source link

Import error when importing black fom environment #473

Closed WarmongeringBeaver closed 6 months ago

WarmongeringBeaver commented 7 months ago

When black-formatter.importStrategy is fromEnvironment, this error message appears:

2024-02-29 17:54:19.850 [info] Interpreter from setting black-formatter.interpreter: python
2024-02-29 17:54:19.850 [info] Server: Stop requested
2024-02-29 17:54:19.856 [info] Interpreter from setting black-formatter.interpreter: python
2024-02-29 17:54:19.858 [info] Server run command: python /import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/tool/lsp_server.py
2024-02-29 17:54:19.858 [info] Server: Start requested.
2024-02-29 17:54:19.943 [info] [Error - 17:54:19] Server process exited with code 0.
2024-02-29 17:54:20.066 [info] Traceback (most recent call last):
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/tool/lsp_server.py", line 64, in <module>
    import lsp_edit_utils as edit_utils
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/tool/lsp_edit_utils.py", line 10, in <module>
    from lsprotocol import types as lsp
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/libs/lsprotocol/types.py", line 14, in <module>
    import attrs
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/libs/attrs/__init__.py", line 3, in <module>
    from attr import (
ImportError: cannot import name 'AttrsInstance' from 'attr' (/usr/lib/python3/dist-packages/attr/__init__.py)

2024-02-29 17:54:20.088 [info] [Error - 17:54:20] Server process exited with code 1.
2024-02-29 17:54:20.089 [info] [Error - 17:54:20] Server initialization failed.
2024-02-29 17:54:20.089 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-02-29 17:54:20.089 [info] [Info  - 17:54:20] Connection to server got closed. Server will restart.
2024-02-29 17:54:20.089 [info] true
2024-02-29 17:54:20.089 [info] [Error - 17:54:20] Black Formatter client: couldn't create connection to server.
2024-02-29 17:54:20.089 [info]   Message: Pending response rejected since connection got disposed
  Code: -32097 
2024-02-29 17:54:20.287 [info] Traceback (most recent call last):
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/tool/lsp_server.py", line 64, in <module>
    import lsp_edit_utils as edit_utils
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/tool/lsp_edit_utils.py", line 10, in <module>
    from lsprotocol import types as lsp
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/libs/lsprotocol/types.py", line 14, in <module>
    import attrs
  File "/import/bc_users/biocomp/tboyer/.vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/libs/attrs/__init__.py", line 3, in <module>
    from attr import (
ImportError: cannot import name 'AttrsInstance' from 'attr' (/usr/lib/python3/dist-packages/attr/__init__.py)

(the log then repeats itself a few times).

Calling black from the CL with my environment activated works like a charm.

Apparently the extensions calls packages in /usr/lib/python3/dist-packages which might be the problem? Also I don't understand why calls to vscode-server/extensions/ms-python.black-formatter-2024.0.1/bundled/ are still made when black-formatter.importStrategy is fromEnvironment.

karthiknadig commented 7 months ago

The issue is that the version of attrs you have is not compatible. When you use fromEnvironment all packages are first picked from your environment.

Here are the two options: Option 1: update the attrs in your environment to latest. Option 2: set black-formatted.importStrategy to useBundled, and set black-formatter.path to point to black. This will use black from your environment but still use internal packages for communication.

karthiknadig commented 6 months ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.