【问题标题】:tzwhere (pytzwhere) - Python 2.7 with simple test casetzwhere (pytzwhere) - 带有简单测试用例的 Python 2.7
【发布时间】:2014-05-20 21:01:38
【问题描述】:

我正在运行 pytzwhere 包提供的最简单的测试问题。模块导入,但出现错误。 pytzwhere 似乎是从 GPS 坐标获取时区的一个不错的离线选项,但没有太多文档。任何有关如何协调这一点的帮助表示赞赏!

In [94]:

import tzwhere

w = tzwhere()
print w.tzNameAt(1.352083, 103.819836)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-94-0b50c8083e93> in <module>()
      1 import tzwhere
      2 
----> 3 w = tzwhere()
      4 print w.tzNameAt(1.352083, 103.819836)

TypeError: 'module' object is not callable

编辑:

已通过以下 cmets 的以下代码修改解决了此问题 -

In [108]:

from tzwhere import tzwhere

w = tzwhere.tzwhere()
print w.tzNameAt(1.352083, 103.819836)
-----------------------------------------------------------------------------
Reading json input file: /Users/user/anaconda/lib/python2.7/site-packages/tzwhere/tz_world_compact.json
Asia/Singapore

【问题讨论】:

    标签: python ipython pytz


    【解决方案1】:

    你不能只从 w = tzwhere() 这样的模块中实例化 w。 tzwhere 是一个包含 tzwhere 类的模块。正如 Python 正确指出的那样,模块是不可调用的。

    from tzwhere import tzwhere w = tzwhere()

    第一行从 module tzwhere 导入 class tzwhere。

    编辑:如果您确实导入“我的方式”:-) w = tzwhere() 是 w 作为 class tzwhere 的实例的有效创建。

    通常在 Python 中,类会被命名为 TzWhere,这样可以避免这种混淆。

    我假设您正在尝试使用https://github.com/pegler/pytzwhere/blob/master/tzwhere/tzwhere.py

    【讨论】:

    • 是的,这是我正在尝试使用的包。我尝试了 w=tzwhere.tzwhere(),我认为它可以与简单的 import tzwhere 一起使用。现在使用'from tzwhere import tzwhere'和'w = tzwhere.tzwhere()'它可以工作。感谢您的帮助。
    猜你喜欢
    • 2018-07-23
    • 2020-04-19
    • 2012-10-13
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多