为什么这在 Python IDLE shell 中有效,但当我从命令提示符将它作为 Python 脚本运行时却无效? [英] Why does this work in the Python IDLE shell but not when I run it as a Python script from the command prompt?

查看:30
本文介绍了为什么这在 Python IDLE shell 中有效,但当我从命令提示符将它作为 Python 脚本运行时却无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于 Python 3.3.2 Shell

Python 3.3.2 Shell 内部

<预><代码>>>>导入日期时间>>>打印(日期时间.日期时间.utcnow())2013-07-09 19:40:32.532341

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

Datetime.py 内部

#日期时间导入日期时间打印(日期时间.日期时间.utcnow())#Prints GMT,它被命名为通用协调时间# 这是 UTC 因为在法语中它类似于# Universahl Tyme Coordinatay#输出类似 2013-07-09 15:15:19.695531 的内容

证明文件存在

C:\Python33\myscripts>ls__pycache__ ex1.out ex2.out ex3.py helloworld.py read1.pydatetime.py ex1.py ex2.py first.py pythonintoimportexport.py test.py

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

C:\Python33\myscripts>python datetime.py回溯(最近一次调用最后一次):文件datetime.py",第 2 行,在 <module> 中导入日期时间文件C:\Python33\myscripts\datetime.py",第 3 行,在 <module> 中打印(日期时间.日期时间.utcnow())AttributeError: 'module' 对象没有属性 'utcnow'

问题

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

解决方案

问题是文件是递归导入自身,而不是导入内置模块datetime:

演示:

$ cat datetime.py导入日期时间打印日期时间.__文件__$ python datetime.py/home/monty/py/datetime.pyc/home/monty/py/datetime.pyc

发生这种情况是因为搜索了模块 按此顺序:

  • 包含输入脚本的目录(或当前目录).
  • PYTHONPATH(目录名称列表,语法与shell 变量 PATH).
  • 依赖于安装的默认值.

只需将 datetime.py 的名称更改为其他名称即可.

This works in the Python 3.3.2 Shell

Inside the Python 3.3.2 Shell

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

That's great! I then wrote a simple text file named "datetime.py"

Inside 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

Proving that the file exists

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

Here is where it gets mysterious!

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'

Question

Why does the same code work in the Python Shell, but not when run as a script?

解决方案

The problem is that file is recursively importing itself, instead of importing the built-in module datetime:

Demo:

$ cat datetime.py
import datetime
print datetime.__file__
$ python datetime.py
/home/monty/py/datetime.pyc
/home/monty/py/datetime.pyc

This happens because the module is searched in this order:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

Simply change the name of datetime.py to something else.

这篇关于为什么这在 Python IDLE shell 中有效,但当我从命令提示符将它作为 Python 脚本运行时却无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