在终端中设置路径 [英] setting path in terminal

查看:116
本文介绍了在终端中设置路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Mac上安装Homebrew,以便安装Macvim.安装Homebrew后,系统会提示我运行brew doctor,这反过来会产生许多警告.我已经解决了其中的几个问题,但仍停留在两个问题上:

I am installing Homebrew on my Mac in order to install Macvim. After installing Homebrew, I am prompted to run brew doctor, which in turn generates a number of warnings. I have resolved several of these but am stuck on two:

警告:在您的PATH中发现了Python. 这可能会导致构建问题,因为此软件会自行安装 将iconv和libxml2的副本复制到以下目录中: 其他构建系统.

Warning: Enthought Python was found in your PATH. This can cause build problems, as this software installs its own copies of iconv and libxml2 into directories that are picked up by other build systems.

我不确定是应该忽略掉它还是寻找卸载Enthought Python的方法.

I'm not sure if I should simply ignore this or look for a way to uninstall Enthought Python.

警告:/usr/bin出现在/usr/local/bin之前 这意味着将使用系统提供的程序,而不是那些 由Homebrew提供.两种路径中都存在以下工具:

Warning: /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. The following tools exist at both paths:

git
git-cvsserver
git-receive-pack
git-shell
git-upload-archive
git-upload-pack

考虑设置您的PATH,以便/usr/local/bin 出现在/usr/bin之前.这里是单线: echo export PATH ="/usr/local/bin:$ PATH" >>〜/.bash_profile

Consider setting your PATH so that /usr/local/bin occurs before /usr/bin. Here is a one-liner: echo export PATH="/usr/local/bin:$PATH" >> ~/.bash_profile

我跑了

echo export PATH ="/usr/local/bin:$ PATH" >>〜/.bash_profile

echo export PATH="/usr/local/bin:$PATH" >> ~/.bash_profile

几次,但继续收到相同的警告.这是我的路径

several times but continue to get the same warning. Here is my PATH

nngrey$ echo $PATH
/Users/nngrey/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/bin:/Users/nngrey/.rvm/gems/ruby-2.0.0-p247@global/bin:/Users/nngrey/.rvm/rubies/ruby-2.0.0-p247/bin:/Users/nngrey/.rvm/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/nngrey/bin

好的,我在这里找到了第二个问题的答案:

Okay, I found the answer to my second question here: How to modify PATH for Homebrew?

我只需要重新启动终端即可.不过,仍然不确定如何处理Python.

I just needed to restart the terminal. Still not sure what to do about Python, though.

任何建议将不胜感激.谢谢

Any suggestions would be appreciated. Thanks

推荐答案

要解决这两个问题:编辑您的~/.bash_profile文件

/usr/bin occurs before /usr/local/bin警告:

此警告告诉您$PATH环境变量将/usr/bin列出的优先级高于/usr/local/bin,我认为这是home brew安装软件包的默认位置.

To solve both issues: Edit your ~/.bash_profile file

The /usr/bin occurs before /usr/local/bin warning:

This warning is telling you that the $PATH environment variable is listing /usr/bin as higher priority than /usr/local/bin, which I presume is the default location that home brew installs packages to.

如果忽略此选项,则意味着如果您已经在先前已将程序安装(不使用"home brew"的情况下)安装到其他位置之后使用"home brew"安装或升级了程序,则外壳程序可能会使用较旧的副本,而不是较新的家庭酿造"安装版本.

If you ignore this, then this means that if you install or upgrade a program using 'home brew' after already having that program previously installed (without using 'home brew') to different location, the shell might invoke commands using the older copy and not the newer 'home brew'-installed version.

$PATH变量可能是Unix Shell环境变量中最重要的变量(您可以键入env列出shell中的环境变量),因为它定义了计算机在查找时将在其中搜索的目录顺序用于执行程序.首先指定优先级最高的路径.

The $PATH variable is perhaps the most important of Unix Shell environment variables (you can type env to list the environment variables in the shell) in that it defines the order of directories in which the computer will search when looking for programs to execute. The highest priority path names are specified first.

