我正在 Jupyter 笔记本中编写一个 Python 脚本来运行 20 多个长 SQL 查询。我在单独的文件 queryStrings.ipynb 中定义了 SQL 查询字符串,代码主体在文件 analytics2020.ipynb 中。
这个旧的 StackOverflow 帖子描述了一种很好的干净方法来定义一个单独的文件中的常量列表(请参阅最后一个答案......来自 Ned Batchelder 的那个)
python-best-cleanest-way-to-define-constant-lists-or-dictionarys
但是,这似乎不适用于 Jupyter Notebook。我已经创建了两个单独的文件
查询字符串.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. '''
分析2020.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'
然而,我引用的上一篇文章告诉我这应该有效。也许我有一种预感,这是因为这些文件是 Jupyter Notebook .ipynb 文件,而不是普通的 .py 文件。
将不胜感激任何帮助解决这个问题!非常感谢。