使用 Python 3 在 Jupyter Notebook 中使用相对导入从位于另一个目录中的模块导入本地函数 [英] Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3

查看:34
本文介绍了使用 Python 3 在 Jupyter Notebook 中使用相对导入从位于另一个目录中的模块导入本地函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的目录结构

I have a directory structure similar to the following

meta_project
    project1
        __init__.py
        lib
            module.py
            __init__.py
    notebook_folder
        notebook.jpynb

notebook.jpynb 中工作时,如果我尝试使用相对导入来访问 module.py 中的函数 function():

When working in notebook.jpynb if I try to use a relative import to access a function function() in module.py with:

from ..project1.lib.module import function

我收到以下错误:

SystemError                               Traceback (most recent call last)
<ipython-input-7-6393744d93ab> in <module>()
----> 1 from ..project1.lib.module import function

SystemError: Parent module '' not loaded, cannot perform relative import

有什么办法可以使用相对导入来让它工作吗?

Is there any way to get this to work using relative imports?

注意,笔记本服务器是在 meta_project 目录级别实例化的,因此它应该可以访问这些文件中的信息.

Note, the notebook server is instantiated at the level of the meta_project directory, so it should have access to the information in those files.

还要注意,至少按照最初的意图,project1 不被认为是一个模块,因此没有 __init__.py 文件,它只是表示文件系统目录.如果问题的解决方案需要将其视为一个模块并包含一个 __init__.py 文件(甚至是一个空白的),那很好,但这样做还不足以解决问题.

Note, also, that at least as originally intended project1 wasn't thought of as a module and therefore does not have an __init__.py file, it was just meant as a file-system directory. If the solution to the problem requires treating it as a module and including an __init__.py file (even a blank one) that is fine, but doing so is not enough to solve the problem.

我在机器之间共享这个目录,相对导入允许我在任何地方使用相同的代码,&我经常使用笔记本进行快速原型设计,因此涉及将绝对路径组合在一起的建议不太可能有帮助.

I share this directory between machines and relative imports allow me to use the same code everywhere, & I often use notebooks for quick prototyping, so suggestions that involve hacking together absolute paths are unlikely to be helpful.

这与 Python 3 中的相对导入不同,后者讨论相对通常在 Python 3 中导入,尤其是从包目录中运行脚本.这与在 jupyter notebook 中尝试调用另一个目录中本地模块中的函数有关,该目录具有不同的一般和特定方面.

This is unlike Relative imports in Python 3, which talks about relative imports in Python 3 in general and – in particular – running a script from within a package directory. This has to do with working within a jupyter notebook trying to call a function in a local module in another directory which has both different general and particular aspects.

推荐答案

我和你在 这个笔记本,我想在其中以 DRY 方式说明相邻模块功能的用法.

I had almost the same example as you in this notebook where I wanted to illustrate the usage of an adjacent module's function in a DRY manner.

我的解决方案是通过在笔记本中添加这样的片段来告诉 Python 附加模块导入路径:

My solution was to tell Python of that additional module import path by adding a snippet like this one to the notebook:

import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)

这允许您从模块层次结构中导入所需的函数:

This allows you to import the desired function from the module hierarchy:

from project1.lib.module import function
# use the function normally
function(...)

请注意,如果没有 project1/lib/ 文件夹,则需要将它们添加到空的 __init__.py 文件中已经.

Note that it is necessary to add empty __init__.py files to project1/ and lib/ folders if you don't have them already.

这篇关于使用 Python 3 在 Jupyter Notebook 中使用相对导入从位于另一个目录中的模块导入本地函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