'ImportError: No module named _thread

Compiling python2 in vscode gives an error. But when I compile python3 it succeeds.

print('test')

returns: ImportError: No module named _thread

PS C:\source>  c:; cd 'c:\source'; & 'C:\Python27\python.exe' 'c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy\launcher' '52037' '--' 'c:\source\test.py' 
Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy\__main__.py", line 43, in <module>
    from debugpy.server import cli
  File "c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy/../debugpy\server\__init__.py", line 9, in <module>
    import debugpy._vendored.force_pydevd  # noqa
  File "c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy/../debugpy\_vendored\force_pydevd.py", line 37, in <module>
    pydevd_constants = import_module('_pydevd_bundle.pydevd_constants')
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_constants.py", line 362, in <module>  
    from _pydev_bundle._pydev_saved_modules import thread, threading
  File "c:\Users\keinblue\.vscode\extensions\ms-python.python-2022.4.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\_pydev_saved_modules.py", line 94, in <module>
    import _thread as thread;    verify_shadowed.check(thread, ['start_new_thread', 'start_new', 'allocate_lock'])
ImportError: No module named _thread


Solution 1:[1]

There is an issue with the vscode python extension version 2022.4.0

just downgrade to version 2022.2.1924087327 and it will work as it works for me now

Just follow these steps:

  • Go to extensions.
  • Click on Gear Icon for the installed extension
  • Click on Install Another Version
  • select the version you wish to install

enter image description here

Solution 2:[2]

I think its the VS Code Python Extension. In VS Code select the Python Extension and select the Uninstall dropdown to install a previous version, the 2022.2.xxx fixed this issue.install previous version

Solution 3:[3]

Today I had the same issue. These are the steps I executed to fix it.

  1. Update the extension to the version v2022.6.2
  2. Reload VSCode
  3. Restart the involved application

It smoothly worked to me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HDJEMAI
Solution 2 Steve
Solution 3 Federico Scaltriti