AttributeError:模块“datetime”在导入yam上没有属性“date”

2024-09-27 21:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码行之一是

import yaml

它是使用pip install pyyaml安装在python3.7上的

出现以下错误

Traceback (most recent call last):

File "C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/peer_changing_send_rate.py", line 1, in from TestPeer.TestPeerChangingSendRate import TestPeerChangingSendSpeed File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeerChangingSendRate.py",

line 1, in from .TestPeer import TestPeer File "C:\code\EPMD\Kodex\Applications\EPMD-Software\TestPeer\TestPeer.py",

line 4, in from BaseProcess.ZmqPeerClass import ZmqPeer File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPeerClass.py",

line 2, in from .ZmqPublisherClass import ZmqPublisher File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqPublisherClass.py",

line 10, in from . import ZmqProcessClass File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ZmqProcessClass.py",

line 5, in from .ConfigBaseClass import ConfigBase File "C:\code\EPMD\Kodex\Applications\EPMD-Software\BaseProcess\ConfigBaseClass.py",

line 3, in import yaml File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml__init__.py", line 9, in from .dumper import * File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\dumper.py", line 6, in from .representer import * File "C:\code\EPMD\Kodex\venv\lib\site-packages\yaml\representer.py", line

263, in SafeRepresenter.add_representer(datetime.date, AttributeError: module 'datetime' has no attribute 'date'

如何让import yaml工作


Tags: infrompyimportyamlvenvlinecode
1条回答
网友
1楼 · 发布于 2024-09-27 21:22:38

您可能在错误消息中显示的一个目录中有一个名为datetime.py的文件(很可能是C:/code/EPMD/Kodex/Applications/EPMD-Software/Sandbox/),如果您这样做了,那么您需要将它重命名为不会影响任何其他Python模块的文件

原因是它屏蔽了实际的datetime模块,因为当前工作目录中的文件/目录/模块优先于安装在site-packages目录中的模块(内置和安装的模块所在目录)。如果两个位置都包含a模块,则import a将从site-packages导入本地a模块,而不是(可能)预期的a模块

yaml\representer.py执行import datetime操作时,它导入了没有date属性的datetime.py文件/模块,这就是它后来尝试使用datetime.date时引发AttributeError的原因

相关问题 更多 >

    热门问题