windows 将模块作为脚本执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3636798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:11:12  来源:igfitidea点击:

Executing modules as scripts

pythonwindows

提问by prime23

I am learn python now, and today, i met a problem in http://docs.python.org/release/2.5.4/tut/node8.html

我现在正在学习 python,今天,我在http://docs.python.org/release/2.5.4/tut/node8.html遇到了一个问题

6.1.1 Executing modules as scripts

When you run a Python module with

python fibo.py <arguments>

the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__". That means that by adding this code at the end of your module:

6.1.1 将模块作为脚本执行

当您运行 Python 模块时

python fibo.py <arguments>

模块中的代码将被执行,就像您导入它一样,但 __name__ 设置为“__main__”。这意味着通过在模块末尾添加此代码:

if __name__ == "__main__":
    import sys`
    fib(int(sys.argv[1]))

you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the "main" file:

$ python fibo.py 50 1 1 2 3 5 8 13 2134

您可以使文件既可用作脚本又可用作可导入模块,因为解析命令行的代码仅在模块作为“主”文件执行时才运行:

$ python fibo.py 50 1 1 2 3 5 8 13 2134

but when i do this in shell, i got

但是当我在 shell 中执行此操作时,我得到了

File "<input>", line 1
python fibo.py 222
SyntaxError: invalid syntax

how to execute script correctly?

如何正确执行脚本?

fibo.py is

fibo.py 是

def fib(n):
    a,b=0,1
    while b<n:
        print b,
        a,b = b,a+b


def fib2(n):
    result=[]
    a,b=0,1
    while b<n:
        result.append(b)
        a,b=b,a+b
    return result

if __name__ =="__main__":
    import sys
    fib(int(sys.argv[1]))

回答by Dave Kirby

What exactly did you do in the shell? What is the code you are running?

你在shell中到底做了什么?你运行的代码是什么?

It sounds like you made a mistake in your script - perhaps missing the colon or getting the indentation wrong. Without seeing the file you are running it is impossible to say more.

听起来您在脚本中犯了一个错误 - 可能缺少冒号或缩进错误。如果没有看到您正在运行的文件,就不可能多说。

edit:

编辑:

I have figured out what is going wrong. You are trying to run python fibo.py 222in the pythonshell. I get the same error when I do that:

我已经弄清楚出了什么问题。您正在尝试python fibo.py 222pythonshell 中运行。当我这样做时,我得到了同样的错误:

[138] % python
Python 2.6.1 (r261:67515, Apr  9 2009, 17:53:24)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python fibo.py 222
  File "<stdin>", line 1
    python fibo.py 222
              ^
SyntaxError: invalid syntax
>>>

You need to run it from the operating system's command line prompt NOT from within Python's interactive shell.

您需要从操作系统的命令行提示符而不是从 Python 的交互式 shell 中运行它。

Make sure to change to Python home directory first. For example, from the Operating system's command line, type: cd C:\Python33\ -- depending on your python version. Mine is 3.3. And then type: python fibo.py 200 (for example)

确保先更改到 Python 主目录。例如,在操作系统的命令行中,键入:cd C:\Python33\ -- 取决于您的 Python 版本。我的是3.3 然后输入:python fibo.py 200(例如)