【问题标题】:Alias not updating Python Binary别名未更新 Python 二进制文件
【发布时间】:2021-05-19 09:55:23
【问题描述】:

以下是理解我的问题的一些必要上下文(顺便说一下,我的 shell 是 /bin/bash):

~
$ alias
alias dotfiles='/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME'
alias ll='ls -al'
alias pip='pip3'
alias python='/usr/local/bin/python3'
~
$ which python
/usr/bin/python
~
$ which python3
/usr/local/bin/python3
~
$ python3 --version
Python 3.9.5
~
$ python --version
Python 3.9.5
~
$ ll /usr/bin/python*
lrwxr-xr-x  1 root  wheel     75 Feb  1  2020 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel     82 Feb  1  2020 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x  1 root  wheel     75 Feb  1  2020 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel     75 Feb  1  2020 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel     82 Feb  1  2020 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x  1 root  wheel  31488 Oct 30  2020 /usr/bin/python3
lrwxr-xr-x  1 root  wheel     76 Feb  1  2020 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x  1 root  wheel     76 Feb  1  2020 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
~
$ ll /usr/local/bin/python*
lrwxr-xr-x  1 marshallmcquillen  admin  38 May 18 06:14 /usr/local/bin/python3 -> ../Cellar/python@3.9/3.9.5/bin/python3
lrwxr-xr-x  1 marshallmcquillen  admin  45 May 18 06:14 /usr/local/bin/python3-config -> ../Cellar/python@3.9/3.9.5/bin/python3-config
lrwxr-xr-x  1 marshallmcquillen  admin  40 May 18 06:14 /usr/local/bin/python3.9 -> ../Cellar/python@3.9/3.9.5/bin/python3.9
lrwxr-xr-x  1 marshallmcquillen  admin  47 May 18 06:14 /usr/local/bin/python3.9-config -> ../Cellar/python@3.9/3.9.5/bin/python3.9-config

我的问题:

  1. 为什么我的 python 别名不起作用? (我也试过在我的~/.bash_profile 中添加alias python=python3,但它仍然没有指向正确的二进制文件)

  2. 为什么python 链接指向(似乎是)python2.7 二进制文件,但版本显示为 3.9.5?

  3. #1 或#2 的答案是否与the difference between brew install and pip3 install 有某种关系?

【问题讨论】:

  • 当您运行程序时,系统首先从alias 获取指向python3 的路径。当您使用 which 时,它不会检查别名,而只会检查真实文件 - 可能使用 update-alternatives 设置
  • 正在为我们所看到的一切工作。当您运行 python --version 时,它会输出我们期望的结果。这里真正的问题可能是"why is which the wrong tool?"

标签: bash shell unix alias


【解决方案1】:

你使用了错误的命令:

which P

根据定义在 PATH 中搜索名为 P 的可执行文件。

要查看 bash 实际如何解释命令,您可以执行以下操作

type P

在你的情况下:

type python

顺便说一句,在您的 PATH 中赋予与可执行文件同名的别名可能会让人感到困惑。由于别名的目的是简化在命令行上的输入,因此我建议您将 Python 的别名命名为简短的名称,例如 py

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2013-12-06
    • 1970-01-01
    相关资源
    最近更新 更多