您可以通过输入Terminal.app来查看~/.bash_profile的内容:

You can view the contents of your ~/.bash_profile by typing in Terminal.app:

$ cat ~/.bash_profile

记住:不要键入$,它只是指示终端提示.

它应该打印出这样的内容:

It should print out something like this:

# Setting PATH for EPD_free-7.3-2
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH

请注意,Enthought的注释表明他们将原始~/.bash_profile保留在同一目录($HOME)中名为.bash_profile.pysave的副本中.如果您希望可以查看该文件,请使用cat或文本编辑器将其检出.如果您决定恢复到原来的状态,则可以通过删除~/.bash_profile文件并将.bash_profile.pysave重命名为.bash_profile来实现. (这不是我的选择,因为过去我必须第二次安装Enthought的发行版(可能在其自身之上),所以我的.bash_profile.pysave仍然包含Enthought的配置,而我的~/.bash_profile.列出了两个的副本理想的配置.)

Note that Enthought's comment indicates that they preserved the original ~/.bash_profile in a copy called .bash_profile.pysave in the same directory ($HOME). If you want you can view that file, check it out with cat or a text editor. If you decide to revert back to it, you can do so by removing the ~/.bash_profile file and by renaming .bash_profile.pysave to .bash_profile. (This wasn't an option for me, as I must have in the past installed Enthought's distribution a second time (possibly on top of itself), so my .bash_profile.pysave still contained Enthought's configurations, and my ~/.bash_profile. listed two copies of the Enthought configurations.)

这是Enthought发行版,将$PATH env变量设置为${PATH}.要显示${PATH}变量的含义,请在外壳中输入echo $PATH.它应该表明Enthought自己的目录具有最高优先级(因为它们位于第一位):

This is the Enthought distribution setting the $PATH env variable to ${PATH}. To show what the ${PATH} variable holds, lets enter echo $PATH into the shell. It should show that Enthought's own directories have first priority (because they come first):

$ echo $PATH
/Library/Frameworks/Python.framework/Versions/Current/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin

