ImportError: cannot import name 'write_connection_file' from 'jupyter_client'

发布时间 2023-10-10 20:07:11作者: emanlee

ImportError: cannot import name 'write_connection_file' from 'jupyter_client' (/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/jupyter_client/__init__.py)
[I 15:25:53.529 NotebookApp] KernelRestarter: restarting kernel (4/5), new random ports
/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/jupyter_client/__init__.py:23: UserWarning: Could not import submodules
  warnings.warn("Could not import submodules")
Traceback (most recent call last):
  File "/home/software/anaconda3/envs/mydlenv/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/software/anaconda3/envs/mydlenv/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/ipykernel/__init__.py", line 5, in <module>
    from .connect import *  # noqa
  File "/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/ipykernel/connect.py", line 12, in <module>
    from jupyter_client import write_connection_file
ImportError: cannot import name 'write_connection_file' from 'jupyter_client' (/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/jupyter_client/__init__.py)
[W 15:25:56.539 NotebookApp] KernelRestarter: restart failed

 

使用命令jupyter kernelspec list可以查看当前的kernel

$ jupyter kernelspec list

删除Jupyter kernel

使用命令

$ jupyter kernelspec remove kernelname

可以删除目标kernel

 

有一个单独的目录保存kernel文件,里面的kernel.json文件保存了jupyter notebook kernel的配置信息:(下面是已经配好之后的信息,原来的已经删掉了)

