cri*_*tor 6 ipython python-3.x jupyter jupyter-notebook python-poetry
我正在尝试在诗歌虚拟环境中使用 Jupyter Notebooks,但是ModuleNotFoundError在尝试导入已经由诗歌安装(和管理)的 python 包时出现问题。
我正在运行 python v3.8.5、poetry v1.1.13 和 PyCharm 2022.1.1。
我已经添加了jupyter和ipykernel作为依赖项,并且可以确认它们已添加到两者中pyproject.toml:
jupyter = "^1.0.0"
ipykernel = "^6.13.0"
Run Code Online (Sandbox Code Playgroud)
和poetry.lock:
[[package]]
name = "ipykernel"
version = "6.13.0"
description = "IPython Kernel for Jupyter"
category = "dev"
optional = false
python-versions = ">=3.7"
[[package]]
name = "jupyter"
version = "1.0.0"
description = "Jupyter metapackage. Install all the Jupyter components in one go."
category = "dev"
optional = false
python-versions = "*"
Run Code Online (Sandbox Code Playgroud)
我使用诗歌安装了 ipython 内核:
poetry run ipython kernel install --user --name=poetry_kernel
我启动了 Jupyter Notebook:
poetry run jupyter notebook
我选择Kernel>>Change kernel并poetry_kernel运行import numpy(安装在诗歌虚拟环境中)但没有找到numpy。我在尝试导入由诗歌管理的其他包时遇到了同样的错误。
然后我发现我需要kernel.json在poetry_kernel.
我进行了修改kernel.json,现在它具有以下内容:
{
"argv": [
"C:\\Users\\my-user-name\\AppData\\Local\\pypoetry\\Cache\\virtualenvs\\project-name--o0gsFoF-py3.8\\Scripts\\python.exe",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "poetry_kernel",
"language": "python",
"metadata": {
"debugger": true
},
"env": {
"CONDA_DLL_SEARCH_MODIFICATION_ENABLE": 1
}
}
Run Code Online (Sandbox Code Playgroud)
我关闭了 jupyter 笔记本,然后重新运行poetry run jupyter notebook,选择poetry_kernel并重新运行,import numpy但出现了相同的错误。
我可以确认我能够import numpy在 PyCharm 的 Python 控制台中poetry run some_module.py导入 numpy。
我使用诗歌的经验很少(在迁移到诗歌之前,我之前从 virtualenv 环境启动了 jupyter 笔记本),并且无法找到任何线索。
我应该补充一点,不想简单地pip install numpy(我需要安装许多其他的),此外我想利用诗歌的依赖管理。
边注:
运行后poetry run jupyter notebook出现以下错误和警告:
[I 10:44:24.123 NotebookApp] 内核启动:220004ff-7963-4f47-abad-59d74789ff98,名称:poetry_kernel [IPKernelApp] 错误 | 没有注册这样的通信目标:jupyter.widget.control [IPKernelApp] 警告 | 没有这样的通讯:6b457f5c-1066-48f7-b795-4fa80cf50947
这显然是无害的,所以我假设ModuleNotFoundError与回溯无关。我无法找到警告和错误的任何解决方案。
我认为答案可能与这个问题类似,其中核心问题是诗歌 venv 不会定义软件包,如果它们已经在您的系统lib/python3.{version}安装中。
您可以通过查看该{my_poetry_venv}/lib/python3.{version}/site-packages文件夹来验证这一点。如果 numpy 不存在,它就不会出现在你的 jupyter 内核中。
我已经在我的系统上验证了:
sitepackages当您尝试时,Jupyter 内核只会找到 venv 中的包子集import {some_package}。| 归档时间: |
|
| 查看次数: |
1395 次 |
| 最近记录: |