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

6
  • 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? Commented Mar 19, 2016 at 14:50
  • Oh sorry thats my typo error. Commented Mar 19, 2016 at 14:52
  • Its not duplicate of that question. May be question is same but those answer not solved my problem. Commented Mar 19, 2016 at 14:57
  • It is a duplicate. The >>> arrows show you are trying to run python from python, not the command python Commented Mar 19, 2016 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). Commented Mar 19, 2016 at 14:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
Yes you are correct. I was running after entering in Python interactive shell. But still Can I run file inside python shell. ?
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.
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

1 Comment

The third part is poor advice and incorrect (try it).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.