尽管 ~/.bashrc 和 ~/.bash_aliases 中有导出和别名,但终端中的默认 python 不会改变

Default python in terminal won't change despite exports and aliases in ~/.bashrc and ~/.bash_aliases

我已按照 this, and this.

等示例中的说明进行操作

我的 ~/.bashrc 文件中有这一行

export python="/usr/local/bin/python3.6"

这行在我的 ~/.bash_aliases 文件中

alias python='/usr/local/bin/python3.6'

这是我的 PATH 变量

/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS

而且我还有下面的python版本和路径

User$ which python
/usr/bin/python
User$ python --version
Python 2.7.10

我也已经尝试退出终端并重新打开

这是目录 /usr/local/bin/python3.6* 和 /usr/bin/python*

中的内容
User$ /usr/local/bin/python3.6
python3.6          python3.6-config   python3.6m-config  
python3.6-32       python3.6m         
User$ /usr/bin/python
python            python2.6-config  pythonw           
python-config     python2.7         pythonw2.6        
python2.6         python2.7-config  pythonw2.7 

谢谢

将这些复制到您的 .bash_profile 文件中,这是 OS X 中的终端会话所寻找的。此外,正如其他人所提到的,将 python 导出为环境变量不会做任何事情 - 别名应该足够了。

我认为问题可能是,您启动了一个登录 shell,它寻找 ~/.bash_profile,而 ~/.bash_aliases 不是来自 ~/.bash_profile .因此 ~/.bash_aliases 中的 alias 命令从未执行过。您可以通过调用 alias python.

来检查这一点

顺便说一句,导出一个名为 python 的变量在您的情况下毫无意义,因为它 永远不会被使用。

关于读取了哪些~/.bash*文件,可以参考man bashINVOCATION章节。这是其中的一些。

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.