Loading

Python报错 ImportError: DLL load failed while importing win32api: %1 不是有效的 Win32 应用程序 的解决方法

今天在用jupyter notebook 的时候发生了kernel error,点开之后提示了以下报错信息

Traceback (most recent call last):
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\web.py", line 1704, in _execute
    result = await result
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "E:\Anaconda3\lib\site-packages\notebook\services\sessions\handlers.py", line 69, in post
    model = yield maybe_future(
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "E:\Anaconda3\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 88, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "E:\Anaconda3\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 100, in start_kernel_for_session
    kernel_id = yield maybe_future(
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\tornado\gen.py", line 234, in wrapper
    yielded = ctx_run(next, result)
  File "E:\Anaconda3\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 168, in start_kernel
    super(MappingKernelManager, self).start_kernel(**kwargs)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_client\multikernelmanager.py", line 185, in start_kernel
    km.start_kernel(**kwargs)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 309, in start_kernel
    kernel_cmd, kw = self.pre_start_kernel(**kw)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_client\manager.py", line 256, in pre_start_kernel
    self.write_connection_file()
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_client\connect.py", line 468, in write_connection_file
    self.connection_file, cfg = write_connection_file(self.connection_file,
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_client\connect.py", line 138, in write_connection_file
    with secure_write(fname) as f:
  File "E:\Anaconda3\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_core\paths.py", line 435, in secure_write
    win32_restrict_file_to_user(fname)
  File "C:\Users\boqia\AppData\Roaming\Python\Python38\site-packages\jupyter_core\paths.py", line 361, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed while importing win32api: %1 不是有效的 Win32 应用程序。

主要先看后两行报错信息,我们知道jupyter notebook再import win32api的时候报错了

这可能是由于python版本是64位的,但电脑中已经存在了32位的pywin32所导致的

我们在命令行中运行以下代码来卸载pywin32

pip uninstall pywin32

由于可能电脑中存在多个版本的pywin32,所以卸载完成后在运行以下代码查看时候还有没卸载的pywin32

pip show pywin32

如果返回了如下结果,则说明电脑中仍然存在pywin32

C:\Users\boqia>pip show pywin32
Name: pywin32
Version: 227
Summary: Python for Window Extensions
Home-page: https://github.com/mhammond/pywin32
Author: Mark Hammond (et al)
Author-email: mhammond@skippinet.com.au
License: PSF
Location: e:\anaconda3\lib\site-packages
Requires:
Required-by: jupyter-core, xlwings, pypiwin32

这时候我们只需要在执行一次卸载命令就行了

pip uninstall pywin32

再彻底卸载完pywin32之后,我们在使用以下命令,安装所兼容的pywin32

pip install pywin32

安装完成后,应该就不会再报错了.

posted @ 2020-12-17 08:45  brokyz  阅读(2530)  评论(0编辑  收藏  举报