2012-02-07 53 views
8

我有點困惑,爲什麼我收到錯誤AttributeError:'模塊'對象沒有屬性'strptime'。當我在我的Python腳本中導入datetime並調用datetime.datetime.strptime(string,format)時,一切正常,但是當我從datetime寫入日期時間並調用datetime.strptime(字符串,格式)時,我的腳本崩潰,出現上述錯誤。我很好,使用導入日期時間和調用datetime.datetime,但我仍然很好奇,爲什麼從datetime導入日期時間在調用datetime.strptime(字符串,格式)崩潰。任何想法爲什麼這可能發生?我的代碼如下...AttributeError:'模塊'對象沒有屬性'strptime' - 可能的Bug?

Bulkolader.yaml

python_preamble: 
- import: re 
- import: base64 

- import: hs_transformers 

- import: google.appengine.ext.db 
- import: google.appengine.ext.bulkload.transform 
- import: google.appengine.ext.bulkload.bulkloader_wizard 

- import: google.appengine.api.datastore 


transformers: 

- kind: HBO 
    connector: csv 

    property_map: 
    - property: __key__ 
     external_name: swfServerID 
     import_transform: hs_transformers.string 

    - property: IP_address 
     external_name: IP 
     import_transform: hs_transformers.string 

    - property: name 
     external_name: swfServer 
     import_transform: hs_transformers.swfServer 

    - property: checkin 
     external_name: clockStampOfLastCheckin 
     import_transform: hs_transformers.date_time 

    - property: update 
     external_name: clockStampOfLastUpdate 
     import_transform: hs_transformers.clockStampOfLastUpdate 

    - property: form_factor 
     external_name: formFactor 
     import_transform: hs_transformers.string 

    - property: serial_number 
     external_name: serialNumber 
     import_transform: hs_transformers.string 

    - property: reverse_SSH 
     external_name: allowReverseSSH 
     import_transform: hs_transformers.boolean 

    - property: insight_account 
     external_name: FK_insightAccountID 
     import_transform: hs_transformers.integer 

    - property: version 
     external_name: ver 
     import_transform: hs_transformers.string 

hs_transformers.py

import re 
import logging 
from datetime import datetime 
from shared.datastore import * 
import google.appengine.ext.bulkload.transform 
from google.appengine.api import memcache 


def clockStampOfLastUpdate(passed_clockstamp): 
    try: 
     if passed_clockstamp != "NULL": 
      datetime_object = date_time(passed_clockstamp) 
      return_file_update = memcache.get(str(datetime_object), namespace = "HBOImport") 

      if not return_file_update: 
       return_file_update = FileUpdate.lastBefore(datetime_object) 
       memcache.set(str(datetime_object), return_file_update, namespace = "HBOImport", time = 20) 

      if return_file_update != None: return return_file_update.key() 
    except Exception, e: 
     logging.error(e) 


def string(passed_string): 
    try: 
     if passed_string != "NULL": return passed_string 
    except Exception, e: 
     logging.error(e) 


def swfServer(passed_url): 
    try: 
     if passed_url != "NULL": return passed_url.split('//', 1)[1].split('.')[0] 
    except Exception, e: 
     logging.error(e) 


def date_time(passed_datetime): 
    try: 
     if passed_datetime != "NULL": return datetime.strptime(passed_datetime, '%m/%d/%y %H:%M') 
    except Exception, e: 
     logging.error(e) 


def boolean(passed_boolean): 
    try: 
     if passed_boolean != "NULL": 
      if passed_boolean == "T": return True 
      if passed_boolean == "F": return False 
    except Exception, e: 
     logging.error(e) 


def integer(passed_integer): 
    try: 
     if passed_integer != "NULL": return int(passed_integer) 
    except Exception, e: 
     logging.error(e) 

CMD錯誤...

Microsoft Windows [Version 6.1.7600] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

C:\Users\Jack Frost>cd "C:\Program Files (x86)\Google App Engine SDK" 

