Skip to main content
 首页 » 编程设计

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

2025年05月04日17haluo1

所以我正在创建一个模块,并将其导入 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 属性,只有模块有。