gpt4 book ai didi

python - 为什么这在 Python IDLE shell 中有效,但当我从命令提示符将其作为 Python 脚本运行时却无效?

转载 作者:太空狗 更新时间:2023-10-30 00:26:37 29 4
gpt4 key购买 nike

这适用于 Python 3.3.2 Shell

Python 3.3.2 外壳内部

>>> import datetime
>>> print(datetime.datetime.utcnow())
2013-07-09 19:40:32.532341

太棒了!然后我写了一个名为“datetime.py”的简单文本文件

内部 Datetime.py

#Date time
import datetime
print(datetime.datetime.utcnow())
#Prints GMT, which is named Universal Coordinated Time
# Which is UTC because in French it's something like
# Universahl Tyme Coordinatay
#Outputs something like 2013-07-09 15:15:19.695531

证明文件存在

C:\Python33\myscripts>ls
__pycache__ ex1.out ex2.out ex3.py helloworld.py read1.py
datetime.py ex1.py ex2.py first.py pythonintoimportexport.py test.py

这就是它变得神秘的地方!

C:\Python33\myscripts>python datetime.py
Traceback (most recent call last):
File "datetime.py", line 2, in <module>
import datetime
File "C:\Python33\myscripts\datetime.py", line 3, in <module>
print(datetime.datetime.utcnow())
AttributeError: 'module' object has no attribute 'utcnow'

问题

为什么相同的代码在 Python Shell 中有效,但作为脚本运行时却不行?

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com