0

I'm new to Python (3.10.4). I've been reading Mark Lutz's textbook and I'm only getting comfortable with the environment (Windows 10), and all this time I've been under the impression there's no real difference between running Python programs in cmd or powershell. However, today I followed the suggestion from the textbook and tried launching a Python script file without typing "python" at the start of the command line, and PowerShell returned me an error message saying "[the script file name] could not be identified as a Cmdlet, a function, a scriptfile or a executeable program." At first I thought I was doing something wrong, but then I tried doing the same in cmd, and I got the expected output. So, in cmd this syntax works just fine.

Then I noticed the PowerShell suggestion that was printed out below the error message. It said if I trust the command, I should run it with a different syntax, so not like

filename.py 

but like

.\filename.py

I tried doing this, and it launched the file (a tiny black window blinked as if I double clicked the file), but I didn't get the output in the PowerShell window this time either.

So, my question is: what's the difference between cmd and powershell from the Python perspective? Why do they treat Python differently? And as I study Python, should I stick to cmd rather than to PowerShell from now on?

  • This has absolutely nothing with Python to do. `cmd` generally includes the current directory on the `PATH` but `./script.py` is not wrong even then, just less ambiguous. However, you separately need to have configured your OS so it knows what to do with this type of file. On Windows, this is typically dependent on the file's extension; Unix-like systems have a different mechanism entirely, where the script file needs to have executable permissions and a valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)). – tripleee Apr 02 '22 at 11:54
  • On Windows, maybe try calling the file `script.pyw`; but again, what exactly works depends on how you installed Python and how you configured your OS. – tripleee Apr 02 '22 at 11:55
  • 1
    `.\filename.py` is the correct PowerShell syntax for launching Python script from current directory. `filename.py` only works when script is located in a directory that is contained in `PATH` environment variable. It doesn't open a new window on my machine. Make sure to select "py launcher" and "associate files with Python" in the installation options. – zett42 Apr 02 '22 at 12:03
  • I am a bit confused. When I installed Python, I clicked on all the OK's and Next's because I didn't want to customize anything. I believe the option to open all *.py files via Python was turned on by default because this is what properties window of the script file says. And it seems like when I launch the script through PowerShell, that's exactly what happens -- the script gets executed rather than printing out the expected output. I can make peace with syntax differences between cmd and powershell, however I find it confusing that when launched from powershell the script gives no output. – сибәрпанк Apr 02 '22 at 13:41
  • 1
    Please take a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) It describes in full details how the Windows Command Processor `cmd.exe` is searching for executables and scripts. Windows PowerShell `powershell.exe` uses also by default the environment variables `PATHEXT` and `PATH`, but does not search by default in the current directory for the executable/script file. There must be used ``.\`` to search also in current directory for the executable/script on using PowerShell. – Mofi Apr 02 '22 at 13:47
  • 1
    A file with extension `.py` or `.pyw` is just a text file and for that reason neither `powershell.exe` nor `cmd.exe` can run such a file directly. Both look up in Windows registry which application is associated with file extension `.py` respectively `.pyw` to find out which executable to run with the Python script file as argument. There can be executed `C:\Windows\System32\reg.exe query HKCR\.py /ve` to get displayed the default string value like `pyfile` (example) and next `C:\Windows\System32\reg.exe query HKCR\pyfile\shell\open\command /ve` to get displayed the command line to run a .py. – Mofi Apr 02 '22 at 13:53
  • `%1` in output command line is replaced by the file name of the Python script file on execution of `python.exe` (Windows console version) or `pythonw.exe` (Windows GUI version) on `powershell.exe` or `cmd.exe` using the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with a [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure to run Python to interpret the Python script file. – Mofi Apr 02 '22 at 13:57

0 Answers0