os x,python,自制软件 -> 发生了一些奇怪的事情

os x, python, homebrew -> something weird going on

希望有人能帮助我。我遵循了一些关于为 python 开发设置新机器的指南。 (one) (two)

我几乎遵循了所有内容,但我对 .bash_profile

的变化感到非常困惑

当我在 .bash_profile 中注释掉 export PATH=/usr/local/bin:$PATH 并在终端中输入 which python 时,我看到了我所期望的。

which python
/usr/local/bin/python
python
Python 2.7.11 (default, Jan 22 2016, 08:29:18) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

bash_profile 中发生了什么,为什么我注释掉所有内容、保存它和 运行 命令都没关系?

更重要的是,如何进入 python 的默认安装并确保它仍然存在且不受影响?我是否正确理解上述版本是由 Homebrew 安装的 python?

.bash_profile 仅在加载 Bash shell 时才被读取,因此在加载 shell 之后更改此文件不会影响它。要在修改并保存文件后查看更改,需要重新启动 shell 或 运行 source ~/.bash_profile.

修改PATH环境变量不会影响Python本身,它只会修改首先搜索到的执行位置,你什么时候会运行python ... .例如,如果您在 /a/python/b/python 中安装了 python,那么:

  • 如果你的 PATH 设置为 /a/python:/b/python:$PATH 而你 运行 python ....py 实际上 /a/python ....py 被执行了;

  • 如果你的 PATH 设置为 /b/python:/a/python:$PATH 而你 运行 python ....py 实际上 /b/python ....py 被执行了。

如果您没有修改 PATH(例如,在 shell 中执行类似 export PATH=/usr/local/bin:$PATH 的操作或将其添加到 .bash_profile),则执行 which python 应该显示默认的 python 路径。

在您描述的情况下,它显示了 python,您通过修改 PATH 将其设置为使用。要查看默认值,注释 export... 字符串,保存 .bash_profile 文件和 运行 source ~/.bash_profile 或重新启动 shell,然后才能看到 which python输出。