如何链接 home brew python 版本并将其设置为默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19340871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:30:57  来源:igfitidea点击:

How to link home brew python version and set it as default

pythonmacoshomebrew

提问by add-semi-colons

I just switched from MacPorts to HomeBrew. After installing all the required XCode versions and other software I tried installing python using homebrew: I think it successfully installed, but when I do which pythonit still shows me 2.7.3 which I think is the version shipped with Mountain Lion.

我刚刚从 MacPorts 切换到 HomeBrew。在安装了所有必需的 XCode 版本和其他软件后,我尝试使用自制软件安装 python:我认为它已成功安装,但是当我这样做时,which python它仍然显示 2.7.3,我认为这是 Mountain Lion 附带的版本。

which python
/usr/local/bin/python

python --version
Python 2.7.3

so I tried to install again

所以我尝试再次安装

brew install python --framework --universal
Warning: python-2.7.5 already installed, it's just not linked

But it says python 2.7.5 already install and not linked, I tried to do brew link python

但是它说python 2.7.5已经安装并且没有链接,我试着做 brew link python

That led me to following message so, I have no idea what I should be doing:

这导致我遵循消息,所以我不知道我应该做什么:

Linking /usr/local/Cellar/python/2.7.5... Warning: Could not link python. Unlinking...

正在链接 /usr/local/Cellar/python/2.7.5... 警告:无法链接 python。正在取消关联...

Error: Could not symlink file: /usr/local/Cellar/python/2.7.5/bin/smtpd2.py
Target /usr/local/bin/smtpd2.py already exists. You may need to delete it.
To force the link and overwrite all other conflicting files, do:
  brew link --overwrite formula_name

To list all files that would be deleted:
  brew link --overwrite --dry-run formula_name

回答by Vikram Dutt

This answer is for upgrading Python 2.7.10 to Python 2.7.11 on Mac OS X El Capitan . On Terminal type:

此答案用于在 Mac OS X El Capitan 上将 Python 2.7.10 升级到 Python 2.7.11。在终端类型上:

brew unlink python

After that type on Terminal

在终端上键入之后

brew install python

回答by Hercules

In the Terminal, type:

在终端中,输入:

brew link python

回答by Rolf Schulze

If you used

如果你使用

brew install python

before 'unlink' you got

在“取消链接”之前你得到

brew info python
/usr/local/Cellar/python/2.7.11

python -V
Python 2.7.10

so do

也是

brew unlink python && brew link python

and open a new terminal shell

并打开一个新的终端外壳

python -V
Python 2.7.11

回答by slapash

I think you have to be precise with which version you want to link with the command brew link pythonlike:

我认为您必须准确地使用要与以下命令链接的版本brew link python

brew link python 3

It will give you an error like that:

它会给你一个这样的错误:

Linking /usr/local/Cellar/python3/3.5.2... 
Error: Could not symlink bin/2to3-3.5
Target /usr/local/bin/2to3-3.5
already exists. 
Linking /usr/local/Cellar/python3/3.5.2... 
Error: Could not symlink bin/2to3-3.5
Target /usr/local/bin/2to3-3.5
already exists. 

You may want to remove it:

您可能想要删除它:

rm '/usr/local/bin/2to3-3.5'

To force the link and overwrite all conflicting files:

要强制链接并覆盖所有冲突文件:

brew link --overwrite python3

To list all files that would be deleted:

列出所有将被删除的文件:

brew link --overwrite --dry-run python3

but you have to copy/paste the command to force the link which is:

但您必须复制/粘贴命令以强制链接:

brew link --overwrite python3

I think that you must have the version (the newer) installed.

我认为您必须安装该版本(较新的)。

回答by Eric Guo

brew switch to python3 by default, so if you want to still set python2 as default bin python, running:

brew 默认切换到 python3,所以如果你仍然想将 python2 设置为默认 bin python,运行:

brew unlink python && brew link python2 --force

回答by lenooh

After installing python3 with brew install python3I was getting the error:

安装 python3 后,brew install python3我收到错误:

Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/Frameworks
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

After typing brew link python3the error was:

输入后brew link python3报错是:

Linking /usr/local/Cellar/python/3.6.4_3... Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

To solve the problem:

解决问题:

sudo mkdir -p /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/*
brew link python3

After this, I could open python3 by typing python3

在此之后,我可以通过键入打开 python3 python3

(From https://github.com/Homebrew/homebrew-core/issues/20985)

(来自https://github.com/Homebrew/homebrew-core/issues/20985

回答by Chaim Eliyah

On OS X High Sierra, I had to do this:

在 OS X High Sierra 上,我必须这样做:

sudo install -d -o $(whoami) -g admin /usr/local/Frameworks
brew uninstall --ignore-dependencies python
brew install python
python --version # should work, returns 2.7, which is a Python thing (it's weird, but ok)

credit to https://gist.github.com/irazasyed/7732946#gistcomment-2235469

归功于https://gist.github.com/irazasyed/7732946#gistcomment-2235469

I think it's better than recursively chowning the /usr/local dir, but that may solve other problems ;)

我认为这比递归 chowning /usr/local 目录要好,但这可能会解决其他问题;)

回答by user7842596

I use these commands to solve it.

我使用这些命令来解决它。

mkdir /usr/local/lib
mkdir /usr/local/lib/pkgconfig
brew link python

回答by Yolanda

The problem with me is that I have so many different versions of python, so it opens up a different python3.7 even after I did brew link. I did the following additional steps to make it default after linking

我的问题是我有很多不同版本的 python,所以即使在我做了 brew link 之后它也会打开一个不同的 python3.7。我做了以下额外的步骤,使其默认联后

First, open up the document setting up the path of python

首先打开设置python路径的文档

 nano ~/.bash_profile

Then something like this shows up:

然后出现这样的事情:

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

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

The thing here is that my Python for brew framework is not in the LibraryFolder!! So I changed the framework for python 3.7, which looks like follows in my system

这里的问题是我的用于 brew 框架的 Python 不在文件夹中!!所以我改变了python 3.7的框架,在我的系统中看起来如下

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/usr/local/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH

Change and save the file. Restartthe computer, and typing in python3.7, I get the python I installed for brew.

更改并保存文件。重新启动计算机,然后输入python3.7,我得到了我为 brew 安装的 python。

Not sure if my case is applicable to everyone, but worth a try. Not sure if the framework path is the same for everyone, please made sure before trying out.

不确定我的情况是否适用于所有人,但值得一试。不确定每个人的框架路径是否相同,请在尝试前确定。

回答by Ankit Kumar Rajpoot

You can follow these steps.

您可以按照以下步骤操作。

$ python3 --version  
$ brew unlink python@2
$ brew link python3   
$ python3 --version   

All steps

所有步骤