0
0

More than 3 years have passed since last update.

Databricksにてローカル環境での開発を考慮した自作モジュールを取り込む方法

Posted at

概要

Databricksにてローカル環境での開発を考慮した自作モジュールを取り込む方法を共有します。
あまりよい方法ではないので、よりよい方法をご存知の方はご教授ください。

Databricksでは自作モジュールを取り込む際に、%runを利用する必要があり、importを利用するとエラーとなるという課題があります。
また、ローカル開発時には、Jupyter等のノートブック型の開発ツールを利用しない場合には、%runがエラーになります。

上記の解決策となる下記の方法を紹介します。
エクスポートする手順を紹介してますが、Databricks Repos機能を用いることでGitでPythonコードを取得することも可能です。

  • tryによりDatabricks上で開発時にエラーとしない
  • databricksからエクスポートすることで%runの箇所をコメントアウト

詳細は下記のGithub pagesのページをご確認ください。

コードを実行したい方は、下記のdbcファイルを取り込んでください。

https://github.com/manabian-/databricks_tecks_for_qiita/blob/main/tecks/import_libarary_local_and_db/dbc/import_libarary_local_and_db.dbc

手順

1. Databricks上での開発

1-1. モジュールを作成

class TestClass:

    @staticmethod
    def print_msg():
        print('Hello World')

image.png

1-2. モジュール(クラスのメソッド)を利用

try:
    from includes.module import *
except ImportError:
    pass
%run ./includes/module
TestClass.print_msg()
Hello World

image.png

2. ローカル上で開発

2-1. Databricksからノートブックをエクスポート

image.png

2-2. ローカル上で実行できることを確認

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0