Yeile
Yeile

Reputation: 761

Python Version in PowerShell

I am previously running python 2.7.11 on Windows 10.

Today, I downloaded and installed python 2.7.13, but the PowerShell version is still at 2.7.11:

python --version
Python 2.7.11
  1. How do I check all installed python versions on my PC?
  2. How do I uninstall python 2.7.11?
  3. How do i set python 2.7.13 as default python version to launch in PowerShell?

Upvotes: 19

Views: 73541

Answers (3)

halt9k
halt9k

Reputation: 953

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:
    try Get-Command py command 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-paths will give a summary on the installed versions, and supposed way to run scripts is not previous python main.py, but commands like py main.py or py -3.5 main.py. Run py --help for 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.

Upvotes: 1

Paul
Paul

Reputation: 1

I execute the powershell command line statement in python3.8,

import subprocess
subprocess.call('powershell.exe Get-WmiObject Win32_PnPSignedDriver| select DeviceName, Manufacturer, DriverVersion', shell=True)

The running result is:

'select' is not an internal or external command, nor an executable program or batch file.

Upvotes: -3

TessellatingHeckler
TessellatingHeckler

Reputation: 29048

In PowerShell, Get-Command python | fl * will tell you which Python executable it's finding and show you details about where it is.

  1. 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.
  2. 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-Command earlier, and decide if you want to remove that.
  3. How PowerShell chooses what to run when you type a command is explained in help about_Command_Precedence, and is:
    1. Alias
    2. Function
    3. Cmdlet
    4. 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:

Upvotes: 32

Related Questions