[英]I'm trying to use python in powershell
I'm trying to follow Zed Shaw's guide for Learning Python the Hard Way.我正在尝试按照 Zed Shaw 的指南学习 Python 艰难之路。 I need to use python in Powershell. I have Python 2.7.3 installed in C:\Python27
.我需要在 Powershell 中使用 python。我在C:\Python27
中安装了 Python 2.7.3。 Whenever I type python into Powershell, I get an error that says the term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.每当我在 Powershell 中键入 python 时,我都会收到一条错误消息,指出术语“python”未被识别为 cmdlet、function、脚本文件或可运行程序的名称。 I also typed in this: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
That was a suggested solution provided, but typing python into Powershell still does nothing.我还输入了这个: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
这是提供的建议解决方案,但是在 Powershell 中输入 python 仍然没有任何作用。 I can type in "start python" and it opens up a window with python but I need it in Powershell. Thanks.我可以输入“启动 python”,它会打开一个 window 和 python,但我需要它在 Powershell。谢谢。
Try setting the path this way:尝试以这种方式设置路径:
$env:path="$env:Path;C:\Python27"
For what's worth, this command did it for me (Python3.3):值得一提的是,这个命令为我做了 (Python3.3):
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Python33", "Machine")
I just had to restart the Powershell after that.之后我只需要重新启动 Powershell。
$env:path="$env:Path;C:\Python27"
will only set it for the current session. $env:path="$env:Path;C:\Python27"
只会为当前会话设置它。 Next time you open Powershell, you will have to do the same thing again.下次打开 Powershell 时,您将不得不再次执行相同的操作。
The [Environment]::SetEnvironmentVariable()
is the right way, and it would have set your PATH environment variable permanently. [Environment]::SetEnvironmentVariable()
是正确的方法,它会永久设置您的 PATH 环境变量。 You just have to start Powershell again to see the effect in this case.在这种情况下,您只需再次启动 Powershell 即可查看效果。
The Directory is not set correctly so Please follow these steps.目录设置不正确,因此请按照以下步骤操作。
In the "Variable value" box, Make sure you see following:在“变量值”框中,确保您看到以下内容:
;c:\python27\;c:\python27\scripts ;c:\python27\;c:\python27\scripts
Click "OK", Test this change by restarting your windows powershell.单击“确定”,通过重新启动 windows powershell 来测试此更改。 Type类型
python Python
Now python version 2 runs!现在 python 版本 2 运行了! yay!耶!
For a permanent solution I found the following worked:对于永久解决方案,我发现以下方法有效:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python 3.5")
This works for me permanently:这对我永久有效:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27","User")
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")
从Python Guide中,这对我有用(Python 2.7.9): [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")
Sometimes you install Python on Windows and it doesn't configure the path correctly.有时您在 Windows 上安装 Python,但它没有正确配置路径。
Make sure you enter [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
确保输入[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
in PowerShell to configure it correctly.在 PowerShell 中正确配置它。
You also have to either restart PowerShell or your whole computer to get it to really be fixed.您还必须重新启动 PowerShell 或您的整个计算机才能真正修复它。
Try the Command this way:以这种方式尝试命令:
PS C:\Users\XXX>python.exe
instead of:代替:
C:\Users\XXX>python
As MaxPRafferty mentioned :正如MaxPRafferty 提到的:
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.请注意任何从谷歌登陆这里的人,答案设置路径都是正确的,但这个问题可能源于没有给 python 安装程序管理权限,它需要设置路径本身。 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.如果仍然不起作用,请选择下面与您的 python 版本和安装目录相对应的 [Environment] 答案。 – MaxPRafferty Nov 18 '15 at 20:06 – MaxPRafferty 15 年 11 月 18 日 20:06
Maybe it is wise to let Python installer to add the path itself.也许让 Python 安装程序自己添加路径是明智的。 The trap here is that by default Python installer does NOT add path for you.这里的陷阱是默认情况下 Python 安装程序不会为您添加路径。 You should look carefully (by scrolling down to see what has been installed) during the installation process instead of directly next
ing to the end.安装过程中应该仔细查看(通过向下滚动查看已安装的内容),而不是直接next
到最后。
What he missed saying is that you cannot run as administrator
once you have installed it.他没有说的是,一旦你安装了它,你就不能run as administrator
。 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
.卸载并重新安装可能会,但最简单的是右键单击并Troubleshoot compatibility
问题,这次要小心,在点击next
之前检查“要安装的内容”对话框中的“添加路径”。 Then restart powershell.然后重启powershell。 Voilà.瞧。 It works for me.这个对我有用。
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.为了能够在不重新启动 shell 窗口的情况下立即使用 Python,您需要更改机器、进程和用户的路径。
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\'
Just eliminate the word "User".只需删除“用户”一词。 It will work.它会起作用。
For python versions above 3.3 using pyton
command is no longer optimal and may lead to unexpected things like opening of Windows Store.对于 python 3.3以上的版本,使用pyton
命令不再是最佳选择,并且可能会导致意外事件,例如打开 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).预期的方法是使用 python 启动器,它默认随官方安装一起提供(并且可能已经安装在您的系统上)。 Use simple py
command where previously python
was used.在之前使用python
的地方使用简单的py
命令。 More details here .更多细节在这里。
For example,例如,
To check if launcher is presented and which installed python versions it finds: py -0p --list-paths
要检查启动器是否存在以及安装了它找到的 python 版本: py -0p --list-paths
To run script: py -3. main.py
运行脚本: py -3. main.py
py -3. main.py
or simply py main.py
py -3. main.py
或者简单py main.py
To use directly in powershell as asked in this question: py
按照这个问题中的要求直接在 powershell 中使用: py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.