61

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
starball
  • 20,030
  • 7
  • 43
  • 238
Johanna
  • 1,019
  • 2
  • 9
  • 20

7 Answers7

102

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.

jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • That makes sense :) However, having corrected the script, I still get the exact same error message? – Johanna Dec 19 '12 at 21:07
  • 2
    @Johanna It's not the script, it's how you're running it. You're typing `python test.py` at the wrong place. – John Kugelman Dec 19 '12 at 21:08
  • 1
    @Johanna What do you mean, "corrected the script"? There was nothing wrong with the script. – melpomene Dec 19 '12 at 21:08
  • @melpomene I mean that I changed it into `print "Hello."`, since I'm using Python 2.7 at the moment. @JohnKugelman Sorry, but could you explain why it is the wrong place? I thought this was the way to run scripts when using the command line? (I can already run the script itself using F5 and IDLE, but I want to understand the use of the command line as well) – Johanna Dec 19 '12 at 21:12
  • 1
    If you are using Windows, press Win+R and type `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`. – aemdy Dec 19 '12 at 21:14
  • 1
    @Johanna `print("foo")` is valid in Python 2.x. What exactly do you mean by "the command line"? – melpomene Dec 19 '12 at 21:14
  • @melpomene In the start menu, under Programs, I have Python 2.7. When I hoover the mouse over it, five options appear, among which are IDLE (Python GUI), and Python (command line). This command line is what I was using, but seeing some of the comments below, it looks like this is the wrong place to execute `python test.py`...? – Johanna Dec 19 '12 at 21:23
  • @Hvandracas Thanks for your help, I'm using Windows, but I don't see a Win key on my keyboard? How do I open this Win+R program? – Johanna Dec 19 '12 at 21:25
  • 1
    @Johanna "Python (command line)" executes `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](http://www.simplehelp.net/images/winkey/windowskey00.jpg), you need to find some other way to execute `cmd` (e.g. on my Windows XP I have "Run..." in my Start menu). – melpomene Dec 19 '12 at 21:30
  • 1
    Just press the start menu, then `Run...` or type in that text box, type `cmd`, press `Enter`, there you go, command prompt. – jdotjdot Dec 19 '12 at 21:31
  • @jdotjdot Thanks, that was very clear. So I managed to open cmd.exe, but when I type in `python test.py`, I get this: 'python' is not recognized as an internal or external command, operable program or batch file. Why is this? (sorry for asking so many questions, but computers are very complicated, and I'm trying to understand!) – Johanna Dec 19 '12 at 21:45
  • @melpomene Thanks for the explanation! I now understand why this wasn't working. But if I execute just `test.py` in the command line, it doesn't work either? Or was that to be expected? – Johanna Dec 19 '12 at 21:46
  • This helped me. I was running it from inside interpreter without realising and was wondering whether this was some sort of weird bug! This should probably been the first thing I checked... – Farhan.K Feb 27 '18 at 16:25
13

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

cmd.exe

python test.py

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
12

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.

Llamax
  • 313
  • 2
  • 13
Amin J
  • 153
  • 1
  • 9
  • 1
    and your recommandation is diffrent from the already accepted 8+ years old answer how exactly? – AlexT Apr 25 '21 at 23:34
  • of course it works, it says the exact same thing as the already accepted 8+ years old answer. – AlexT May 02 '21 at 23:28
  • 1
    @Alex.T The accepted states that you must exit the Python interpreter, but does not explain how to. This answer does. I tried editing the original to add that information, but it was not approved. You can also use `quit()`. – Llamax Aug 05 '21 at 16:33
  • I also think that this is the very best anser! – yBother Jan 28 '22 at 08:58
5

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.

Community
  • 1
  • 1
Saurabh Ariyan
  • 827
  • 11
  • 21
1

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.

SnirD
  • 708
  • 11
  • 22
0

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

asmeurer
  • 86,894
  • 26
  • 169
  • 240
-1

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

Chaitanya
  • 132
  • 2
  • 16