Cannot run .py files in PowerShell

Can someone assist me with the possible reasons why I cannot run .py files in PowerShell. I can run them in CMD fine. I thought PowerShell carried the same basic functionality as the CMD has? Below is the error I am getting? Thank you.

PS C:\Users\ajoh\Desktop\Python\py4e\ex_03> Ch_03_Score_Spy.py
Ch_03_Score_Spy.py : The term ‘Ch_03_Score_Spy.py’ is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1

  • Ch_03_Score_Spy.py
  • CategoryInfo : ObjectNotFound: (Ch_03_Score_Spy.py:String) [], CommandNotFoundException
  • FullyQualifiedErrorId : CommandNotFoundException

Suggestion [3,General]: The command Ch_03_Score_Spy.py was not found, but does exist in the current location. Windows PowerShell does not load commands from the current location by default. If you trust this command, instead type: “.\Ch_03_Score_Spy.py”. See “get-help about_Command_Precedence” for more details.
PS C:\Users\ajoh\Desktop\Python\py4e\ex_03>

 

Try Invoke-Item either with an absolute or a relative path to your *.py file. Read the help (completely including examples) to learn more about.

Try

PS C:\Users\ajoh\Desktop\Python\py4e\ex_03>python Ch_03_Score_Spy.py

PowerShell doesn’t run commands / invokables from the current directory without you explicitly telling it you want to use the one from the current directory, by prefixing it with . - as the suggestion message states.

Always read the help / error messages. :slight_smile:

Like it says in the error message:

If you trust this command, instead type: ".\Ch_03_Score_Spy.py".

Or if you don’t want a new window. For some reason it pops a new window in powershell but not in cmd.

python Ch_03_Score_Spy.py

Thank you for all your responses. The invoke-item command seemed to work, except the program display’s briefly and then disappears like it does when you just double-click the .py itself. I am going through pyfe courses and while learning the code I am trying to learn how to package and run the script. How to give it to someone else so they can put it into practice. If anyone has any ideas or articles that would help me down this road I would appreciate it. I did look at the get-help.

If you’re running python I’d just be using cmd and not powershell in most cases. There’s not really a huge reason to mix the two most of the time; PS is in large part a whole other world. You could script it entirely in PowerShell and just have it run natively in the shell rather than calling out to the python executable, but that’s a bit of a jump if the code’s already written.

If you wanted, you could define a simple PowerShell function to invoke Python commands, something like:

function py {
    param($Path)
    python $Path
}

and then just run it with py script.py to shorten the necessary command a bit. New-Alias -Name py -Value python might also do the trick a bit more simply.

Using .\Ch_03_Score_Spy.py I got my script to appear with the first user input, but after you enter a value it just disappears. I will try creating the PowerShell py function. I am trying to learn how to write a script and then being able to give it to somebody to use. Thanks a lot for your comments.

Like I said, if you run “python script.py” it will stay in the same window, assuming python is in the path.

This is my path. Is there a problem with it?

PS C:\python\py4e\ex_03> .\Ch_03_Score_Spy.py

Does this work?

PS C:\python\py4e\ex_03> python Ch_03_Score_Spy.py

Says I need to import the Python module.