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!
This question is related to
python
command-line
syntax-error
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:
If you have correct variables already set; then you are calling the file inside the python interpreter.
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.
Running from the command line means running from the terminal or DOS shell. You are running it from Python itself.
Come out of the "python interpreter."
I hope this should work
Don't type python test.py
from inside the Python interpreter. Type it at the command prompt, like so:
Source: Stackoverflow.com