虚拟环境下无法通过 Jupyter Notebook 连接 Python

卡琳娜1117

我在我的工作文件夹和一个 jupyter notebook 中创建了一个虚拟环境,但我的 jupyter notebook 似乎没有连接到 Python。

我通过运行以下命令在此目录(C:\Users\MyName\Documents\DataScience\Code)中创建了一个虚拟环境:

pip install virtualenv
virtualenv .venv
.venv\Scripts\activate

然后,我安装了以下两个软件包。

pip install jupyter
pip install jupyterlab

安装 jupyter 后,我通过在命令提示符下输入“jupyter notebook”打开了 jupyter notebook,它工作正常。当我输入“Jupyter Notebook”时,浏览器会弹出。但是,如果我尝试通过单击“新建”下拉菜单并选择“Python 3”在 jupyter 浏览器中创建一个新笔记本,则会出现错误。它让我打开一个新笔记本,但它引发了内核错误。它仍然在右上角显示 Python 3 (ipykernel),但带有一个看起来像炸弹图标的黑点。

通常,当我创建一个新的 jupyter notebook 时,它只会说没有 ipykernel 的 Python 3。我试过“pip(or conda) install pywin32”,但没有用。如何解决此错误并将我的笔记本连接到 Python?我的计算机上有 Python3.8,并且想将 jupyter notebook 连接到没有 ipykernel 的常规 Python 3。

仅供参考,这是我在单击内核错误时看到的错误消息。

Traceback (most recent call last):
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\web.py", line 1704, in _execute
    result = await result
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\handlers.py", line 69, in post
    model = yield maybe_future(
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 98, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 769, in run
    yielded = self.gen.throw(*exc_info)  # type: ignore
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 110, in start_kernel_for_session
    kernel_id = yield maybe_future(
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\tornado\gen.py", line 762, in run
    value = future.result()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 176, in start_kernel
    kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\multikernelmanager.py", line 186, in start_kernel
    km.start_kernel(**kwargs)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\manager.py", line 337, in start_kernel
    kernel_cmd, kw = self.pre_start_kernel(**kw)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\manager.py", line 286, in pre_start_kernel
    self.write_connection_file()
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\connect.py", line 466, in write_connection_file
    self.connection_file, cfg = write_connection_file(self.connection_file,
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_client\connect.py", line 136, in write_connection_file
    with secure_write(fname) as f:
  File "c:\users\MyName\anaconda3\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_core\paths.py", line 461, in secure_write
    win32_restrict_file_to_user(fname)
  File "c:\users\MyName\Documents\DataScience\Code\.venv\lib\site-packages\jupyter_core\paths.py", line 387, in win32_restrict_file_to_user
    import win32api
ImportError: DLL load failed while importing win32api: The specified procedure could not be found.



谢谢你。

账单

Jupyter Notebook 确保 IPython 内核可用,但您必须手动添加具有不同 Python 版本或虚拟环境的内核。

pip install --user ipykernel

接下来,您可以通过键入以下内容将虚拟环境添加到 Jupyter:

python -m ipykernel install --user --name=myenv

这应该打印以下内容:

Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/myenv

在此文件夹中,您将找到一个 kernel.json 文件,如果您正确执行所有操作,该文件应如下所示:

 {
 "argv": [
   "/home/user/anaconda3/envs/myenv/bin/python",
   "-m",
   "ipykernel_launcher",
   "-f",
   "{connection_file}"
  ],
  "display_name": "myenv",
  "language": "python"
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Jupyter Notebook:由于Websocket连接失败,因此无法连接到服务器

无法在Jupyter Notebook中创建Python 3 Notebook

Jupyter Notebook:与内核的连接无限重启

无法连接到Docker服务的Jupyter Notebook

无法在Windows 10中连接到Tensorflow Jupyter Notebook

激活虚拟环境并启动批处理文件中的jupyter Notebook

虚拟环境中的Jupyter Notebook没有看到虚拟环境包

在OSx上使用虚拟环境设置Jupyter Notebook的最佳方法是什么

无法启动Jupyter Notebook

无法使用Jupyter Notebook运行python代码

如何将virutualenvwrapper与Jupyter Notebook连接

无法在AWS上连接Jupyter Notebook

docker容器中的python jupyter Notebook连接到selenium / standalone-chrome

Jupyter Notebook在conda基础下工作,但不在虚拟环境中工作

使用pyspark从Jupyter Notebook连接SQL DW

在Jupyter Notebook中使用Python虚拟环境

Jupyter Notebook无法识别熊猫库:Python

Jupyter Notebook和虚拟环境的奇怪行为

Jupyter Notebook Q#内核未连接

虚拟环境库在Jupyter Notebook中不会更新吗?

Conda环境和Jupyter Notebook,Python 3.9.2

Python Bloomberg API无法从iPython Notebook连接

AWS上的Jupyter Notebook无法连接到内核

如何使conda虚拟环境持久且可用于Jupyter Notebook之类的工具?

无法停止 Jupyter Notebook

未在 Jupyter-notebook 中创建的 Python 虚拟环境

无法从虚拟环境运行 jupyter

Java 内核无法连接到 jupyter notebook

如何在 vscode jupyter notebook 中使用创建的 python 虚拟环境