70

I'm trying to follow Zed Shaw's guide Learning Python the Hard Way. I need to use Python in PowerShell.

I have Python 2.7.3 installed in C:\Python27. Whenever I type python into a PowerShell window, I get an error that says:

the term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.

I also typed in this: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

That was a suggested solution provided, but typing python into a PowerShell window still does nothing. I can type in "start python" and it opens up a window with Python, but I need it in PowerShell.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
masonc15
  • 1,043
  • 2
  • 12
  • 19
  • Works for me and I never set any paths for Python. What version of Powershell are you using? Mine is 1.0 the dark blue screen? Just be sure C:\Python27\ is in the PATH listing. Powershell should be there too. Mine is in system not user. Why do you worry about setting it from the command line? Use you windows Luke. It's the force of the future. :D – Douglas G. Allen Aug 28 '14 at 06:30
  • Just a note to anyone landing here from google, the answers setting path are all correct, but this problem probably stems from not giving the python installer administrative rights it needs to set the path itself. An alternative may be to simply right click the installer and select run as administrator, then repair the installation. If that still doesn't work, choose the [Environment] answer below that corresponds to your python version and installation directory. – MaxPRafferty Nov 18 '15 at 20:06
  • This approach is no longer good advice for Windows. Instead of adding Python's executable directly to path, it's better to allow the installer (a modern one) to install pylauncher. This adds a `py` command to `PATH`, and `py` can be used to invoke any specific installed minor version of Python without adding its `python.exe` to `PATH`. – jpmc26 Sep 25 '19 at 13:31
  • Developers often use Python virtual environments which does not need the Python executable in the PATH variable. https://docs.python.org/3/library/venv.html – lit Aug 07 '23 at 13:30

13 Answers13

98