/* 文件路径:/Users/flyhero/anaconda3/share/jupyter/kernels/python3/kernel.json */
{
 "argv": [
  "/Users/flyhero/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
} 

使用命令jupyter kernelspec remove 'kernelname'可以删除指定的kernel

使用命令jupyter kernelspec list可以查看jupyter所有的kernel

 

向Jupyter中添加conda虚拟环境

之前觉得自己菜的抠脚,也不敢深究,能用就行。现在有时间,便了解的更仔细一点。jupyter notebook运行需要的kernel和conda创建的虚拟环境并不能完全互通。利用conda创建了虚拟环境,但是启动jupyter notebook之后却找不到虚拟环境。实际上是由于在虚拟环境下缺少kernel.json文件。解决方案如下:

首先安装ipykernel:conda install ipykernel

在虚拟环境下创建kernel文件:conda install -n 环境名称 ipykernel

激活conda环境:source activate 环境名称

将环境写入notebook的kernel中:

python -m ipykernel install --user --name 环境名称 --display-name "你想为kernel添加的名称"

如果经常需要用jupyter notebook,那么最好在创建虚拟环境的时候便安装好ipykernel:

conda create -n 环境名称 python=3.5 ipykernel

===========================================================================
 
在项目中我们常常需要用到多个环境,因此在JupyterNotebook中配置多个内核是我们必须要学会的。为了举例子,我们这里利用conda创造了两个环境分别是py3.8和py3.7.

第一步:了解Jupyter Kernel如何启动的

  当我们使用conda  activate py3.8 启动环境之后,如果当前环境中安装了jupyter和ipykernel两个包,我们就可以使用命令:jupyter notebook,在当前环境启动jupyter服务。此时在网页右上角中我们可以看到创建Notebook的地方出现了Python3(ipykernel),这意味着此时创建的jupyter notebook文件默认的内核是py3.8环境的内核(其实很容易想到,因为我们启动jupyter notebook这个服务就是在py3.8这个环境下启动的所以默认的内核自然应该是py3.8的)。

第二步:创建新内核

  如果我们想要使用其他的环境,意味着我们需要重新conda  activate 环境名,再启动jupyter notebook服务,再创建Notebook,这一套操作十分麻烦,有没有更简单的呢?如果可以在同一个jupyter notebook服务链接到其他环境内核(不再使用默认的内核)是不是就可以不用这么麻烦了呢?

  按照本文的例子,我现在想要在py3.8的环境里,创建Notebook时使用py3.7的内核。该怎么操作呢?

  首先,启动py3.7环境(conda  activate py3.7 ),并且在该环境(py3.7)下安装jupyter和ipykernel两个包。接下来我们需要使用命令行将当前环境内核(py3.7)添加到.local/share/jupyter/kernels/配置文件中,在任何环境里启动的jupyter都是可以看到该配置文件,因此只要把内核环境(此处是py3.7)注册到这个文件中,接下来无论在什么环境启动jupyter notebook服务都是可以该内核的。命令行如下:

python -m ipykernel install --user --name py3.7(这里名字可以随便取可以是hello等)

  如果顺利,会返回以下命令:

   这时候就能说明该环境(py3.7)已经注册到.local/share/jupyter/kernels/配置文件中了。这里要注意,命令行中--name 后面名字可以随便写,但是我们一定要在指定环境中输入以上命令行,如果你在python3.6环境中输入,此时注册的就是python3.6的内核(虽然内核名字叫做py3.7)。如果你在python3.9环境输入以上命令行,此时注册的就是3.9内核。

第三步:重启Jupyter服务

  重启jupyter服务之后(刷新页面也可以),我们就可以看到此处已经有其他的内核选项了。此时就说明内核添加成功了。

第四步:其他常用命令

   这里提供一些其他常用的命令。

  查看当前可用的kernel

jupyter kernelspec list

   使用这个命令需要注意,看下我的结果。

   箭头所指的环境是指当前环境下的jupyter内核,而从py3.7内核(第一行)的路径可以知道这个是公用的(根据前面的解释可以知道已经注册到了.local/share/jupyter/kernels/配置文件中),任何环境下都能看到这个py3.7这个内核。而第二行python3的这个内核其实就是当前环境的默认内核(如果你conda  activate 其他环境,这里会变的)。

  删除kernel:

jupyter kernelspec remove 内核名

 

 ========================================================================
ModuleNotFoundError: No module named 'ipython_genutils.py3compat'
卸载,然后,安装
 
ImportError: cannot import name 'Environment' from 'jinja2' (unknown location)
卸载,然后,安装
ImportError: cannot import name 'Markup' from 'markupsafe' (unknown location)

ModuleNotFoundError: No module named 'zmq.eventloop'
ModuleNotFoundError: No module named 'zmq.eventloop'
pip uninstall pyzmq
pip install pyzmq

ModuleNotFoundError: No module named 'pyrsistent'
ImportError: cannot import name 'JsonSchemaException' from 'fastjsonschema' (unknown location)

pip install --force-reinstall --no-deps fastjsonschema==2.16.2
 
========================================================================
mydlenv) [root@bin]# pip uninstall JsonSchemaException
WARNING: Skipping JsonSchemaException as it is not installed.
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
您在 /var/spool/mail/root 中有邮件
(mydlenv) [root@bin]# pip install JsonSchemaException
ERROR: Could not find a version that satisfies the requirement JsonSchemaException (from versions: none)
ERROR: No matching distribution found for JsonSchemaException
(mydlenv) [root@bin]# pip uninstall fastjsonschema
Found existing installation: fastjsonschema 2.16.2
ERROR: Cannot uninstall fastjsonschema 2.16.2, RECORD file not found. You might be able to recover from this via: 'pip install --force-reinstall --no-deps fastjsonschema==2.16.2'.
(mydlenv) [root@bin]# pip install --force-reinstall --no-deps fastjsonschema==2.16.2
Collecting fastjsonschema==2.16.2
  Using cached fastjsonschema-2.16.2-py3-none-any.whl (22 kB)
Installing collected packages: fastjsonschema
  Attempting uninstall: fastjsonschema
    Found existing installation: fastjsonschema 2.16.2
ERROR: Cannot uninstall fastjsonschema 2.16.2, RECORD file not found. You might be able to recover from this via: 'pip install --force-reinstall --no-deps fastjsonschema==2.16.2'.
 
 
/home/sqxiong/anaconda3/lib/python3.8/site-packages/opt_einsum-3.3.0.dist-info/RECORD

(mydlenv) [root@ibiomed bin]# locate fastjsonschema | grep my
/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/setuptools/config/_validate_pyproject/fastjsonschema_exceptions.py
/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/setuptools/config/_validate_pyproject/fastjsonschema_validations.py
/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/setuptools/config/_validate_pyproject/__pycache__/fastjsonschema_exceptions.cpython-38.pyc
/home/software/anaconda3/envs/mydlenv/lib/python3.8/site-packages/setuptools/config/_validate_pyproject/__pycache__/fastjsonschema_validations.cpython-38.pyc

 

REF

https://www.cnblogs.com/CircleWang/p/15882638.html

https://zhuanlan.zhihu.com/p/81605893