【问题标题】:TypeError: 'module' object is not callable - using datetimeTypeError:“模块”对象不可调用 - 使用日期时间
【发布时间】:2017-07-05 20:24:58
【问题描述】:

抱歉,没有找到任何其他解决方法,所以我创建了一个新问题。 我正在尝试运行一个将当前小时、分钟和秒打印到屏幕上的代码。我用来执行此操作的代码是:

def time():
    import datetime
    import time
    time.sleep(1)
    nowTime = datetime.now()
    print ('Right now it's %s hours, %s minutes and %s seconds.' % (nowTime.hour,nowTime.minute,nowTime.second))
    time.sleep(5)

当这段代码尝试执行时,我收到以下错误:

Traceback (most recent call last):
  File "/Users/Teolicht/Desktop/Python/testy.py", line 219, in <module>
    start()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 18, in start
    questions()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 34, in questions
    day()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 52, in day
    time()
TypeError: 'module' object is not callable

start()questions()day() 是程序在通过time() 之前经过的一些其他功能。如果我尝试直接执行time(),它会起作用!所以,这是time()函数从开始到结束的整个代码:

from datetime import datetime
import time
import random
import sys

def start():
    import time
    name = input('Hi. What is your name? ')
    print ('Nice to meet you, ' + str(name) + '.')
    time.sleep(1)
    print ('How old are you?')
    age = input()
    time.sleep(1)
    print (age + '! Cool.')
    time.sleep(2)
    questions()

def questions():
    import time
    print ('Some things you can ask me:')
    time.sleep(1)
    print ('• What day is today? (qdh)')
    time.sleep(1)
    print ('• What time is it? (qhs)')
    time.sleep(1)
    print ('• I want to play a game! (qjj)')
    time.sleep(1)
    print ('• How many days till my birthday? (qda)')
    time.sleep(1)
    questionsAnswer = input()
    if questionsAnswer == 'qdh':
        day()
    elif questionsAnswer == 'qhs':
        time()
    elif questionsAnswer == 'qjj':
        game()
    else:
        birthday()

def day():
    import time
    time.sleep(1)
    nowDay = datetime.now()
    print ('Today is %s/%s/%s' % (nowDay.day,nowDay.month,nowDay.year))
    time.sleep(2)
    dayAnswer = input('Want to know what time it is? "Y" for yes and "N" for no: ').lower()
    if dayAnswer == 'n':
        questions()
    else:
        time()

def time():
    import time
    time.sleep(1)
    nowTime = datetime.now()
    print ('Right now it's %s hours, %s minutes and %s seconds.' % (nowTime.hour,nowTime.minute,nowTime.second))
    time.sleep(5)
        questions()
       ...

可能是 start()questions()day() 中的某些内容导致错误。有任何想法吗?非常感谢!

【问题讨论】:

  • 您的意思是 time.time() 在您的 day 函数的最后一行吗?
  • import time在一个名为time的函数中?您对范围内的内容感到困惑并不奇怪。
  • 没错,jonrsharpe

标签: python python-3.x


【解决方案1】:

它对我有用,尽量不要创建自己的 time() 方法,我将它重命名为“my_time()”。

时间模块定义了很多函数,或者你只是“导入时间”,或者你需要指定你想要导入的每个函数,比如“从时间导入睡眠”

from datetime import datetime
from time import time, sleep
import random
import sys

def questions():
    print ('Some things you can ask me:')
    sleep(1)
    print ('• What day is today? (qdh)')
    sleep(1)
    print ('• What time is it? (qhs)')
    sleep(1)
    print ('• I want to play a game! (qjj)')
    sleep(1)
    print ('• How many days till my birthday? (qda)')
    sleep(1)
    questionsAnswer = input()
    if questionsAnswer == 'qdh':
        day()
    elif questionsAnswer == 'qhs':
        my_time()
    elif questionsAnswer == 'qjj':
        my_game()
    else:
        my_birthday()

def day():
    sleep(1)
    nowDay = datetime.now()
    print ('Today is %s/%s/%s' % (nowDay.day,nowDay.month,nowDay.year))
    sleep(2)
    dayAnswer = input('Want to know what time it is? "Y" for yes and "N" for no: ').lower()
    if dayAnswer == 'n':
        questions()
    else:
        my_time()

def my_time():
    sleep(1)
    nowTime = datetime.now()
    print ('Right now it\'s %s hours, %s minutes and %s seconds.' % (nowTime.hour, nowTime.minute, nowTime.second))
    sleep(5)
    questions()

def my_game():
    pass

def my_birthday():
    pass

#def start():
name = input('Hi. What is your name? ')
print ('Nice to meet you, ' + str(name) + '.')
sleep(1)
print ('How old are you?')
age = input()
sleep(1)
print (age + '! Cool.')
sleep(2)
questions()

【讨论】:

  • 谢谢!重命名my_time() 并将datetime.now() 更改为datetime.datetime.now 解决了这个问题。再次感谢!
【解决方案2】:

在你的指挥下

nowTime = datetime.now()

datetimemodule,它没有方法 now()

你可能想要

nowTime = datetime.datetime.now()

其中第一个datetimemodule,第二个是其中的class - 使用类方法now() 创建具有当前本地日期和时间的object

【讨论】:

  • 仔细阅读异常。 datetime 模块不是他的问题。他正在通过from datetime import datetime使用datetime.datetime。正如 Jonrsharpe 已经说过的,他在他的函数内部调用了 time 模块,也称为 time
  • @Christian - 你部分正确 - 我的回答只修复了 OP time() 函数。在其time() 函数中导入time 模块绝对是一个坏主意 - 但没有执行它的不良后果,因为import 语句将名称导入它自己的命名空间。
  • time() 重命名为 my_time() 并将 datetime.now() 更改为 datetime.datetime() 现已修复。谢谢大家!
【解决方案3】:

将您的 import time 替换为 from time import time

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 2017-06-17
    • 2014-09-21
    • 2011-05-30
    • 2021-11-22
    • 2017-09-24
    相关资源
    最近更新 更多