1

I just downloaded VSCode and Python today and am attempting to follow a Python tutorial course. But every time I try to run my program in the terminal, it won't open / can't find my file.

This is the code I'm trying to run:

My Beginner Code

(Edit: The 'Hi' is random and has no bearing on the code I was trying to run)

And this is what happens when I try to run it:

PS C:\Users\...\Hellow World> $python HW.py
At line:1 char:9
+ $python HW.py
+         ~~~~~
Unexpected token 'HW.py'. in expression or statement
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Beginner Code Error Output

I downloaded Python from the Microsoft Store. It's version 3.9.1

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
  • Your code snippet isn't present in the directory you are running your code in . Please try to run your code be pressing Ctrl F5 – kishan keswani Feb 20 '21 at 21:34
  • 3
    `$python` is a variable in Windows PS. You want to invoke `python` (no dollar sign). – Brian61354270 Feb 20 '21 at 21:38
  • @kishankeswani I'm not sure how you came to that conclusion. The error the OP included is a parse error for PS. `HW.py` didn't have the chance to be interpreted as a filename, let alone checked for existence. – Brian61354270 Feb 20 '21 at 21:41

3 Answers3

2

When you write

$python HW.py

in Windows PowerShell, the token $python is expanded as a variable. You want to invoke the Python interpreter, which is just python (note: no dollar sign).

You likely picked up the dollar sign by copying from an example that featured a Bourne shell style prompt. The dollar sign simply indicates the end of the shell prompt and the beginning of your command, much like the > in your PowerShell prompt. You don't need to (and should not) type the $ yourself.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
0

Just remove the $ sign

From :

$python file.py

To :

python file.py

If you are using python 3.x, you can do this also:

python3 file.py

After that you can run script file.py in terminal using python command

-1

Try using python3 HW.pyor python HW.py rather than $python HW.py

Also, your code might throw an error as this might try adding a string into an int which will cause a Type error. Try running the following code, it should work perfectly if u are in the right directory in the terminal. Make sure u specify the int before input so the compiler knows that it's expecting an int from the user.

x = int(input("x: "))
y = x + 1
print(y)
Saad Mahboob
  • 21
  • 1
  • 3