0

I have a helloworld.py containing print("Hello world!"). When I try to run it from the command line with python helloworld.py it gives me an error: The syntax of the command is incorrect..

I have python 3.8.2 set up correctly, I get the version when running python --version and I can use and print statements in the python interpreter.

So what can I do to fix this problem? I'm running Windows 10. And I've removed the aliases like suggested in this thread: https://stackoverflow.com/a/58773979/8029153

I observe the same behavior in Powershell and cmd.

Output with Powershell:

PS C:\Users\user> cd .\Desktop\...\scripts\
PS C:\Users\user\Desktop\...\scripts> dir


    Directory: C:\Users\user\Desktop\...\scripts


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        28.4.2020     10.07            188 helloworld.py


PS C:\Users\user\Desktop\...\scripts> python helloworld.py
The syntax of the command is incorrect.

Output with plain cmd:

C:\Users\user\Desktop\...\scripts>dir

 Directory of C:\Users\user\Desktop\...\binaries

28.04.2020  10.08    <DIR>          .
28.04.2020  10.08    <DIR>          ..
28.04.2020  10.07               188 test.py
               6 File(s)      9 696 468 bytes
               2 Dir(s)  135 932 608 512 bytes free

C:\Users\user\Desktop\...\scripts>python test.py
The syntax of the command is incorrect.

Output of Powershell's get-command:

PS C:\Users\user\Desktop\...\scripts> get-command python

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         0.0.0.0    C:\Users\user\AppData\Local\Micros...

Output of where command:

C:\Users\user\Desktop\...\scripts>where python
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe
Wolf
  • 9,679
  • 7
  • 62
  • 108
Iiro Alhonen
  • 352
  • 3
  • 19
  • Since it's obviously to late now for getting interesting outputs from the command `python version`, I withdraw my wish to add it here. As I was reported from a Win-10 user "without" a python, the MS Store Python placeholder ignores the `--version` command line switch giving completely empty output (and no errors). – Wolf May 06 '20 at 08:35

2 Answers2

1

The nonsensical combination that the

  • Python interpreter works and valid
  • Python scripts fail

can actually only be traced back to an outdated Windows search path.

As to see how the program name typed into the command line is resolved into a runnable binary, use the where command. It lists you all matching candidates for all path prefixes (;-separated parts) of the PATH environment variable.

A look into your actual result

C:\Users\user\Desktop\...\scripts>where python
C:\Users\user\AppData\Local\Microsoft\WindowsApps\python.exe

reveals that python is still resolved to the Windows-Store placeholder named python.exe that has the only purpose to install a MS-compilation of Python.

After having installed it (better go to https://www.python.org/downloads/windows/ and get an installer there), the value of the PATH variable in the old command line window is still the old one, so you could try the above where command again and again with no change.

It was not the 3rd install that made it work, but the fact that you tried to run the script in a fresh console window (maybe after a reboot or logoff+login or explorer restart [1]). An already open command line doesn't see changes to the PATH environment variable that are done by the installer.

And the reason why the "Python interpreter worked" must be that you started it (namely IDLE) from the Windows start menu.


[1] I'm not sure about how exactly the installer added the Python's bin directory to PATH to make explorer aware of the change, some Installers seemingly manage that, some not, so the restart we often observe at the end of an installation.

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • After the 3rd install, it started magically working. So... corruption? – Iiro Alhonen May 06 '20 at 08:05
  • @IiroAlhonen Finallly I have found a scenario that removes the apparent contradictions. Please let me know if this matches your observations. – Wolf May 06 '20 at 10:16
-1

Try with this command - python3 helloworld.py

  • Same error as before. – Iiro Alhonen May 06 '20 at 07:45
  • If the same error occurs with this command as well, then I guess there are some bugs in the latest version. – Akshaya May 06 '20 at 07:48
  • This answer shows that a difference between running `python` and `python3` will be unlikely https://stackoverflow.com/questions/58754860/cmd-opens-window-store-when-i-type-python/58773979#58773979 – Wolf May 06 '20 at 11:07