如果仔细查看上面的示例,您会发现 /usr/local/bin在列表(!)中出现两次,一次出现在第三位,一次出现在倒数第二位. /usr/bin发生在它们之间,但是brew doctor的解析器正在检测问题(这可能是brew doctor错误,仅检查尾随的/usr/local/bin,但是几乎可以肯定有一个Enthought错误,因为他们离开了我包含两个/usr/local/bin$PATH定义.如果您两次都没有看到/usr/local/bin/,则可能是因为我可能在终端上安装了两次Enthought..

If you look closely at the above example, you can see that /usr/local/bin occurs twice in the list (!), once in third position and once in the second to last position. /usr/bin occurs between them, but the parser for brew doctor is detecting a problem (this could be a brew doctor bug that is only checking for a trailing /usr/local/bin, but there is almost certainly an Enthought bug because they left my $PATH definition with two /usr/local/bin's in there. If you don't see /usr/local/bin/ twice, then it could be because I may have had installed Enthought twice on my end.).

因此,即使这里可能没有真正的问题,brew doctor仍然这样认为,并且我们仍然需要摆脱具有最高优先级的Enthought目录,因为如果我们想获取一个不同的目录,这些条目可能会干扰将来会使用家庭酿造"来分发Python.

So, even though there may not really be a problem here, brew doctor still thinks so and we also still need to get rid of Enthought's directories placed with highest priority, because those entries could interfere if we wanted to acquire a different Python distribution in the future using 'home brew'.

shell将按从左到右的顺序在$PATH变量中查找这些位置,以执行命令等. 请记住,路径之间用冒号(:)隔开,并按从左到右的降序排列,优先级最高.

The shell will look to these locations in the $PATH variable from left to right order for executing commands, etc. Remember, the paths are separated by colons (:) and are listed in descending priority from left to right, with highest priority first.

$ echo 'export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"' >> ~/.bash_profile

(这会将(>>运算符)回显的字符串附加到~/.bash_profile文件的末尾.如果使用此命令,您仍然需要编辑〜/.bash_profile以便稍后删除Enthought内容)

(This will append (the >> operator) the echoed string to the end of the ~/.bash_profile file. If you use this command, you will still need to edit your ~/.bash_profile to remove the Enthought stuff later)

点击此处并向下滚动至第3步,以获取有关此特定命令的更多信息.

Click here and scroll down to step 3 for more info on this particular command.

或者您可以cd进入$HOME目录,并将上述命令中的字符串手动添加到您的.bash_profile中,如下文所示,用于删除Enthought的配置.

Or you can cd into the $HOME directory and add the string in the above command to the your .bash_profile manually, as I show below for removing Enthought's configurations.

更改到主目录:

$ cd $HOME

例如在TextEdit中编辑文件:

To edit the file in, for instance, Text

$ open -a "TextEdit" .bash_profile

找到这两行:

PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH

并通过在每行前面放置#来注释掉它们.然后保存文件.

And comment them out by placing an # in front of each line. Then save the file.

(如果您打算不再次使用Enthought发行版,则可以删除这些行和其他注释,而不必将其注释掉)

$ vim ~/.bash_profile

如果您没有写权限,则可能无法更改~/.bash_profile文件.如果是这样,请退出vim(ESC,然后键入:q)并通过在上述vim命令前加上sudo和一个空格来更改为超级用户.您可以稍后在外壳中再次键入exit来退出sudo(超级用户执行").

If you do not have write privileges you may be prevented from changing the ~/.bash_profile file. If so, exit vim (ESC, then type :q) and change to super user by prepending the above vim command with sudo and a space. You can exit sudo ('super user do') later by typing exit once back in the shell.

vim第一次打开时,会显示命令模式,在该模式下键入将完全不会更改文件内容,而是会在窗口底部的提示符处添加命令(例如:q以退出,或:w写入(保存)更改.

When vim first opens up, you are presented with the command mode, where typing will not change the file contents at all, but add commands to the prompt at the bottom of the window (such as :q to quit, or :w to write (save) changes).

在vim中,您可以使用箭头键(或HJKL键)移动.找到要编辑的行后,按I键进入-INSERT-模式并开始在光标前插入字符(在命令模式下按A键也将退出命令模式,但会开始在光标后的 处插入文本.

In vim, you can move around with the arrow keys (or the H, J, K, L keys). Once you've located the line to edit, press the I key to enter -INSERT- mode and start inserting characters before the cursor (Pressing the A key in command mode will also exit command mode but will start inserting text after the cursor).

如果您认为将来可能要使用Enthought发行版,则可以通过在每行的开头放置#来注释掉所有行,如下所示:

# Setting PATH for EPD_free-7.3-2
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
# export PATH
# export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

如果您没有计划使用Enthought发行版,或者已经卸载了它,只需从〜/.bash_profile文件中删除以上所有行.

If you have no plans to use the Enthought distribution or you have already uninstalled it, just delete all of the above lines from the ~/.bash_profile file.

要为$ PATH环境变量声明一个新定义,请在下面添加以下行:

To declare a new definition for the $PATH environment variable, add this line below:

export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

完成文件编辑后,按ESC离开-INSERT-模式并返回命令模式.然后键入:wq来写入更改并退出vim编辑器.然后,您将返回到外壳.

Once you are done editing the file, press ESC to leave -INSERT- mode and return to command mode. then type :wq to write the changes and quit the vim editor. You will then be returned to the shell.

$ source .bash_profile

如果仅注释掉Enthought行,则可以返回到.bash_profile,并通过取消注释这两行来再次启用Enthought分发.

If you only commented out the Enthought lines, you'll be able to return to your .bash_profile and enable the Enthought distribution again by uncommenting those two lines.

现在运行brew doctor,您应该一切顺利.如果它没有没有警告就无法运行,请杀死Terminal并重新打开它,然后重试.

Now run brew doctor and you should be good to go. If it doesn't run without warnings, kill Terminal and reopen it and try again.

正在运行的echo $PATH现在应该显示:

Running echo $PATH should now show:

/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin

这篇关于在终端中设置路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