I am a little bit confused as to why I am receiving the error AttributeError: 'module' object has no attribute 'strptime'. When I import datetime in my Python script and call datetime.datetime.strptime(string, format) everything works fine but when I write from datetime import datetime and call datetime.strptime(string, format) my script crashes with the above error. I am fine using import datetime and calling datetime.datetime but I am still curious as to why from datetime import datetime crashes on the call datetime.strptime(string, format). Any ideas as to why this might be happening? My code is below...
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 error...
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
Perhaps
from shared.datastore import *
is redefining datetime
to be the module.
You can check this guess by putting print statements in your code
from datetime import datetime
print(datetime)
from shared.datastore import *
print(datetime)
and seeing if the value of datetime
has changed.
If my guess is wrong, you can still use print statements to "bisect" your code until you find which line is changing the value of datetime
.
(Whatever the case, it is preferable to not use the wildcard form of import, since it can be a source of these weird bugs.)