简体   繁体   English

为什么这在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?

This works in the Python 3.3.2 Shell 这适用于Python 3.3.2 Shell

Inside the Python 3.3.2 Shell 在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" 然后我写了一个名为“datetime.py”的简单文本文件

Inside 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

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? 为什么相同的代码在Python Shell中有效,但在作为脚本运行时却不行?

The problem is that file is recursively importing itself, instead of importing the built-in module datetime : 问题是文件以递归方式导入自身,而不是导入内置模块的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). PYTHONPATH(目录名列表,语法与shell变量PATH相同)。
  • the installation-dependent default. 依赖于安装的默认值。

Simply change the name of datetime.py to something else. 只需将datetime.py的名称更改为其他名称即可。

As @Sukrit Kalra says, don't use datetime.py as your file name. 正如@Sukrit Kalra所说,不要使用datetime.py作为文件名。 Python is getting confused with which datetime is which (and is importing itself!). Python正在与哪个datetime混淆(并且正在导入自己!)。 Maybe; 也许;

 $ mv datetime.py my_datetime.py

Never use filenames same as module names. 切勿使用与模块名称相同的文件名。 Change filename to something else apart from datetime.py . 除了datetime.py之外,将文件名更改为其他内容。

Naming your file datetime causes Python to import the file you're running as a module. 命名文件datetime会导致Python导入您作为模块运行的文件。 For example, look at sys.path . 例如,查看sys.path Mine, for example, is ['', '/usr/lib/python3.3', ...] , which means that Python looks FIRST in the current working directory (the '' ) for modules. 例如,我的是['', '/usr/lib/python3.3', ...] ,这意味着Python在模块的当前工作目录( '' )中看起来是FIRST。 And because anything that ends in .py can be imported as a module, it imports the script that you're actually running (which, if I'm not mistaken, actually causes it to be run twice, once as __main__ and once as a module). 并且因为以.py结尾的任何内容都可以作为模块导入,它会导入您实际运行的脚本(如果我没有弄错,实际上会导致它运行两次,一次作为__main__ ,一次作为一个模块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line? 为什么 python 脚本在 pycharm 中运行和在命令提示符下运行时的行为不同? - Why does a python script behaves differently when it is run in pycharm and when it is run from a command prompt? 命令提示符中的 Python 不起作用,但在 python 空闲时它可以 - Python in command prompt doesn't work, but in python idle it does 为什么这个python脚本只能在Ubuntu 12.04上使用IDLE? - Why does this python script only work from IDLE on Ubuntu 12.04? 如何在 IDLE 上运行特定于命令提示符的 python 库? - How do I run python libraries specific to command prompt on IDLE? Python - 如何从 IDLE 与命令提示符运行模块 - Python - How to run module from IDLE vs. command prompt 我可以在当前shell中从python脚本运行shell命令吗 - Can I run a shell command from python script in the current shell 为什么我不能从Windows命令提示符运行python脚本? - Why can't I run a python script from the Windows Command Prompt? 如何从python shell或闲置命令运行.exe文件 - How to run .exe file with command from python shell or idle 从 BBEdit/TextWrangler 在 Shell (IDLE) 中运行 Python 脚本 - Run Python script in Shell (IDLE) from BBEdit/TextWrangler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM