Search This Blog

python hello.py File "", line 1 python hello.py ^ SyntaxError: invalid syntax

 Error: python hello.py File "<stdin>", line 1 python hello.py ^ SyntaxError: invalid syntax while running a python script in command prompt ( windows )


Possible Reasons: 

  1.  The problem is that you are trying to run python hello.py from within the Python interpreter, which is why you're seeing the traceback.
  2. You are not using the command prompt in administrator access.
  3. You are not in the right directory where your python files exist.

Solution: ( OS: Windows 10 )

Step 1: If you are using Windows, press Win+R and type cmd. As shown in the image below. And run it as administrator.


Step 2: Suppose in your computer, hello.py file is at this address: c:\users\yourname\desktop\pythonfiles

So, use the below-mentioned commands to go at the above-mentioned path.

C:\WINDOWS\system> cd..

C:\Windows>cd..

C:\Users>cd users\yourname\Desktop\

C:\Users\engineerbae\Desktop> cd python files

If you're already on the correct path directly jump to step 3.

Step 3: Now, run the hello.py this way: python hello.py in the command prompt.

You shall receive your output :)


Lately, when I ran my first python script using the command prompt. I had been using an interpreter directly and had no clue what's going wrong. 


Hope it helped.

Thank You!

No comments:

Post a Comment