In PowerShell, Get-Command python | fl * will tell you which Python executable it's finding and show you details about where it is.
- You can check Settings -> Apps and Features, or Control Panel -> Programs and Features. They will show you distinct versions of Python you installed, but that might not be enough if Python is installed as part of some other toolkit or program.
- If Python 2.7.11 is there, select it and click uninstall. If it's not there, see if you can tell what it's installed with, from the output of
Get-Commandearlier, and decide if you want to remove that. - How PowerShell chooses what to run when you type a command is explained in help about_Command_Precedence, and is:
- Alias
- Function
- Cmdlet
- Native Windows commands
At the point of "Native Windows commands", it goes to the PATH environment variable, a semi-colon separated list of path names, which get searched in order, looking for a matching executable file.
You can see the folders with:
$Env:PATH -split ';'
And you can watch PowerShell identify what to run for 'python' with the command
Trace-Command –Name CommandDiscovery –Expression {get-command python} -PSHost
So, to make Python 2.7.13 the one to launch, you could:
- make it the only Python version available.
- move its folder to the front of the PATH list, ahead of any other version. See: What are path and other environment variables, and how can I set or use them - question on SuperUser.com
- make a batch file to launch it called python.bat in a folder in the PATH ahead of other versions.
- make an alias (in your PS Profile) named python to launch the one you want (
New-Alias -name python -Value C:\Python27\python.exe, etc).
In PowerShell, Get-Command python | fl * will tell you which Python executable it's finding and show you details about where it is.
- You can check Settings -> Apps and Features, or Control Panel -> Programs and Features. They will show you distinct versions of Python you installed, but that might not be enough if Python is installed as part of some other toolkit or program.
- If Python 2.7.11 is there, select it and click uninstall. If it's not there, see if you can tell what it's installed with, from the output of
Get-Commandearlier, and decide if you want to remove that. - How PowerShell chooses what to run when you type a command is explained in help about_Command_Precedence, and is:
- Alias
- Function
- Cmdlet
- Native Windows commands
At the point of "Native Windows commands", it goes to the PATH environment variable, a semi-colon separated list of path names, which get searched in order, looking for a matching executable file.
You can see the folders with:
$Env:PATH -split ';'
And you can watch PowerShell identify what to run for 'python' with the command
Trace-Command –Name CommandDiscovery –Expression {get-command python} -PSHost
So, to make Python 2.7.13 the one to launch, you could:
- make it the only Python version available.
- move its folder to the front of the PATH list, ahead of any other version. See: What are path and other environment variables, and how can I set or use them - question on SuperUser.com
- make a batch file to launch it called python.bat in a folder in the PATH ahead of other versions.
- make an alias (in your PS Profile) named python to launch the one you want (
New-Alias -name python -Value C:\Python27\python.exe, etc).
This was a question about python 2.7, but probably it will be useful to give an answer for the versions above 3.3 too:
Previously, multiple versions (also environments, also they are specific folders) of python on the same system was rare, and placing the only available python.exe directly in PATH was acceptable. Currently, multiple installed python versions will conflict and override each other if simply placed that way.
After 3.3 a python launcher was introduced which detects and activates one of the installed versions automatically. It is supposed to be placed in PATH instead of any python executable.
So in this modern situation, Get-Command python | fl * may give you nothing or nothing helpful.
And to run scripts or to get available versions, use launcher:
- ensure you have it:
tryGet-Command pycommand from the PowerShell. If launcher is missing, it can be installed with the official installer. There is a separate checkbox for the launcher which is enabled by default. - if install is correct, command
py --list-pathswill give a summary on the installed versions, and supposed way to run scripts is not previouspython main.py, but commands likepy main.pyorpy -3.5 main.py. Runpy --helpfor more info.
Additional confirmation that intended way changed.
Just to give an idea, launcher is not the only way to activate, this also can be done by a simple command.
For example, there is a version under D:\python_install\python.exe. But it's not in the PATH and python command correctly ends with not found error or opens Windows Store. An additional command in cmd or bat SET PATH=D:\python_install\;%PATH% or PowerShell $env:Path = "D:\python_install\;" + $env:Path temporarily activates that specific version, and python will work as previously during that specific run.
I have read through some previous posts with the same issue but the solutions are not working for me. Hoping someone can point to my error.
-
Trying to delete Python reference in Windowsapps directory using windows explorer. I get an error message that does not let me delete them.
-
In environmental variables I moved windows apps down below the python line. I still get an error message in Powershell that Python not found.
PowerShell does not show output of `where python` command
Running Python in PowerShell - Stack Overflow
Command "where python" does not return anything in PowerShell - Stack Overflow
I'm trying to use Python in PowerShell - Stack Overflow
How can I run a Python script from PowerShell?
How do I pass arguments from PowerShell to a Python script?
Can I integrate Python scripts into an MSI package?
Since, you are able to run Python in PowerShell. You can just do python <scriptName>.py to run the script. So, for a script named test.py containing
name = raw_input("Enter your name: ")
print "Hello, " + name
The PowerShell session would be:
Start:
cd C:\Python27
python test.py
Session transcript:
Enter your name: Monty Python
Hello, Monty Python
As far as I have understood your question, you have listed two issues.
Problem 1
You are not able to execute the Python scripts by double clicking the Python file in Windows.
Reason
The script runs too fast to be seen by the human eye.
Solution
Add input() in the bottom of your script and then try executing it with double click. Now the cmd will be open until you close it.
Example
print("Hello World")
input()
Problem 2
./ issue
Solution
Use Tab to autocomplete the filenames rather than manually typing the filename with ./ autocomplete automatically fills all this for you.
Usage
CD into the directory in which .py files are present and then assume the filename is test.py then type python te and then press Tab, it will be automatically converted to python ./test.py.
Try setting the path this way:
$env:path="$env:Path;C:\Python27"
For what's worth, this command did it for me (Python 3.3):
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Python33", "Machine")
I just had to restart the PowerShell window after that.
Possibly because after python 3.3 intended command is not python, but simply py
Detailed explanation here
And short answer to your question:
check if powershell command py -0p --list-paths finds any versions.
I am using Windows 11, Python3.10 and Laptop, and Windows Powershell cannot show Python version (command: python --version).
Solution: type Python inside Windows Powershell, it will prompt out Microsoft Store, just click "get", wait to finish the process, then go to Windows Powershell, type "python --version", it can show the Python version.
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").
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.
I am really new at all this so forgive me if I sound dumb or ignorant.I am just beginning to learn Python.Initially,I was going through Microsofts Introduction to Python on edX and I went through Module 1 but I didnt understand how to access the web coding assignment at the end.I still went through all the exercises and the MCQ's and I was doing pretty good at them so I didnt want to give up so I decided to find another way to learn. I got the pdf of Learn Python the Hard Way and decided to follow that instead.
I have already installed Atom and Python 3.6.7 and clicked add file path.But when I tried to type python in Powershell nothing comes up and I cant run anything.Its driving me crazy and I spent like an hour trying to get it to run but nothing.How can I get it to run?
Anyway,can I just follow the steps in Learn Python the Hard Way using Jupyter Notebooks instead?I went through exercise 1 in LPTHW in Jupyter and it seemed fine but in the book the author says if I cant set up then I shouldnt even continue and its stressing me out.
Again,I am really new to all of this,I am like a super beginner in Python.If anyone can help,I would be eternally so grateful to the stars and beyond.
Thanks!
EDIT:Thanks for the help everyone.I tried to set a new file path but even then I couldnt get anything in Powershell.I decided to use IDLE instead for now.Maybe over the weekend I may try to figure out whats wrong again.