0

I am a beginner in using python.

I created a file script.py which the code is only one line: print("hello"), but when I executed it on windows command prompt (I use windows 10), it results error

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

I have set the variable paths so when I typed python on CMD, it shows the version of the python that I installed. I saved the script.py in the folder where Python 3.6 installed (Programs/Python 3.6/) but it couldn't work. I also tried to write the address path of the file, e.g ./F/Data/script.py but the result was the same. How do I fix this?

dede
  • 823
  • 4
  • 13
  • 24
  • 4
    You're in a Python interactive session. `python script.py` is something you run from a command prompt when you're not already in Python. Get out of Python first. – user2357112 Dec 04 '17 at 17:42
  • 1
    Hello, Diyah. you need to use `python script.py` as a *terminal command*, not inside the Python REPL. – juanpa.arrivillaga Dec 04 '17 at 17:42
  • Above comments are right on... Or, alternatively, as long as you are in the Python REPL, just type `print('hello')` and you will have executed a Python statement. – Ryan Dec 04 '17 at 17:44
  • In other words, " so when I typed python on CMD, it shows the version of the python that I installed." that actually opens up a REPL session. All that version info is just what is printed when you open a REPL. Note the `>>>` indicates you are in the REPL. If you *did* simply want to see the version, you could use `python --version` – juanpa.arrivillaga Dec 04 '17 at 17:47
  • Fun fact: you can still use the REPL if you'd like, you just need to do `import script` and that's it. Make sure you are running the interpreter on the same directory though. – Matias Cicero Dec 04 '17 at 17:48
  • @MatiasCicero but won't that fail in REPL if you use `if __name__ == '__main__':` in your script, whereas using the terminal command would execute that code? – roganjosh Dec 04 '17 at 17:50
  • @MatiasCicero it couldn't work in python terminal – dede Dec 04 '17 at 17:51
  • @roganjosh You're of course right. However, I just wanted to point that out for this particular case. – Matias Cicero Dec 04 '17 at 17:53
  • @Ryan Sandridge I've tried to type the code in both cmd and python terminal, the code was executed well – dede Dec 04 '17 at 17:56
  • Thank you guys, this is a silly mistakes. My friend was coming and solving this – dede Dec 04 '17 at 18:02

0 Answers0