20 以上の長い SQL クエリを実行するために、Jupyter ノートブックで Python スクリプトを作成しています。別のファイル queryStrings.ipynb で SQL クエリ文字列を定義し、コードの本体はファイル analytics2020.ipynb にあります。
この古い StackOverflow の投稿では、別のファイルで定数のリストを定義するためのきれいな方法について説明しています (最後の回答を参照してください ... Ned Batchelder からのもの)。
python-best-cleanest-way-to-define-constant-lists-or-dictionarys
ただし、これは Jupyter Notebook では機能しないようです。私は2つの別々のファイルを作成しました
queryStrings.ipynb
q_CurrWeekiOSDailySessionCountDuration = ''' with session_boundaries as ( SELECT e.cust_id_attr_value ,e.event_timestamp ,DATEDIFF(minutes, LAG(e.event_timestamp) OVER(PARTITION BY e.cust_id_attr_value ORDER BY e.event_timestamp), e.event_timestamp) AS inactivity_time ,LAG(e.event_timestamp) OVER(PARTITION BY e.cust_id_attr_value ORDER BY e.event_timestamp) as prior_event_timestamp FROM APPLICATIONDB e WHERE event_data:"c-platform-m-os" = 'iOS' AND event_timestamp BETWEEN \'{:s}\' AND \'{:s}\' ) select session_date, sum(num_sessions) as total_sessions, etc. etc. '''
analytics2020.ipynb
import pandas as pd import numpy as np from queryStrings import q_CurrWeekiOSDailySessionCountDuration print('===== q_CurrWeekiOSDailySessionCountDuration ====') print(q_CurrWeekiOSDailySessionCountDuration)
ただし、これを実行しようとすると、エラーが発生します。
26 from queryStrings import q_CurrWeekiOSDailySessionCountDuration
27 print('===== q_CurrWeekiOSDailySessionCountDuration ====')
28 print(q_CurrWeekiOSDailySessionCountDuration)
ModuleNotFoundError: No module named 'queryStrings'
ただし、引用した前の投稿では、これはうまくいくはずだと言っています。おそらく、これらのファイルがプレーンなバニラ .py ファイルではなく、Jupyter Notebook .ipynb ファイルであるためだと思います。
これを解決する助けをいただければ幸いです。本当にありがとう。