我有点困惑为什么我收到错误AttributeError:'module'对象没有属性'strptime'。当我在Python脚本中导入datetime并调用datetime.datetime.strptime(字符串,格式)时,一切正常,但是当我从datetime import datetime写入并调用datetime.strptime(字符串,格式)时,我的脚本崩溃并出现上述错误。我很好用import datetime并调用datetime.datetime但我仍然很好奇为什么从datetime import datetime崩溃调用datetime.strptime(字符串,格式)。关于为什么会发生这种情况的任何想法?我的代码如下......
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
答案 0 :(得分:13)
也许
from shared.datastore import *
正在重新定义datetime
作为模块。
您可以通过在代码中添加print语句来检查此猜测
from datetime import datetime
print(datetime)
from shared.datastore import *
print(datetime)
并查看datetime
的值是否已更改。
如果我猜错了,你仍然可以使用print语句“平分”代码,直到找到哪一行改变了datetime
的值。
(无论如何,最好不要使用通配符形式的导入,因为它可能是这些奇怪错误的来源。)