为什么我无法从jupyter笔记本中导入多重速率?

发布于 2025-02-06 13:33:30 字数 559 浏览 3 评论 0原文

我正在尝试从使用Pycharm配置的VENV环境中运行Jupyter笔记本。但是,当我尝试从Jupyter笔记本运行代码时,我会在尝试导入模块时会遇到以下

import multipledispatch
ModuleNotFoundError: No module named 'multipledispatch'

错误任何问题。

当我从内检查已安装的软件包列表时,使用

python -m pip list

该模块的笔记本按预期出现。如果我尝试从笔记本中安装模块,

!python -m pip install multipledispatch

请告诉我已经满足了要求,但是,我仍然无法导入模块。

我能够导入创建VirtualEnv后安装的其他非标准模块,因此似乎仅限于多重ISPATCH模块。有人有任何想法可能导致这一问题或如何修复它吗?

I'm trying to run a jupyter notebook from within a venv environment configured using PyCharm. However, when I try to run the code from the jupyter notebook I'm getting the following error when trying to import a module I know to exist

import multipledispatch
ModuleNotFoundError: No module named 'multipledispatch'

If I open a terminal, activate the virtual environment and run the code manually in Python it works without any problems.

When I check the list of installed packages from within the notebook using

python -m pip list

the module appears as expected. If I try to install the module from within the notebook using

!python -m pip install multipledispatch

it tells me that requirements are already satisfied, however, I'm still unable to import the module.

I'm able to import other non-standard modules which I installed after creating the virtualenv, so this appears to be limited to the multipledispatch module. Does anyone have any ideas what might be causing this or how it might be fixed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2025-02-13 13:33:30

我弄清楚了这件事。即使我将python内核设置为基本项目中的 venv ,但 site-packages 目录目录均未添加到python路径中。

如下

  • 我的目录结构
    • main.py
    • 软件包1
      • package1.py
    • 软件包2
      • package2.py
    • 笔记本
      • 笔记本book.ipynb
    • VENV
      • bin
      • lib

i通过手动向项目和pip添加路径( site-packages *目录在笔记本开始时。 >将其与笔记本电脑相同

目录

pip_path值为我可以在笔记本上运行的虚拟机的站点包装目录的位置。根。

import sys
import os


def fix_paths():
    module_path = os.path.abspath(os.pardir)
    pip_path = os.path.join(module_path, "venv", "lib", "python3.9", "site-packages")

    for path in [module_path, pip_path]:
        if path not in sys.path:
            sys.path.append(path)

在每个笔记本的开头我添加了以下内容

from project_path import fix_paths

fix_paths()

I figured out what's happening with this. Even though I'd set the python kernel to use the venv from the base project, the site-packages directory wasn't being added to the Python path.

My directory structure was as follows

  • projectDir
    • main.py
    • package1
      • package1.py
    • package2
      • package2.py
    • notebooks
      • notebook.ipynb
    • venv
      • bin
      • lib

I fixed this by manually adding the path to the project and the pip (site-packages* directory at the beginning of the notebook. I created a file called project_path.py and placed it in the same directory as the notebook.

The os.pardir reference allows me to add the projectDir to the notebook's path, this ensures that my custom packages can be called from within the notebook.

The pip_path value gives me the location of the site-packages directory for the virtual machine in which the notebook runs. By adding both of these to the python path I can import packages in my notebooks just as I can in the root dir.

import sys
import os


def fix_paths():
    module_path = os.path.abspath(os.pardir)
    pip_path = os.path.join(module_path, "venv", "lib", "python3.9", "site-packages")

    for path in [module_path, pip_path]:
        if path not in sys.path:
            sys.path.append(path)

At the beginning of each notebook I added the following to run the code and fix up the paths

from project_path import fix_paths

fix_paths()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文