【问题标题】:Different python used in .cmd when file is ran vs. when python is opened directly运行文件时与直接打开 python 时在 .cmd 中使用的不同 python
【发布时间】:2020-05-03 13:30:02
【问题描述】:

在 Windows 10 上:

您好,我在理解我的计算机如何使用 python 时遇到问题。当我在我的 IDE Atom 中运行 python 文件时,它使用来自 PATH 的 python。当我在 .cmd 中运行 python 时,它使用 PATH 中的 python。当我从 .cmd 执行相同的 python 文件时,它使用不在 PATH 中的 python。为什么要这样做,我怎样才能让它使用相同的 python?我希望一切都使用 anaconda python。

我不明白 \AppData\ python 来自哪里?

![ ]4

【问题讨论】:

    标签: python path


    【解决方案1】:

    您在控制面板中看到的只是一个模板(如果您愿意的话)。每个进程在启动时都会获得这些设置,但之后可以修改它们。举个例子:

    1. 启动cmd.exe:
      1. echo %PATH% - 将输出继承的值
      2. set PATH=%PATH%;some_other_dir - 将更改当前 (cmd.exe) 进程中的继承值
      3. 重复 #11. - 将输出更新后的值
      4. 再次打开控制面板设置。你不会在那里看到 some_other_dir

    要检查当前的Python 进程环境(继承自父进程),请使用os.environ。添加你的脚本:

    import os
    
    print(os.environ["PATH"])
    

    所以,非 Anaconda Python 实际上可能在 %PATH% 中。

    但是由于[Wikipedia]: File association,这种行为也可能是(我倾向于这个)。更多详情,请查看[SO]: How do I set “default App” for a file extension to an “.exe” on Windows 10 after April 2018 update (@CristiFati's answer)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多