0

I am learning Python now. At my very first program I am getting following error.

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

Below is my code in that file -

name = "John"
age = 19 
print name
print age

I am using following Python 3.5.1 version on Windows 10

I can print simple text from cmd line but no able to run script from file.

Update

I am running it from command line itself.. first I make a folder on my docs then run cmd line and type python after that when I just type print "Hello" it works fine but when I run python file like python first_tutorial.py it shows error

Any guidance please.

Thanks

John
  • 177
  • 3
  • 13
  • 1
    Just a nitpicking but `first_tutorial.py` and `firsttutorial.py` seems to be a little different. Anyway, more seriously, have you set the path of the Python interpreter? – Ian Mar 19 '16 at 14:50
  • Oh sorry thats my typo error. – John Mar 19 '16 at 14:52
  • Its not duplicate of that question. May be question is same but those answer not solved my problem. – John Mar 19 '16 at 14:57
  • It is a duplicate. The `>>>` arrows show you are trying to run python from python, not the command python – OneCricketeer Mar 19 '16 at 14:59
  • 1
    @John: I would expect that if you followed those answers, you would then get a SyntaxError at `print name` rather than `python first_tutorial.py`. That means that your current problem is solved. The `print name` is a separate problem. To fix that, use `print(name)`. Likewise, `print(age)`. – zondo Mar 19 '16 at 14:59
  • Thanks zondo cricket_007 ASCIIThenANSI – John Mar 19 '16 at 15:06

2 Answers2

3

You're running in the Python interactive shell. It's trying to interpret your command as Python code, which causes an error.

You need to run python first_tutorial.py from the commandline. After that your code should work fine, assuming there's no other errors.

ASCIIThenANSI
  • 865
  • 1
  • 9
  • 27
  • I am running it from commandline itself.. first I make a folder on my docs then run cmd line and type `python` after that when I just type `print "Hello" ` it works fine but when I run python file like `python first_tutorial.py` it shows error – John Mar 19 '16 at 15:00
  • Yes you are correct. I was running after entering in Python interactive shell. But still Can I run file inside python shell. ? – John Mar 19 '16 at 15:04
  • John, if your script is named `mytest.py`, you can run it (once) from the interactive prompt with `import mytest`. But it's better to edit it in IDLE and run it with F5. – alexis Mar 19 '16 at 15:19
0

When you type 'python' on the command prompt, you get an interpreter, so you can enter python code here:

>>> name = "john"
>>> print(name)
john

If you have a file test.py that you want to run, you can do that from the windows command prompt:

C:\Apps>python test.py
John
19

If you really want to run a python file from the python interpreter, you can do that too, but you need to open the file in python and exec it.

>>> with open("test.py") as f:
...   exec(f)
...
John
19
Kevin
  • 1