Try setting the path this way:

 $env:path="$env:Path;C:\Python27"
Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
  • 3
    Sometimes you install Python on Windows and it doesn't configure the path correctly. Make sure you enter [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") in PowerShell to configure it correctly. You also have to either restart PowerShell or your whole computer to get it to really be fixed. –  May 26 '16 at 15:06
  • 4
    @LameBrains When you create something like a variable in a terminal it only exists for the current session. If you want to make it "permanent" you need to add it in a file that gets run at startup (profile / rc files). In the case of Powershell type $profile to find yours. – JackHasaKeyboard Apr 06 '17 at 01:43
  • 1
    I have Anaconda3 installed. This did the trick `$env:path="$env:Path;C:\ProgramData\Anaconda3"` and `$env:path="$env:Path;C:\ProgramData\Anaconda3\Scripts"` (the second command allowed me to access `conda` and `Jupyter notebook` – blaylockbk Jun 20 '18 at 19:29
  • @blaylockbk where do u set it in visual studio? settings.json? – ERJAN May 28 '20 at 12:13
27

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
20

$env:path="$env:Path;C:\Python27" will only set it for the current session. Next time you open PowerShell, you will have to do the same thing again.

The [Environment]::SetEnvironmentVariable() is the right way, and it would have set your PATH environment variable permanently. You just have to start PowerShell again to see the effect in this case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 3
    [Environment]::SetEnvironmentVariable() Can you please elaborate as to how to get that to work? You are right about the one time usage of Mike's command. – Man Dec 23 '13 at 03:47
11

The directory is not set correctly, so please follow these steps.

  1. "MyComputer"Right ClickProperties"System Properties""Advanced" tab

  2. "Environment Variables""Path""Edit"

  3. In the "Variable value" box, make sure you see following:

    ;c:\python27\;c:\python27\scripts
    
  4. Click OK. Test this change by restarting your Windows PowerShell. Type

    python
    
  5. Now Python version 2 runs! Yay!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HeggyHere
  • 131
  • 1
  • 4
  • C:\Python27\ C:\Python27\Scripts\ did it for me, Thanks! – Quang Van Mar 07 '17 at 16:30
  • 2
    This works! Added these to the path: `C:\ProgramData\Anaconda3` and `C:\ProgramData\Anaconda3\Scripts` – blaylockbk Jun 20 '18 at 19:48
  • Those using Visual Studio and Anaconda, add the following two paths : `C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64` `C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\Scripts` – Soumya Boral Jul 02 '18 at 17:29
9

For a permanent solution I found the following worked:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python 3.5")
Matt
  • 91
  • 1
  • 2
7

This works for me permanently:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27","User")

5

From the Python Guide, this is what worked for me (Python 2.7.9): [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")

datalifenyc
  • 2,028
  • 1
  • 20
  • 18
  • 2
    Awesome Thanks man i tried this solution after my Vs Code reset the path. It works like magic. but remember to restart after running this line of code in power shell – Taymour Niazi Jun 13 '20 at 08:06
4

Sometimes you install Python on Windows and it doesn't configure the path correctly.

Make sure you enter [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
in PowerShell to configure it correctly.

You also have to either restart PowerShell or your whole computer to get it to really be fixed.

2

For versions above Python 3.3, using the python command is no longer optimal and may lead to unexpected things like opening of Windows Store.

Intended way is to use python launcher which is shipped by default with official installation (and which is probably already installed on your system). Use simple py command where previously python was used. More details here.

For example, to check if launcher is presented and which installed python versions it finds: py -0p --list-paths To run script: py -3. main.py or simply py main.py To use directly in PowerShell as asked in this question: py

Additional confirmation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
halt9k
  • 527
  • 4
  • 13
1

Try the command this way:

cd C:\Users\XXX
python.exe

instead of:

cd C:\Users\XXX
python
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mao
  • 11
  • 1
1

As MaxPRafferty mentioned:

Just a note to anyone landing here from google, the answers setting path are all correct, but this problem probably stems from not giving the python installer administrative rights it needs to set the path itself. An alternative may be to simply right click the installer and select run as administrator, then repair the installation. If that still doesn't work, choose the [Environment] answer below that corresponds to your python version and installation directory. – MaxPRafferty Nov 18 '15 at 20:06

Maybe it is wise to let Python installer to add the path itself. The trap here is that by default Python installer does NOT add path for you. You should look carefully (by scrolling down to see what has been installed) during the installation process instead of directly nexting to the end.

What he missed saying is that you cannot run as administrator once you have installed it. Uninstall and reinstall may do, but the simplest is to right click and Troubleshoot compatibility, being careful this time to check the 'add path' in the "what to install" dialog before hiting next. Then restart powershell. Voilà. It works for me.

Community
  • 1
  • 1
Kangqiao Zhao
  • 111
  • 1
  • 8
1

To be able to use Python immediately without restarting the shell window you need to change the path for the machine, the process and the user.

Function Get-EnvVariableNameList {
    [cmdletbinding()]
    $allEnvVars = Get-ChildItem Env:
    $allEnvNamesArray = $allEnvVars.Name
    $pathEnvNamesList = New-Object System.Collections.ArrayList
    $pathEnvNamesList.AddRange($allEnvNamesArray)
    return ,$pathEnvNamesList
}

Function Add-EnvVarIfNotPresent {
Param (
[string]$variableNameToAdd,
[string]$variableValueToAdd
   ) 
    $nameList = Get-EnvVariableNameList
    $alreadyPresentCount = ($nameList | Where{$_ -like $variableNameToAdd}).Count
    #$message = ''
    if ($alreadyPresentCount -eq 0)
    {
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Machine)
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::Process)
    [System.Environment]::SetEnvironmentVariable($variableNameToAdd, $variableValueToAdd, [System.EnvironmentVariableTarget]::User)
        $message = "Enviromental variable added to machine, process and user to include $variableNameToAdd"
    }
    else
    {
        $message = 'Environmental variable already exists. Consider using a different function to modify it'
    }
    Write-Information $message
}


Function Get-EnvExtensionList {
    $pathExtArray =  ($env:PATHEXT).Split("{;}")
    $pathExtList = New-Object System.Collections.ArrayList
    $pathExtList.AddRange($pathExtArray)
    return ,$pathExtList
}


Function Add-EnvExtension {
Param (
[string]$pathExtToAdd
   ) 
    $pathList = Get-EnvExtensionList
    $alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
    if ($alreadyPresentCount -eq 0)
    {
        $pathList.Add($pathExtToAdd)
        $returnPath = $pathList -join ";"
        [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Machine)
        [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::Process)
        [System.Environment]::SetEnvironmentVariable('pathext', $returnPath, [System.EnvironmentVariableTarget]::User)
        $message = "Path extension added to machine, process and user paths to include $pathExtToAdd"
    }
    else
    {
        $message = 'Path extension already exists'
    }
    Write-Information $message
}

Function Get-EnvPathList {
    [cmdletbinding()]
    $pathArray =  ($env:PATH).Split("{;}")
    $pathList = New-Object System.Collections.ArrayList
    $pathList.AddRange($pathArray)
    return ,$pathList
}

Function Add-EnvPath {
Param (
[string]$pathToAdd
   ) 
    $pathList = Get-EnvPathList
    $alreadyPresentCount = ($pathList | Where{$_ -like $pathToAdd}).Count
    if ($alreadyPresentCount -eq 0)
    {
        $pathList.Add($pathToAdd)
        $returnPath = $pathList -join ";"
        [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Machine)
        [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::Process)
        [System.Environment]::SetEnvironmentVariable('path', $returnPath, [System.EnvironmentVariableTarget]::User)
        $message = "Path added to machine, process and user paths to include $pathToAdd"
    }
    else
    {
        $message = 'Path already exists'
    }
    Write-Information $message
}

Add-EnvExtension '.PY'
Add-EnvExtension '.PYW'
Add-EnvPath 'C:\Python27\'
Gaspare Bonventre
  • 1,134
  • 11
  • 19
0

Just eliminate the word "User". It will work.

Xshan
  • 1