C:\Program Files (x86)\Google App Engine SDK>python appcfg.py upload_data --url=http://bulkloader-testing.appspot.com/remote_api --config_file="C:\Users\Jack Frost\Eclipse Workspace\Headsprout\GAE 2.1.2012\src\utilities\bulkloader\bulkloader.yaml" --filename="C:\Users\Jack Frost\Eclipse Workspace\Headsprout\GAE 2.1.2012\src\utilities\bulkloader\csv_files\small_hbos.csv" --kind=HBO 
Uploading data records. 
[INFO ] Logging to bulkloader-log-20120207.105053 
[INFO ] Throttling transfers: 
[INFO ] Bandwidth: 250000 bytes/second 
[INFO ] HTTP connections: 8/second 
[INFO ] Entities inserted/fetched/modified: 20/second 
[INFO ] Batch Size: 10 
[INFO ] Opening database: bulkloader-progress-20120207.105053.sql3 
[INFO ] Connecting to bulkloader-testing.appspot.com/remote_api 
[INFO ] Starting import; maximum 10 entities per post 
PASSED DATETIME 2/1/12 17:52 
TYPE <type 'unicode'> 
[ERROR ] [WorkerThread-0] WorkerThread: 
Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\tools\adaptive_thread_pool.py", line 176, in WorkOnItems 
status, instruction = item.PerformWork(self.__thread_pool) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\tools\bulkloader.py", line 764, in PerformWork 
transfer_time = self._TransferItem(thread_pool) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\tools\bulkloader.py", line 933, in _TransferItem 
self.content = self.request_manager.EncodeContent(self.rows) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\tools\bulkloader.py", line 1394, in EncodeContent 
entity = loader.create_entity(values, key_name=key, parent=parent) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\ext\bulkload\bulkloader_config.py", line 445, in create_entity 
entity = self.dict_to_entity(input_dict, self.bulkload_state) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\ext\bulkload\bulkloader_config.py", line 147, in dict_to_entity 
self.__run_import_transforms(input_dict, instance, bulkload_state_copy) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\ext\bulkload\bulkloader_config.py", line 252, in __run_import_transforms 
value = self.__dict_to_prop(transform, input_dict, bulkload_state) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\ext\bulkload\bulkloader_config.py", line 207, in __dict_to_prop 
value = transform.import_transform(value) 
    File "C:\Program Files (x86)\Google App Engine SDK\google\appengine\ext\bulkload\bulkloader_parser.py", line 111, in __call__ 
return self.method(*args, **kwargs) 
    File "C:\Users\Jack Frost\Eclipse Workspace\Headsprout\GAE 2.1.2012\src\utilities\bulkloader\hs_transformers.py", line 44, in date_time 
print datetime.strptime(passed_datetime, '%m/%d/%y %H:%M') 
AttributeError: 'module' object has no attribute 'strptime' 
[INFO ] [WorkerThread-1] Backing off due to errors: 1.0 seconds 
[INFO ] An error occurred. Shutting down... 
[ERROR ] Error in WorkerThread-0: 'module' object has no attribute 'strptime' 

[INFO ] 10 entities total, 0 previously transferred 
[INFO ] 0 entities (1516 bytes) transferred in 3.0 seconds 
[INFO ] Some entities not successfully transferred 
+1

整潔。你清楚地知道你必須使用的解決方法,並且只是好奇爲什麼會出現這種情況(就像我一樣)。我花了一些時間在Python 2.x的導入機制中進行了深入的研究,並且我可以告訴你,一般來說,顯式地(即比datetime import datetime更喜歡「import datetime」)可以避免很多奇怪的問題。我不能告訴你爲什麼這個特定問題會出現在你身上,只是推測可能是一些Google AppEngine代碼與進口有關。 – mattbornski 2012-02-07 19:03:38

+0

是的,我只是好奇,爲什麼在技術上他們都應該工作時,我必須使用一條進口線。我的假設是,我的.yaml文件中的一個導入語句正在導入另一個使用datetime import datetime中的行的腳本(Google的腳本之一),並因此導致兩個腳本之間的奇怪交互。 – 2012-02-07 19:08:18

回答

13

也許

from shared.datastore import * 

正在重新定義datetime爲模塊。

您可以通過將打印語句在你的代碼

from datetime import datetime 
print(datetime) 
from shared.datastore import * 
print(datetime) 

,看到如果datetime值已改變檢查這個猜測。

如果我的猜測錯誤,仍然可以使用打印語句「平分」您的代碼,直到找到哪條線正在更改datetime的值。

(無論是哪種情況,優選的是不使用進口通配符形式,因爲它可以是這些奇怪的錯誤的來源。)

+0

感謝您的建議。 – 2012-02-07 19:10:08

+6

故事的道德:不要'輸入*'。 – katrielalex 2012-02-07 19:13:14

相關問題