26

I'm using Windows PowerShell. Let's say I have a script called test.py that prints a few things. If I do:

PS D:\>.\test.py

then it opens a CMD window which prints a few things and then closes. It's actually running the Python interpreter under CMD. If I do

PS D:\>python test.py

it acts like I'd expect it to, with the output appearing in PowerShell.

How can I make it so that the script will run in PowerShell when I just give its name?

asked Jun 16, 2012 at 22:14
1
  • Once you've updated your PATHEXT variable using zdan's accepted answer below, if you put Python scripts in a directory named by your PATH environment variable, there will be no need to supply a fully-qualified (or relative) path to run them anymore. Then Python scripts behave like any other installed program, and you can join them together in pipelines with other programs / cmdlets (e.g. get-clipboard | myscript.py), and all that other goodness. :) Commented Aug 28, 2019 at 1:32

2 Answers 2

31

Edit the PATHEXT environment variable and add the .py extension.

Just add this line to your PowerShell profile:

$env:PATHEXT += ";.py"

or you could just edit PATHEXT globally in the system settings (just search in the Start menu for "environment" and choose the option for "Edit environment variables for your account").

Kevin E
5956 silver badges13 bronze badges
answered Jun 17, 2012 at 5:34
1
  • 1
    How does it decide which Python interpreter to use, though? I did this and the only change was that I now see a message Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.. However, Python is installed and py is in the PATH ... Commented Apr 26, 2021 at 13:42
-1

You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type>python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g>python C:\myfile.py.

If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g.>myfile.py

I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just>myfile should work.

Edit after Update:

Typing just>python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.

answered Jun 16, 2012 at 22:40
1
  • Why do you copy and paste your answer from somewhere? You even forgot to copy the formatting properly, and there was no "Update" in there. Commented Jun 17, 2012 at 6:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.