python - 我的代码的第 102 行总是出错

标签 python datetime error-handling

所以我正在创建一个模块,并将其导入 python shell 并运行一些东西以确保所有功能正常工作等。

出于某种原因,每次我运行代码时,都会出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ryansaxe/Desktop/Code/python/modules/pymaps.py", line 102, in url_maker
    #anything can be here
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

那么 #anything can be here 所在的位置就是我的代码第 102 行中的任何内容。最初第 102 行是:

if isinstance(startindex,datetime.datetime):

我得到了上面的错误。我在第 102 行放了一个快速打印语句来检查,它给出了同样的错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ryansaxe/Desktop/Code/python/modules/pymaps.py", line 102, in url_maker
    print 'Hello'
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

这是某种错误吗?为什么它告诉我 print 'Hello' 行的日期时间有错误?

因为它可能会有帮助,所以我将向您提供我遇到问题的功能,因为我不知道这是怎么可能的。我保留了 print 'Hello' 行,这样您就可以看到第 102 行在哪里:

def url_maker(latitudes,longitudes,times=None,color='red',label=' ',zoom=12,center=None,start=None,end=None,by=None,size='600x300'):
    urls = []   
    import datetime
    if isinstance(times[0],str) or isinstance(times[0],datetime.datetime):
        from dateutil import parser
        if isinstance(times[0],str):
            times = [parser.parse(x) for x in times]
        if isinstance(start,str):
            startindex = parser.parse(start)
        else:
            startindex = start
        if isinstance(end,str):
            endindex = parse.parse(end)
        else:
            endindex = end
        print 'Hello'
        if isinstance(startindex,datetime.datetime):
            startpos = between_times(times,startindex,by='start')
        elif isinstance(startindex,int):
            if isinstance(endindex,datetime.datetime):
                startpos = between_times(times,endindex,by='end') - start
            else:
                startpos = start
        else:
            pass
        if isinstance(endindex,datetime.datetime):
            endpos = between_times(times,endindex,by='end')
        elif isinstance(endindex,int):
            if isinstance(startindex,datetime.datetime):
                endpos = between_times(times,startindex,by='start') + end
            else:
                endpos = end
        else:
            pass
    else:
        times = range(1,len(latitudes) + 1)
        if isinstance(start,int):
            startpos = start
        else:
            startpos = None
        if isinstance(end,int):
            endpos = end
        else:
            endpos = None
    if isinstance(by,str):
        lat,lon,t = latitudes[startpos:endpos],latitudes[startpos:endpos],times[startpos:endpos]
        print lat
        t,lats,lons = time_sample(t,by,lat,lon)
    elif isinstance(by,int):
        lats,lons,t = latitudes[startpos:endpos:by],latitudes[startpos:endpos:by],times[startpos:endpos:by]
    else:
        lats,lons,t= latitudes[startpos:endpos],latitudes[startpos:endpos],times[startpos:endpos]
    print t
    print len(t)
    if center == None:
        latit = [str(i) for i in lats]
        longi = [str(i) for i in lons]
        center = '&center=' + common_finder(latit,longi)
    else:
        center = '&center=' + '+'.join(center.split())
    zoom = '&zoom=' + str(zoom)
    for i in range(len(lats)):
        #label = str(i)
        x,y = str(lats[i]),str(lons[i])
        marker = '&markers=color:' + color + '%7Clabel:' + label + '%7C' + x + ',' + y
        url = 'http://maps.googleapis.com/maps/api/staticmap?maptype=roadmap&size=' + size + zoom + center + marker + '&sensor=true'
        urls.append(url)
        #print i
    return urls,t

最佳答案

您正在使用陈旧的字节码缓存运行,或者正在现有解释器中重新运行代码而不重新启动它。

回溯代码只有字节码可以使用,其中包含文件名和行号信息。发生异常时,加载源文件以检索原始代码行,但如果源文件已更改,则会导致显示错误行。

重新启动解释器和/或删除所有 *.pyc 文件;后者将在解释器再次导入代码时重新创建。

至于你的具体异常;您可能从某处的 datetime 模块导入了 datetime 类:

from datetime import datetime

datetime 类没有 datetime 属性,只有模块有。

关于python - 我的代码的第 102 行总是出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16994971/

相关文章:

Python:同时记录击键和鼠标移动

python - 在 For 循环中从 Windows 批处理文件中的数组访问项目

MySQL 选择日期时间

c# - WPF:如何只从文本框中获取时间?

python - 区分python中出现的错误

python - 从 python 评估 "Shell command line with shell variables"或将 python 字符串评估为 shell 命令行

python - 当由 cron 作业启动时,将打印 python 输出重定向到一个文件

python - 如何在分钟内转换(可能为负)Pandas TimeDelta( float )?

express - 如果在Express-Validator中间件之前使用multer作为中间件功能,则存在Multer(Express)处理错误

json - "Value of optional type Self? not unwrapped"inside failable initializer with "try?"and "Self", 如何在不强制展开的情况下写入