如何使用brew安装的Python作为默认Python?

问题:如何使用brew安装的Python作为默认Python?

我尝试在Mac OS X 10.6.2上切换到Homebrew(在使用了fink和macport之后)。我已经安装了python 2.7

brew install python 

问题是,与Macport相反,似乎没有python_select实用程序,而我的默认mac python始终是默认的

which python

给我吗

/usr/bin/python

而且/usr/bin/python不是符号链接

我如何才能使python brew成为我的默认python?

I try to switch to Homebrew (after using fink and macport) on Mac OS X 10.6.2. I have installed python 2.7 with

brew install python 

The problem is that, contrary to Macport, it seems that there is no python_select utility, and my default mac python is always default

which python

give me

/usr/bin/python

and /usr/bin/python is not a symlink

How can I do to make python brew flavour to be my default python ?


回答 0

使用Homebrew时,以下命令可以提供更好的效果:

brew doctor

输出:

==> / usr / bin出现在/ usr / local / bin之前,这意味着将使用系统提供的程序代替Homebrew提供的程序。如果您例如这是一个问题。brew安装了Python。

考虑编辑.bash_profile,以将/ usr / local / bin放在$ PATH中的/ usr / bin之前。

As you are using Homebrew the following command gives a better picture:

brew doctor

Output:

==> /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. This is an issue if you eg. brew installed Python.

Consider editing your .bash_profile to put: /usr/local/bin ahead of /usr/bin in your $PATH.


回答 1

请参阅:如何在Homebrew中符号链接python?

$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python

See: How to symlink python in Homebrew?

$ brew link --overwrite python
Linking /usr/local/Cellar/python/2.7.3... 28 symlinks created
$ which python
/usr/local/bin/python

回答 2

快速解决:

  1. 打开 /etc/paths
  2. 更改行的顺序(最高优先级在顶部)

就我而言/etc/paths

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

如果您想了解有关OSX中路径的更多信息,我发现本文非常有用:

http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/

Quick fix:

  1. Open /etc/paths
  2. Change the order of the lines (highest priority on top)

In my case /etc/paths looks like:

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

If you want to know more about paths in OSX I found this article quite useful:

http://muttsnutts.github.com/blog/2011/09/12/manage-path-on-mac-os-x-lion/


回答 3

如自制软件安装程序本身所建议,请确保将其添加到您的.bashrc.zshrc

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

As suggested by the homebrew installer itself, be sure to add this to your .bashrc or .zshrc:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

回答 4

我为OSX High Sierra做过“ brew install python”。该$PATH/usr/local/bin任何其他的路径之前,但仍然which python是指向系统的Python。

当我深入研究时,发现处没有python可执行文件/usr/local/bin。可执行文件名为python2。要解决此问题,请创建python指向以下内容的符号链接python2

/usr/local/bin $: ln -s python2 python

I did “brew install python” for OSX High Sierra. The $PATH had /usr/local/bin before any other path but still which python was pointing to the system’s python.

When I looked deeper I found that there is no python executable at /usr/local/bin. The executable is named python2. To fix this problem create a symbolic link python pointing to python2:

/usr/local/bin $: ln -s python2 python


回答 5

自制软件不会替换“ / usr / bin”中的内容。您只需要在路径中将“ / usr / local / bin”放在“ / usr / bin”之前,然后“哪个python”将为您提供“ / usr / local / bin / python”。

强烈建议不要替换/ usr / bin / python(或/ usr / bin / ruby​​)。

Homebrew does NOT replace stuff in “/usr/bin”. You’ll just want to put “/usr/local/bin” ahead of “/usr/bin” in your path, then “which python” will give you “/usr/local/bin/python”.

Replacing /usr/bin/python (or /usr/bin/ruby) is highly unrecommended.


回答 6

python现在使用公式(目前python3为v3.6.5),brew将链接目录

/usr/local/opt/python -> ../Cellar/python/3.6.5

它还将链接二进制文件

/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3

如果仍然需要使用python2.x,请使用:

brew install python@2

要使用homebrew python,只需将其目录放在PATH中以进行bash:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

对于鱼:

set -x PATH /usr/local/opt/python/libexec/bin $PATH

注意:

  1. 这样做会遮盖系统的默认版本 python
  2. 用于将python链接到/usr/local/share/python较旧版本的homebrew 。

python formula now uses python3(v3.6.5 for now), brew will link the directory:

/usr/local/opt/python -> ../Cellar/python/3.6.5

it will also link the binary:

/usr/local/bin/python3 -> ../Cellar/python/3.6.5/bin/python3

If you still need to use python2.x, use:

brew install python@2

To use homebrew’s python, just put its directory in PATH, for bash:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

for fish:

set -x PATH /usr/local/opt/python/libexec/bin $PATH

Note:

  1. doing this will shadow the system default version of python
  2. homebrew used to link python to /usr/local/share/python in older versions.

回答 7

修改$ PATH,将其添加到bashrc或bash_profile中:

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

更多点击这里: 问题#89791

Modify your $PATH, Add this in your bashrc or bash_profile:

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

more click here: Issue #89791


回答 8

您需要编辑PATH环境变量,以确保在/ usr / bin之前搜索自制python所在的位置。您还可以在shell配置中进行设置,以将诸如PYTHON之类的变量设置为所需的python版本并调用,$PYTHON而不是python从命令行调用。

