属性错误:“模块”对象没有属性“strptime”; -- 可能的错误?

发布于 2025-01-03 13:20:42 字数 7113 浏览 0 评论 0原文

我有点困惑为什么收到错误 AttributeError: 'module' object has no attribute 'strptime'。当我在 Python 脚本中导入 datetime 并调用 datetime.datetime.strptime(string, format) 时,一切正常,但是当我从 datetime import datetime 写入并调用 datetime.strptime(string, format) 时,我的脚本因上述错误而崩溃。我可以很好地使用 import datetime 和调用 datetime.datetime 但我仍然很好奇为什么 from datetime import datetime 在调用 datetime.strptime(string, format) 时崩溃。关于为什么会发生这种情况有什么想法吗?我的代码如下...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-01-10 13:20:42

也许

from shared.datastore import *

正在将 datetime 重新定义为模块。

您可以通过在代码中添加 print 语句

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

并查看 datetime 的值是否已更改来检查此猜测。

如果我的猜测是错误的,您仍然可以使用 print 语句“平分”您的代码,直到找到哪一行正在更改 datetime 的值。

(无论如何,最好不要使用通配符形式的导入,因为它可能是这些奇怪错误的来源。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文