趣味で計算流砂水理 Computational Sediment Hydraulics for Fun Learning

数値計算とか河川工学とかプログラミングのことを書いています

MENU

未解決:公式の「Jupyter Notebooksをモジュールとしてインポートする方法」がWindows環境でうまく動かない ⇒ とりあえずの解決策あり

スポンサーリンク

 

 

 

本年もよろしくお願いします。



導入

Pythonユーザーであれば、.pyファイルのようにJupyter Notebooksファイル(.ipynb)をimportしたい場面はよくあります。

その方法は、以下の公式サイトで紹介されています。

Importing Jupyter Notebooks as Modules — Jupyter Notebook 7.3.2 documentation

また、いくつかの参考サイトにも示されています。

ところが、私のWindows環境だとどうも上手く動きませんでしたので、問題点と暫定的な解決策を示しておきます。(詳しい方教えて下さい) ちなみにLinuxubuntu)やGoogle Colobでは問題なく動作しました。

公式の方法を使用時の不具合

公式によると、

以下の3つの関数・クラスを使用してJupyter Notebooksファイルしています。

  • def find_notebook
    • モジュール名を受け取り、モジュールを返す。
  • class NotebookLoader
    • IPython変換を適用して、それぞれのセルを純粋なPythonに変換に実行
  • class NotebookFinder
    • ある名前がインポートできるかどうかを確認し、適切なローダーを返す。
import io, os, sys, types
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell
def find_notebook(fullname, path=None):
    """find a notebook, given its fully qualified name and an optional path

    This turns "foo.bar" into "foo/bar.ipynb"
    and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
    does not exist.
    """
    name = fullname.rsplit('.', 1)[-1]
    if not path:
        path = ['']
    for d in path:
        nb_path = os.path.join(d, name + ".ipynb")
        if os.path.isfile(nb_path):
            return nb_path
        # let import Notebook_Name find "Notebook Name.ipynb"
        nb_path = nb_path.replace("_", " ")
        if os.path.isfile(nb_path):
            return nb_path
class NotebookLoader(object):
    """Module Loader for Jupyter Notebooks"""

    def __init__(self, path=None):
        self.shell = InteractiveShell.instance()
        self.path = path

    def load_module(self, fullname):
        """import a notebook as a module"""
        path = find_notebook(fullname, self.path)

        print("importing Jupyter notebook from %s" % path)

        # load the notebook object
        with io.open(path, 'r', encoding='utf-8') as f:
            nb = read(f, 4)

        # create the module and add it to sys.modules
        # if name in sys.modules:
        #    return sys.modules[name]
        mod = types.ModuleType(fullname)
        mod.__file__ = path
        mod.__loader__ = self
        mod.__dict__['get_ipython'] = get_ipython
        sys.modules[fullname] = mod

        # extra work to ensure that magics that would affect the user_ns
        # actually affect the notebook module's ns
        save_user_ns = self.shell.user_ns
        self.shell.user_ns = mod.__dict__

        try:
            for cell in nb.cells:
                if cell.cell_type == 'code':
                    # transform the input to executable Python
                    code = self.shell.input_transformer_manager.transform_cell(cell.source)
                    # run the code in themodule
                    exec(code, mod.__dict__)
        finally:
            self.shell.user_ns = save_user_ns
        return mod
class NotebookFinder(object):
    """Module finder that locates Jupyter Notebooks"""

    def __init__(self):
        self.loaders = {}

    def find_module(self, fullname, path=None):
        nb_path = find_notebook(fullname, path)
        if not nb_path:
            return

        key = path
        if path:
            # lists aren't hashable
            key = os.path.sep.join(path)

        if key not in self.loaders:
            self.loaders[key] = NotebookLoader(path)
        return self.loaders[key]

これらを読み込んだ上で、

sys.meta_path.append(NotebookFinder())

とすることにより、作業ディレクトリ内のJupyter Notebooksファイル、例えば、

def message():
    print("Hello, World!")

ImportingJupyterNotebooksAsModules/tmp.ipynb at main · computational-sediment-hyd/ImportingJupyterNotebooksAsModules · GitHub

import tmp

のようにimportすることでmessage関数にアクセスできるようになります。

一方、私のwindow環境では、

import tmp
# ModuleNotFoundError: No module named 'tmp'

となってしまいます。

多分、sys.meta_pathの追加が上手く行っていないようですが、解決できませんでした。

詳しい方ぜひ教えていただけると助かります。

暫定的解決策

公式の方法のうち、def find_notebook、class NotebookLoaderの2つを別ファイル(ImportingNotebooks.py)に記述して、 任意のJupyter Notebooksファイル(sample.ipynb)で、

import ImportingNotebooks as inb
loader = inb.NotebookLoader()

のようにimportすることで、

m = loader.load_module("tmp")
m.message()
# Hello, World!

のようにtmp.ipynbをimportしてメソッドを実行できるようになります。

また、フルパスで読み込む方法としては、class NotebookLoaderを少し変更して(ImportingNotebooks2.py)、以下のように読み込む方法もあります(sampl2.ipynb)。

import ImportingNotebooks2 as inb
loader = inb.NotebookLoader()
m = loader.load_module("tmp.ipynb")
m.message()
# Hello, World!

どちらの方法も公式の方法には劣りますが比較的スマートかと思います。

GitHub

github.com