Search code examples
pythonpython-3.xvisual-studio-codepython-extensions

Sorting imports fails on Python VSCode extension


Background

I have an anaconda environment that has Python 3.7 installed. I have a file with some imports out of order which I want VScode to order when pressing CRTL+s.

However, instead or ordering the imports, there is a crash and nothing happens.

Problem

When I press CRTL+s on my VScode, I get a pop up saying the Python extension crashes. After some investigation, this is the stack-trace I found:

Error 2021-10-15 16:00:05: Traceback (most recent call last):
  File "/home/user/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/sortImports.py", line 12, in <module>

Traceback (most recent call last):
  File "/home/user/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/sortImports.py", line 12, in <module>

Error 2021-10-15 16:00:05: Failed to format imports for '/home/user/Workplace/work/my-project/cars/client.py'. Traceback (most recent call last):
  File "/home/user/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/sortImports.py", line 12, in <module>

Error 2021-10-15 16:00:05:     import isort.main
  File "/home/user/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/isort/__init__.py", line 21, in <module>
    from . import settings
  File "/home/user/.vscode/extensions/ms-python.python-2021.10.1336267007/pythonFiles/lib/python/isort/settings.py", line 5, in <module>
    import configparser
  File "/home/user/anaconda3/envs/my-project/lib/python3.7/configparser.py", line 147, in <module>
    import re
  File "/home/user/anaconda3/envs/my-project/lib/python3.7/re.py", line 124, in <module>
    import enum
  File "/home/user/anaconda3/envs/my-project/lib/python3.7/enum.py", line 2, in <module>
    from types import MappingProxyType, DynamicClassAttribute
  File "/home/user/Workplace/work/my-project/cars/types.py", line 1, in <module>
    from pathlib import Path
  File "/home/user/anaconda3/envs/my-project/lib/python3.7/pathlib.py", line 13, in <module>
    from urllib.parse import quote_from_bytes as urlquote_from_bytes
  File "/home/user/anaconda3/envs/my-project/lib/python3.7/urllib/parse.py", line 627, in <module>
    _asciire = re.compile('([\x00-\x7f]+)')
AttributeError: module 're' has no attribute 'compile'

I don't see any issues with my setup. For more information, this is my conda env setup:

name: my-project
channels:
  - pytorch
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=4.5=1_gnu
  - blas=1.0=mkl
  - ca-certificates=2021.9.30=h06a4308_1
  - certifi=2021.10.8=py37h06a4308_0
  - cpuonly=1.0=0
  - intel-openmp=2021.3.0=h06a4308_3350
  - ld_impl_linux-64=2.35.1=h7274673_9
  - libffi=3.3=he6710b0_2
  - libgcc-ng=9.3.0=h5101ec6_17
  - libgomp=9.3.0=h5101ec6_17
  - libstdcxx-ng=9.3.0=hd4cf53a_17
  - libuv=1.40.0=h7b6447c_0
  - mkl=2021.3.0=h06a4308_520
  - ncurses=6.2=he6710b0_1
  - ninja=1.10.2=hff7bd54_1
  - openssl=1.1.1l=h7f8727e_0
  - pip=21.2.2=py37h06a4308_0
  - pytest-mock=3.6.1=pyhd3eb1b0_0
  - python=3.7.11=h12debd9_0
  - pytorch=1.9.1=py3.7_cpu_0
  - readline=8.1=h27cfd23_0
  - setuptools=58.0.4=py37h06a4308_0
  - sqlite=3.36.0=hc218d9a_0
  - tk=8.6.11=h1ccaba5_0
  - torchaudio=0.9.1=py37
  - typing-extensions=3.10.0.2=hd3eb1b0_0
  - typing_extensions=3.10.0.2=pyh06a4308_0
  - wheel=0.37.0=pyhd3eb1b0_1
  - xz=5.2.5=h7b6447c_0
  - zlib=1.2.11=h7b6447c_3
  - pip:
    - argcomplete==1.12.3
    - attrs==21.2.0
    - babel==2.9.1
    - beautifulsoup4==4.10.0
    - black==21.9b0
    - charset-normalizer==2.0.4
    - click==7.1.2
    - colorama==0.4.4
    - colorclass==2.2.0
    - commonmark==0.9.1
    - dataclasses==0.6
    - docutils==0.16
    - humanize==3.11.0
    - idna==3.2
    - imagesize==1.2.0
    - importlib-metadata==4.8.1
    - iniconfig==1.1.1
    - jinja2==3.0.1
    - m2r==0.2.1
    - markupsafe==2.0.1
    - mistune==0.8.4
    - mypy==0.910
    - mypy-extensions==0.4.3
    - nodeenv==1.6.0
    - numpy==1.21.2
    - packaging==21.0
    - pathspec==0.9.0
    - pillow==8.3.2
    - platformdirs==2.4.0
    - pluggy==1.0.0
    - py==1.10.0
    - pygments==2.10.0
    - pyparsing==2.4.7
    - pyright==0.0.10
    - pytest==6.2.5
    - pytest-describe==2.0.0
    - pytz==2021.1
    - pyyaml==5.4.1
    - regex==2021.10.8
    - requests==2.26.0
    - requests-toolbelt==0.9.1
    - responses==0.13.4
    - rich==10.12.0
    - six==1.16.0
    - snowballstemmer==2.1.0
    - soupsieve==2.2.1
    - tests==0.7
    - toml==0.10.2
    - tomli==1.2.1
    - torch==1.9.1+cpu
    - typed-ast==1.4.3
    - types-pyyaml==5.4.12
    - types-requests==2.25.11
    - upolygon==0.1.6
    - urllib3==1.26.6
    - zipp==3.5.0
variables:
  PYTHONPATH: .
prefix: /home/user/anaconda3/envs/my-project

At first I thought it could be a dependency issue, so I started removing every extra dependency I could find.

However, even after doing it, My vscode extension is still not working.

Questions

  • What is wrong with my setup?
  • How can I fix it?

Solution

  • Reason

    The problem here is that I had broken dependencies which would not allow me to do any updates nor new installs.

    This had to do with having packages from both conda and pip. Some of them play nice together, some don't.

    Solution

    My solution, was unfortunately, rather atomic. I deleted the environment and created a new one with Python 3.7. Upon doing that, I also added an extra conda channel conda-forge which I recommend instead of pip.

    Once I did that I installed all the dependencies and packages using conda and it worked.

    Commands

    Here are the command I used:

    conda create -n my-project python=3.7
    conda config --env --add channels conda-forge
    conda env config vars set PYTHONPATH="."
    conda deactivate
    conda activate my-project 
    

    And then conda isntall ... the packages you need.