概要
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')
1-2. モジュール(クラスのメソッド)を利用
try:
from includes.module import *
except ImportError:
pass
%run ./includes/module
TestClass.print_msg()
Hello World