I am 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
-
3Type "exit()", then press play in Visual Studio Code to run your code.JustBeingHelpful– JustBeingHelpful2021年03月06日 02:54:23 +00:00Commented Mar 6, 2021 at 2:54
8 Answers 8
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.
13 Comments
python test.py at the wrong place.cmd there. Then navigate to the script containing directory and type python test.py. If you're on mac or linux, then open terminal, navigate to the script containing directory and type in python test.py.print("foo") is valid in Python 2.x. What exactly do you mean by "the command line"?python. Thus you were trying to execute python test.py from within a running python. (That's the error.) If you don't have a win key, you need to find some other way to execute cmd (e.g. on my Windows XP I have "Run..." in my Start menu).You can simply type exit() in the Python terminal to exit the Python interpreter. Then when you run the code, there will be no more errors.
4 Comments
quit().Don't type python test.py from inside the Python interpreter. Type it at the command prompt, like so:
cmd.exe
python test.py
Comments
I faced a similar problem, on my Windows computer, please do check that you have set the Environment Variables correctly.
To check that Environment variable is set correctly:
Open cmd.exe
Type Python and press return
(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.
To set environment variable:
- goto Computer-> System Properties-> Advanced System Settings -> Set Environment Variables
- Goto path in the system variables; append ;C:\Python27 in the end.
If you have correct variables already set; then you are calling the file inside the python interpreter.
Comments
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.
Comments
Running from the command line means running from the terminal or DOS shell. You are running it from Python itself.
Comments
I experienced nearly same error when working with Thonny running a test program for Rapsberry Pi Pico W in python shell. My test command
print("Hello Pico W")
output error:
Traceback (most recent call last):
File "<stdin>", line 1
SyntaxError: invalid syntax
I solved this by first running
exit()
Then running the test code which executed successfully.
Comments
Come out of the "python interpreter."
- Check out your PATH variable c:\python27
- cd and your file location. 3.Now type Python yourfilename.py.
I hope this should work