在python中使用命令行时出现语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13961140/
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-08-18 10:02:03  来源:igfitidea点击:

syntax error when using command line in python

pythoncommand-linesyntax-error

提问by Johanna

I am a beginner to python and am at the moment having trouble using the command line. I have a script test.py (which only contains print("Hello.")), and it is located in the map C:\Python27. In my system variables, I have specified python to be C:\Python27 (I have other versions of Python installed on my computer as well).

我是 python 的初学者,目前在使用命令行时遇到问题。我有一个脚本 test.py(只包含print("Hello.")),它位于映射 C:\Python27 中。在我的系统变量中,我将 python 指定为 C:\Python27(我的计算机上也安装了其他版本的 Python)。

I thought this should be enough to run python test.pyin the command line, but when I do so I get this:

我认为这应该足以python test.py在命令行中运行,但是当我这样做时,我得到了这个:

File "<stdin>", line 1
python test.py
       ^
SyntaxError: invalid syntax

What is wrong? Thanks in advance!

怎么了?提前致谢!

采纳答案by jdotjdot

Looks like your problem is that you are trying to run python test.pyfrom within the Python interpreter, which is why you're seeing that traceback.

看起来您的问题是您试图python test.py从 Python interpreter 中运行,这就是您看到该回溯的原因。

Make sure you're out of the interpreter, then run the python test.pycommand from bash or command prompt or whatever.

确保您没有使用解释器,然后python test.py从 bash 或命令提示符或其他任何地方运行命令。

回答by John Kugelman

Don't type python test.pyfrom inside the Python interpreter. Type it at the command prompt, like so:

不要python test.py从 Python 解释器内部输入。在命令提示符下键入它,如下所示:

cmd.exe

命令行工具

python test.py

蟒蛇测试.py

回答by asmeurer

Running from the command line means running from the terminal or DOS shell. You are running it from Python itself.

从命令行运行意味着从终端或 DOS shell 运行。您正在从 Python 本身运行它。

回答by SnirD

In order to run scripts, you should write the "python test.py" command in the command prompt, and not within the python shell. also, the test.py file should be at the path you run from in the cli.

为了运行脚本,您应该在命令提示符中而不是在 python shell 中编写“python test.py”命令。此外,test.py 文件应该位于您在 cli 中运行的路径。

回答by Saurabh Ariyan

I faced a similar problem, on my Windows computer, please do check that you have set the Environment Variables correctly.

我遇到了类似的问题,在我的 Windows 计算机上,请检查您是否正确设置了环境变量。

To check that Environment variable is set correctly:

要检查环境变量是否设置正确:

  1. Open cmd.exe
  2. Type Python and press return
  3. (a) If it outputs the version of python then the environment variables are set correctly.

    (b) If it outputs "no such program or file name" then your environment variable are not set correctly.

  1. 打开cmd.exe
  2. 输入 Python 并按回车
  3. (a) 如果它输出 python 的版本,那么环境变量设置正确。

    (b) 如果它输出“没有这样的程序或文件名”,那么您的环境变量设置不正确。

To set environment variable:

设置环境变量:

  1. goto Computer-> System Properties-> Advanced System Settings -> Set Environment Variables
  2. Goto path in the system variables; append ;C:\Python27 in the end.
  1. 转到计算机-> 系统属性-> 高级系统设置-> 设置环境变量
  2. 转到系统变量中的路径;追加 ;C:\Python27 最后。

If you have correct variables already set; then you are calling the file inside the python interpreter.

如果您已经设置了正确的变量;然后你在 python 解释器中调用文件。

回答by Chaitanya

Come out of the "python interpreter."

从“python解释器”出来。

  1. Check out your PATH variable c:\python27
  2. cd and your file location. 3.Now type Python yourfilename.py.
  1. 检查您的 PATH 变量 c:\python27
  2. cd 和您的文件位置。3.现在输入 Python yourfilename.py。

I hope this should work

我希望这应该有效