【问题标题】:How to delete a certain path from $PATH on mac如何在 mac 上从 $PATH 中删除某个路径
【发布时间】:2013-02-17 16:34:48
【问题描述】:

我需要在 Mac OS X Lion 上的 $PATH 变量中删除 /Library/Frameworks/Python.framework/Versions/2.7/bin。我打开了几个文件,它们在路径中添加了一些内容,但没有找到添加 python 路径的位置。这是几个文件和路径变量的输出:

echo $PATH 返回

/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Users/friedrich/.rvm/gems/ruby-1.9.3-p362/bin:/Users/friedrich/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/friedrich/.rvm/rubies/ruby-1.9.3-p362/bin:/Users/friedrich/.rvm/bin:/Users/friedrich/.rbenv/shims:/Users/friedrich/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/usr/local/git/bin

文件/etc/paths/

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

文件~/.profile

export PATH=$PATH:/usr/local/git/bin:/usr/local/Cellar/todo-txt/2.9/bin

文件~/.bash_profile

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

# todo.txt script path
source /usr/local/Cellar/todo-txt/2.9/etc/bash_completion.d/todo_completion complete -F _todo t

# MacPorts Installer addition on 2013-03-01_at_21:26:56: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

最后一个向路径添加内容的文件似乎是.bash_profile 添加export PATH=/opt/local/bin:/opt/local/sbin:$PATH,所以我需要在该文件之前添加一些内容。

【问题讨论】:

  • 查看更改顺序需要查看/usr/local/Cellar/todo-txt/2.9/etc/bash_completion.d/todo_completion

标签: python macos python-2.7 path terminal


【解决方案1】:

还有更多方法可以在 OS X 上向 PATH 中添加内容。

除了/etc/paths 文件,还有一个目录/etc/paths.d。在该目录中有与/etc/paths 格式相同的文本文件。 /etc/paths.d 中每个文件的行添加到您的 PATH 中 /etc/paths 中的行之后,.bash_profile 中的内容之前(尽管这取决于 .bash_profile 中的确切内容)。

如果您不需要的 Python 框架路径不在上面,我可以考虑尝试其他两个地方。您的主目录中还有/private/etc/paths/private/etc/paths.d,可能还有.bashrc 文件。

我希望这对你有用。如果没有,那么我将使用的解决方法是在.bash_profile 中创建最后一个命令,将/usr/bin 附加到PATH 的前面。这样,Apple 版本的 Python(链接到 /usr/bin)将在您的 Python 框架版本之前找到。

【讨论】:

    【解决方案2】:

    考虑到路径添加到$PATH 变量的顺序(因此您要删除第三个元素),您可以在.bash_profile 的末尾添加这样的命令:

     export PATH=`echo $PATH | cut -d":" -f1,2,4-`
    

    【讨论】:

    • 刚刚尝试过,它在一个空的 $PATH 中解析。好像是剪错了。
    • 如果从 .bash_profile 中删除新添加的导出,重新打开终端并在终端中运行 echo $PATH | cut -d":" -f3 --complement 会输出什么?
    • cut: illegal option -- - usage: cut -b list [-n] [file ...] cut -c list [file ...] cut -f list [-s] [-d delim] [file ...]
    • 我已经编辑了我的帖子。请检查命令的新形式。
    • 剪裁之类的事情对我来说似乎非常脆弱。如果你的 $PATH 改变会发生什么?项目可以pre-preended 到路径中,即使在运行脚本或诸如此类的单个会话期间也是如此。最好找到要删除的条目的来源,即使您必须执行 sudo find / -print | xargs grep <path-to-remove> 并让它运行一整夜。
    【解决方案3】:

    您可以使用参数扩展来修剪路径的固定部分。将此添加到 .bash_profile 的末尾(或者至少,在添加违规路径之后处理它的时间足够晚):

    PATH=${PATH/\/Library\/Frameworks\/Python.framework\/Versions\/2.7\/bin:}
    

    (所有的反斜杠都是不幸的,但你至少只需要输入一次。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2012-07-23
      • 2023-04-08
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多