AttributeError:模块'datetime'在导入Yaml上没有属性'date'

问题描述 投票:0回答:1

我的代码行之一是

import yaml

已使用pip install pyyaml在python 3.7上安装

出现以下错误

追踪(最近通话):

文件“ C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/peer_changing_send_rate.py”,第1行,在从TestPeer.TestPeerChangingSendRate导入TestPeerChangingSendSpeed文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ TestPeer \ TestPeerChangingSendRate.py”,

第1行,在从.TestPeer导入TestPeer文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ TestPeer \ TestPeer.py”,

第4行,在从BaseProcess.ZmqPeerClass导入ZmqPeer文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ BaseProcess \ ZmqPeerClass.py”,

第2行,在从.ZmqPublisherClass导入ZmqPublisher文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ BaseProcess \ ZmqPublisherClass.py”,

第10行,在来自。导入ZmqProcessClass文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ BaseProcess \ ZmqProcessClass.py”,

第5行,在从.ConfigBaseClass导入ConfigBase文件“ C:\ code \ EPMD \ Kodex \ Applications \ EPMD-Software \ BaseProcess \ ConfigBaseClass.py”,

第3行,在进口Yaml第9行中的文件“ C:\ code \ EPMD \ Kodex \ venv \ lib \ site-packages \ yaml__init __。py”来自.dumper import *在第6行的文件“ C:\ code \ EPMD \ Kodex \ venv \ lib \ site-packages \ yaml \ dumper.py”来自.representer import *文件“ C:\ code \ EPMD \ Kodex \ venv \ lib \ site-packages \ yaml \ representer.py”,行

263,在SafeRepresenter.add_representer(datetime.date,AttributeError:模块'datetime'没有属性'date'

我如何使import yaml工作?

python python-3.x pyyaml
1个回答
0
投票

[您可能在错误消息中显示的目录之一(很可能是datetime.py)中有一个名为C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/的文件,如果这样做,则需要将其重命名为不会遮盖任何其他Python模块的名称。] >

原因是它掩盖了实际的datetime模块,因为当前工作目录中的文件/目录/模块优先于site-packages目录中安装的模块(内置和已安装模块所在的目录) 。

[当yaml\representer.py执行import datetime时,它导入了不具有datetime.py属性的date文件/模块,这就是为什么在以后尝试使用AttributeError时引发datetime.date的原因。

© www.soinside.com 2019 - 2024. All rights reserved.