[Solved] Jupyter Kernel error: kernel Cannot Connect—-import win32api; ImportError: DLL load failed…

The kernel is not connected

Error original

Traceback (most recent call last):
  File "D:\Anaconda\envs\tf2\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "D:\Anaconda\envs\tf2\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "D:\Anaconda\envs\tf2\lib\site-packages\traitlets\config\application.py", line 844, in launch_instance
    app.initialize(argv)
  File "D:\Anaconda\envs\tf2\lib\site-packages\traitlets\config\application.py", line 87, in inner
    return method(app, *args, **kwargs)
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 625, in initialize
    self.write_connection_file()
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 236, in write_connection_file
    iopub_port=self.iopub_port, control_port=self.control_port)
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_client\connect.py", line 136, in write_connection_file
    with secure_write(fname) as f:
  File "D:\Anaconda\envs\tf2\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_core\paths.py", line 461, in secure_write
    win32_restrict_file_to_user(fname)
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_core\paths.py", line 387, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed: 找不到指定的程序。
[I 16:31:42.557 NotebookApp] KernelRestarter: restarting kernel (1/5), new random ports
[IPKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\YJF\\AppData\\Roaming\\jupyter\\runtime\\kernel-edcd3c8f-6a6b-4512-a376-4a71c0a0b1d5.json'
Traceback (most recent call last):
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 261, in init_connection_file
    self.load_connection_file()
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_client\connect.py", line 494, in load_connection_file
    info = json.load(f)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[I 16:31:45.568 NotebookApp] KernelRestarter: restarting kernel (2/5), new random ports
[IPKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\YJF\\AppData\\Roaming\\jupyter\\runtime\\kernel-edcd3c8f-6a6b-4512-a376-4a71c0a0b1d5.json'
Traceback (most recent call last):
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 261, in init_connection_file
    self.load_connection_file()
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_client\connect.py", line 494, in load_connection_file
    info = json.load(f)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[I 16:31:48.584 NotebookApp] KernelRestarter: restarting kernel (3/5), new random ports
[IPKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\YJF\\AppData\\Roaming\\jupyter\\runtime\\kernel-edcd3c8f-6a6b-4512-a376-4a71c0a0b1d5.json'
Traceback (most recent call last):
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 261, in init_connection_file
    self.load_connection_file()
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_client\connect.py", line 494, in load_connection_file
    info = json.load(f)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[I 16:31:51.595 NotebookApp] KernelRestarter: restarting kernel (4/5), new random ports
[IPKernelApp] ERROR | Failed to load connection file: 'C:\\Users\\YJF\\AppData\\Roaming\\jupyter\\runtime\\kernel-edcd3c8f-6a6b-4512-a376-4a71c0a0b1d5.json'
Traceback (most recent call last):
  File "D:\Anaconda\envs\tf2\lib\site-packages\ipykernel\kernelapp.py", line 261, in init_connection_file
    self.load_connection_file()
  File "D:\Anaconda\envs\tf2\lib\site-packages\jupyter_client\connect.py", line 494, in load_connection_file
    info = json.load(f)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "D:\Anaconda\envs\tf2\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "D:\Anaconda\envs\tf2\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
[W 16:31:54.603 NotebookApp] KernelRestarter: restart failed
[W 16:31:54.603 NotebookApp] Kernel edcd3c8f-6a6b-4512-a376-4a71c0a0b1d5 died, removing from map.
[W 16:32:22.967 NotebookApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:22.977 NotebookApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.024 NotebookApp] Config option `template_path` not recognized by `TocExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.031 NotebookApp] Config option `template_path` not recognized by `TocExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.052 NotebookApp] Config option `template_path` not recognized by `LenvsHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.062 NotebookApp] Config option `template_path` not recognized by `LenvsHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.088 NotebookApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.098 NotebookApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.151 NotebookApp] Config option `template_path` not recognized by `LenvsLatexExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.157 NotebookApp] Config option `template_path` not recognized by `LenvsLatexExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.380 NotebookApp] Config option `template_path` not recognized by `LenvsSlidesExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.388 NotebookApp] Config option `template_path` not recognized by `LenvsSlidesExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.515 NotebookApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.523 NotebookApp] Config option `template_path` not recognized by `ExporterCollapsibleHeadings`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.570 NotebookApp] Config option `template_path` not recognized by `TocExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.577 NotebookApp] Config option `template_path` not recognized by `TocExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.598 NotebookApp] Config option `template_path` not recognized by `LenvsHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.609 NotebookApp] Config option `template_path` not recognized by `LenvsHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.633 NotebookApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.643 NotebookApp] Config option `template_path` not recognized by `LenvsTocHTMLExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.694 NotebookApp] Config option `template_path` not recognized by `LenvsLatexExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.700 NotebookApp] Config option `template_path` not recognized by `LenvsLatexExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.921 NotebookApp] Config option `template_path` not recognized by `LenvsSlidesExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?
[W 16:32:23.928 NotebookApp] Config option `template_path` not recognized by `LenvsSlidesExporter`.  Did you mean one of: `extra_template_paths, template_name, template_paths`?

Error reporting reason

    import win32api
ImportError: DLL load failed: The specified program could not be found.

This is caused by the incompatibility between the python version and the pywin32 version.

Solution:

Reinstall the latest version of pywin32 = = 301 in the TF2 environment with CONDA error. Solution: CONDA install pywin32 = = 301

Read More: