【问题标题】:Python only works for AdminPython 仅适用于管理员
【发布时间】:2018-08-29 15:40:12
【问题描述】:

我关注了这个视频:https://www.youtube.com/watch?v=Y2q_b4ugPWk 并让我的 cmd 将 python 识别为路径变量。但这仅在我以管理员身份打开 cmd 和 powershell 时才有效。如何让它适用于所有用户?

作为管理员:

PS C:\windows\system32> python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

作为用户:

PS C:\Users> python
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ python
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users>

【问题讨论】:

    标签: python powershell cmd path


    【解决方案1】:

    使用提升的权限(右键单击powershell.exe,以管理员身份运行)

    $PathToPython = 'C:\whatever\containingfolderforpythonexe'
    $CurrentValue = [Environment]::GetEnvironmentVariable('Path', 'Machine')
    [Environment]::SetEnvironmentVariable('Path', "$CurrentValue;$PathToPython", 'Machine')
    

    【讨论】:

    • 非常感谢成功了,即使路径“C:\Program Files\py3.5”不是我保存 python.exe 的目录的实际路径,它如何工作?
    • @Mwspencer 您需要将该值更改为您拥有 python exe 的任何位置。 Windows 总体上非常智能,并且会搜索该目录(我只是在这里做了一个很好的假设)
    • 有趣的是它开箱即用,一定是巧合吗?我的 python.exe 位于 AppData/Roaming/Continuum/Anaconda,所以看起来很奇怪
    • @Mwspencer 是的,我只是在这里做了一个很好的猜测。我用一个可更新的变量更新了我的答案,以备将来使用
    【解决方案2】:

    我为我的 powershell 脚本构建了一个批处理开启器。一旦以提升的权限打开,任何其他调用的脚本也会被提升。 我用 .vbs 做类似的事情来在管理员中打开批处理。

    @ECHO off
    ECHO Please Wait.
    powershell.exe -ExecutionPolicy ByPass -file "File.ps1"
    ECHO.
    ECHO PRESS ANY KEY ONCE DEBUGGED
    Pause >nul
    exit
    

    【讨论】:

    • @Echo off 意味着您不必在屏幕上看到脚本。 Please waitPRESS ANY KEY ONCE DEBUGGED 仍然显示
    • 这意味着如果不使用@ECHO Message here 之类的内容,您将看不到任何输出到标准输出的屏幕上。因此,您的“请稍候”等消息永远不会被看到。仍然可以看到在 powershell 中输出的命令。
    • 在声明任何内容之前测试代码。我已经在 100 台不同的机器上运行该代码,只要您首先声明 ECHO,它就会显示在屏幕上。
    • 嗯,一定是在考虑不同的指令,谢谢提供信息
    猜你喜欢
    • 2020-02-18
    • 2015-03-03
    • 2017-01-29
    • 2018-12-11
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多