syntax error when using command line in python

2019-01-04 11:37发布

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).

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

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

What is wrong? Thanks in advance!

7条回答
贪生不怕死
2楼-- · 2019-01-04 12:08

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

cmd.exe

python test.py

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-04 12:16

If you are seeing this error, even though you're using the interpreter correctly, it might be that you are in Windows and accidentally typed an unrecognized character (e.g. an up arrow on your keyboard). This character won't show up visually, but it can mess up your Python interpreter:

>>> from driver.driver_client import DriverClient
  File "<stdin>", line 1
    from driver.driver_client import DriverClient
    ^
SyntaxError: invalid syntax
>>> from driver.driver_client import DriverClient
>>>
查看更多
劫难
4楼-- · 2019-01-04 12:17

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-04 12:18

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.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 12:20

Come out of the "python interpreter."

  1. Check out your PATH variable c:\python27
  2. cd and your file location. 3.Now type Python yourfilename.py.

I hope this should work

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-04 12:23

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

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

查看更多
登录 后发表回答