另外,正如另一位发帖人所述(尤其是在Mac上),请勿弄乱/ usr / bin中的python将其指向另一个python安装。您只是在自找麻烦。

You need to edit your PATH environmental variable to make sure wherever the homebrew python is located is searched before /usr/bin. You could also set things up in your shell config to have a variable like PYTHON be set to your desired version of python and call $PYTHON rather than python from the command line.

Also, as another poster stated (and especially on mac) DO NOT mess with the python in /usr/bin to point it to another python install. You’re just asking for trouble if you do.


回答 9

我所做的brew install python,我的$PATH还是不错的,不过,which python给我的系统中安装了一个。重新启动终端,将其修复。

I did brew install python, my $PATH was good, but still, which python gave me the system installed one. Restarting the terminal fixed it.


回答 10

python现在指向python3,如果需要,python 2请执行以下操作: brew install python@2然后在.zshrc 或.bashrc文件中, export PATH="/usr/local/opt/python@2/libexec/bin:$PATH" 现在pyhon --version= Python 2.7.14和python3 --version= Python 3.6.4。这就是我在终端中经常看到的行为。

python now points to python3, if you need python 2 then do: brew install python@2 and then in your .zshrc or .bashrc file export PATH="/usr/local/opt/python@2/libexec/bin:$PATH" Now, pyhon --version = Python 2.7.14 and python3 --version = Python 3.6.4. That’s the behavior I’m used to seeing in my terminal.


回答 11

我相信有一些方法可以将自制的python设置为默认值,但我认为解决问题的正确方法是不要弄乱系统python的路径:最好创建一个virtualenv,其中默认使用自制的python(通过使用virtualenv- -python选项)。使用类似的工具python_select几乎总是一个坏主意。

I believe there are means to make homebrew python default, but in my opinion the proper way to solve a problem is not to mess with system python paths: it is better to create a virtualenv in which homebrew python would be default (by using virtualenv –python option). Using tools like python_select is almost always a bad idea.


回答 12

改用pyenv在Python版本之间安装和切换。我已经使用rbenv多年了,但做的却是Ruby。在此之前,管理版本非常困难。

请查阅pyenv的github页面以获取安装说明。基本上是这样的:-使用自制软件安装pyenv。brew install pyenv -在shell启动脚本的末尾添加一个函数,以便pyenv可以做到。echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  • 使用pyenv安装所需的许多不同版本的Python。pyenv install 3.7.7
  • 将默认(全局)版本设置为刚安装的现代版本。pyenv global 3.7.7
  • 如果您从事的项目需要使用其他版本的python,请查看pyevn local。这将在项目的文件夹中创建一个文件,该文件指定python版本。Pyenv将使用该文件中的版本替换全局python版本。

Use pyenv instead to install and switch between versions of Python. I’ve been using rbenv for years which does the same thing, but for Ruby. Before that it was hell managing versions.

Consult pyenv’s github page for installation instructions. Basically it goes like this: – Install pyenv using homebrew. brew install pyenv – Add a function to the end of your shell startup script so pyenv can do it’s magic. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

  • Use pyenv to install however many different versions of Python you need. pyenv install 3.7.7.
  • Set the default (global) version to a modern version you just installed. pyenv global 3.7.7.
  • If you work on a project that needs to use a different version of python, look into pyevn local. This creates a file in your project’s folder that specifies the python version. Pyenv will look override the global python version with the version in that file.

回答 13

做就是了:

brew install python
brew link python

之后,将其添加到您的bashrc或bash_profile中:

alias python='/usr/local/bin/python2'

请享用!

Just do:

brew install python
brew link python

After doing that, add this to your bashrc or bash_profile:

alias python='/usr/local/bin/python2'

Enjoy!


回答 14

/usr/local/opt/python/libexec/bin显式添加到您的.bash_profile

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

之后,它应该可以正常工作。

Add the /usr/local/opt/python/libexec/bin explicitly to your .bash_profile:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

After that, it should work correctly.


回答 15

如果你是鱼壳

echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish

If you are fish shell

echo 'set -g fish_user_paths "/usr/local/opt/python/libexec/bin" $fish_user_paths' >> ~/.config/fish/config.fish

回答 16

您可以编辑/ etc / paths。这是我的:

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

然后为python版本添加一个符号链接。就我而言

$ cd /usr/local/bin
$ ln -s python3 python

瞧!

You can edit /etc/paths. Here is mine:

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

Then add a symlink for the python version. In my case

$ cd /usr/local/bin
$ ln -s python3 python

Voila!


回答 17

从High Sierra开始,您需要使用:

sudo chown -R $(whoami) $(brew --prefix)/*

这是因为/usr/local不再受宠

Since High Sierra, you need to use:

sudo chown -R $(whoami) $(brew --prefix)/*

This is because /usr/local can no longer be chowned


回答 18

完全不知道默认Python是什么意思。我认为用其他版本替换系统Python解释器是一种不好的做法。系统功能可能在某种程度上取决于系统Python和特定模块或特定Python版本。相反,在一个安全的不同的地方安装自定义的Python的安装,并根据需要,以调用调整$ PATH 查找,而不是Python的通过路径的寻找默认的Python。

No idea what you mean with default Python. I consider it bad practice to replace the system Python interpreter with a different version. System functionality may depend in some way on the system Python and specific modules or a specific Python version. Instead install your custom Python installations in a safe different place and adjust your $PATH as needed in order to call you Python through a path lookup instead of looking for the default Python.