LoginSignup
1
0

More than 3 years have passed since last update.

【Redash】pythonの関数内で標準ライブラリが使えない

Posted at

Redashでpythonをデータソースとして使っていて、ちょっとハマったので残しておきます。

環境

  • Redash 7.0.0
  • python 2.7

関数内で標準ライブラリが使えない

以下のように、先頭でimportしたdatetimeライブラリをhoge関数の中で使いたかったのですが、

import datetime

def hoge():
    print(datetime.datetime.now())

hoge()

↓  Executeしたらエラーが発生しました。(!?)

Error running query: <type 'exceptions.NameError'> global name 'datetime' is not defined

解決

関数内でimportしたら動作しました。

def hoge():
    import datetime
    print(datetime.datetime.now())

hoge()
# -> [2020-02-12T09:59:05.617825] 2020-02-12 09:59:05.617767
1
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
1
0