syntax error when using command line in python

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

cmd.exe

python test.py

Method 3

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:

  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.

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.

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

Method 4

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.

Method 5

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.

Method 6

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

Method 7

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x