Jupyter Notebook的python3(ipykernel)启动失败报 “No module named ‘psutil._psutil_windows‘”

记录一个Jupyter Notebook启动时的报错

环境

win10 默认装的环境py3.9 32bit的,jupyter book安装在conda建立的虚拟环境py3.9(64bit)中

问题1

jupyter notebook 启动kernel一直显示reconnecting最后failed
wrong
每次启动时报错:ModuleNotFoundError: No module named 'psutil._psutil_windows'

Traceback (most recent call last):
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\kernelapp.py", line 51, in <module>
    from .ipkernel import IPythonKernel
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\ipkernel.py", line 19, in <module>
    from .comm.comm import BaseComm
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\comm\__init__.py", line 3, in <module>
    from .comm import Comm
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\comm\comm.py", line 14, in <module>
    from ipykernel.kernelbase import Kernel
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\kernelbase.py", line 35, in <module>
    import psutil
  File "C:\Users\meij1\AppData\Roaming\Python\Python39\site-packages\psutil\__init__.py", line 109, in <module>
    from . import _pswindows as _psplatform
  File "C:\Users\meij1\AppData\Roaming\Python\Python39\site-packages\psutil\_pswindows.py", line 35, in <module>
    from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS
ModuleNotFoundError: No module named 'psutil._psutil_windows'

根据信息发现是虚拟环境py3.9在导入psutil的时候是从外部本机环境的py39中导入的,结果发现这个环境中没有psutil这个module.

在base环境中使用pip重新安装psutil后重新尝试:
install

问题2

重新run了一遍报错换成了ImportError: DLL load failed while importing _psutil_windows: %1 is not a valid Win32 application:看起来是32位的程序跑在了64位系统上。

Traceback (most recent call last):
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\kernelapp.py", line 51, in <module>
    from .ipkernel import IPythonKernel
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\ipkernel.py", line 19, in <module>
    from .comm.comm import BaseComm
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\comm\__init__.py", line 3, in <module>
    from .comm import Comm
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\comm\comm.py", line 14, in <module>
    from ipykernel.kernelbase import Kernel
  File "C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9\lib\site-packages\ipykernel\kernelbase.py", line 35, in <module>
    import psutil
  File "C:\Users\meij1\AppData\Roaming\Python\Python39\site-packages\psutil\__init__.py", line 109, in <module>
    from . import _pswindows as _psplatform
  File "C:\Users\meij1\AppData\Roaming\Python\Python39\site-packages\psutil\_pswindows.py", line 35, in <module>
    from ._psutil_windows import ABOVE_NORMAL_PRIORITY_CLASS
ImportError: DLL load failed while importing _psutil_windows: %1 is not a valid Win32 application.

研究了一下,发现问题处在这个本机环境干扰了我的py3.9虚拟环境, 本机环境装的psutil是32bit,32b但是虚拟环境py3.9默认设置的是64bit 平台,看下面的platform:win-64

(py3.9) C:\Users\meij1\Videos>conda info
     active environment : py3.9
    active env location : C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs\py3.9
            shell level : 2
       user config file : C:\Users\meij1\.condarc
 populated config files : C:\Users\meij1\.condarc
          conda version : 4.12.0
    conda-build version : not installed
         python version : 3.9.7.final.0
       virtual packages : __win=0=0
                          __archspec=1=x86_64
       base environment : C:\Users\meij1\3D Objects\Anarchy\Miniconda3  (writable)
      conda av data dir : C:\Users\meij1\3D Objects\Anarchy\Miniconda3\etc\conda
  conda av metadata url : None
           channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\meij1\3D Objects\Anarchy\Miniconda3\pkgs
                          C:\Users\meij1\.conda\pkgs
                          C:\Users\meij1\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\meij1\3D Objects\Anarchy\Miniconda3\envs
                          C:\Users\meij1\.conda\envs
                          C:\Users\meij1\AppData\Local\conda\conda\envs
               **platform : win-64**
             user-agent : conda/4.12.0 requests/2.27.1 CPython/3.9.7 Windows/10 Windows/10.0.19042
          administrator : False
             netrc file : None
           offline mode : False

应该是两个环境的存在弄混了psutil,将本机的32bit pstuil卸载后,py3.6的psutil重新安装就没问题了。

问题解决

kernel 启动正常

[I 22:47:29.154 NotebookApp] Kernel started: 37ce7359-26a5-4911-acbe-6ae21e40bce3, name: python3
[I 22:49:29.675 NotebookApp] Saving file at /Jupyter Notebook/Untitled.ipynb

马后炮

  1. 根据报错信息分析具体哪个环境的问题base/py3.9…
  2. 建议装了minicoda/Anaconda之后本机的python环境可以删掉了,避免一些奇怪问题。
  3. 如何修改conda的平台信息。
conda config --env --set subdir win-32		# windows下
conda config --env --set subdir linux-32		# linux下

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年12月5日
下一篇 2023年12月5日

相关推荐

此站出售,如需请站内私信或者邮箱!