如何使用Homebrew在Mac上安装Python 2和3?

问题:如何使用Homebrew在Mac上安装Python 2和3?

我需要能够在Python 2和3之间来回切换。如何使用Homebrew做到这一点,因为我不想弄乱路径并遇到麻烦。现在,我通过Homebrew安装了2.7。

I need to be able to switch back and forth between Python 2 and 3. How do I do that using Homebrew as I don’t want to mess with path and get into trouble. Right now I have 2.7 installed through Homebrew.


回答 0

我会用pyenv您可以安装它:

$ brew install pyenv

要在您的Bash shell中启用pyenv,您需要运行:

$ eval "$(pyenv init -)"

要在启动时自动为Bash执行此操作,请将该行添加到您的中~/.bash_profile1个

用法:

安装并激活pyenv后,您可以安装不同版本的python并选择可以使用的版本。例:

$ pyenv install 2.7.5

您可以检查已安装的版本:

$ pyenv versions

您可以使用以下命令在python版本之间进行切换:

$ pyenv global 3.3.1

您还可以使用以下命令为当前目录设置python版本:

$ pyenv local 3.5.2

您可以通过运行python --version以下命令进行检查:

$ python --version
Python 3.5.2

1 Homebrew曾经指示您在安装pyenv时执行此操作,但是该消息已删除。对于Zsh和其他Shell,精确步骤可能有所不同。

I would use pyenv You can install it:

$ brew install pyenv

To enable pyenv in your Bash shell, you need to run:

$ eval "$(pyenv init -)"

To do this automatically for Bash upon startup, add that line to your ~/.bash_profile. 1

Usage:

Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. Example:

$ pyenv install 2.7.5

You can check the versions you have installed with:

$ pyenv versions

And you can switch between python versions with the command:

$ pyenv global 3.3.1

Also you can set a python version for the current directory with:

$ pyenv local 3.5.2

You can check by running python --version:

$ python --version
Python 3.5.2

1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed. For Zsh and other shells, the precise steps may be different.


回答 1

您可以同时安装两个版本。

对于Homebrew> = 1.5.0:

自2018年3月1日起,该python公式将升级到Python 3.x,同时python@2专门为Python 2.7添加一个新公式。

在此处查看更改公告,或在此处查看有关将Homebrew用于Python最终文档

对于较早的自制软件:

对于Python 2.x:

brew install python

对于Python 3.x:

brew install python3

现在,您将在计算机中同时安装两个版本。当您要使用版本2时,请使用python可执行文件。当您要使用版本3时,请使用python3可执行文件。

You can have both versions installed at the same time.

For Homebrew >=1.5.0:

Since 1st March 2018 the python formula will be upgraded to Python 3.x, while a new python@2 formula will be added for Python 2.7, specifically.

See changes announcement here or the final doc about using Homebrew for Python here.

For older Homebrew:

For Python 2.x:

brew install python

For Python 3.x:

brew install python3

Now, you will have both the versions installed in your machine. When you want to use version 2, use the python executable. When you want to use version 3, use the python3 executable.


回答 2

当前,Homebrew为Python 2和3提供了两个不同的公式。brew install python安装python3,然后brew install python@2安装python2。Homebrew文档中的更多详细信息:

https://docs.brew.sh/Homebrew-and-Python

如果您当前通过Homebrew安装了2.x,Homebrew会给您以下消息:

Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`

如果您运行:

brew upgrade python

您应该能够:

python --version

python3 --version

查看安装了什么版本的Python 2.x和3.x。

Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python@2 installs python2. More details in Homebrew docs:

https://docs.brew.sh/Homebrew-and-Python

If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:

Error: python 2.7.14 is already installed
To upgrade to 3.6.5, run `brew upgrade python`

If you run:

brew upgrade python

you should be able to do:

python --version

and

python3 --version

To see what versions of Python 2.x and 3.x installed.


回答 3

另外,您可能只需输入“ python3”来运行最新版本的python3.x,然后输入“ python”或“ python2”来运行最新安装的2.x版本。

Alternatively, you probably can just enter “python3” to run your most current version of python3.x and “python” or “python2” to run the latest installed 2.x version.


回答 4

有两种方法都可以使用,但是今天最简单的解决方案是使用pyenv。pyenv允许在版本之间轻松切换。这是我要做的设置:

第1步:

从Mac移除所有python

 brew uninstall --ignore-dependencies --force python
 sudo rm -rf ~/miniconda3/
 sudo rm -rf ~/.conda/

从以下内容删除 ~/.bash_profile

export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"

还有以下内容 ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"

第2步

安装pyenv和所需的python版本

brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0

第三步

添加pyenv initbash_profilebashrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

第4步

检查安装了什么

pyenv versions
  • 系统(由/Users/ishandutta2007/.pyenv/version设置)

    2.7

    3.7.0

第5步:

选择一个默认值

pyenv global 3.7.0

当项目需要较旧的版本时,只需转到其根文件夹并运行

pyenv local 2.7

There are ways to use both , but the simplest solution today is to use pyenv. pyenv allows easy switching between versions. Here is what I did to set up:

STEP1:

Remove all pythons from your mac

 brew uninstall --ignore-dependencies --force python
 sudo rm -rf ~/miniconda3/
 sudo rm -rf ~/.conda/

Remove the following from ~/.bash_profile

export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"

and also the following from ~/.bashrc

export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=/usr/local/lib/python2.7/site-packages/google:$PYTHONPATH
alias python="/usr/bin/python"

STEP2:

Install pyenv and the python versions you need

brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0

STEP3:

add pyenv init to bash_profile or bashrc

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

STEP4:

Check what got installed

pyenv versions
  • system (set by /Users/ishandutta2007/.pyenv/version)

    2.7

    3.7.0

STEP5:

Choose a default

pyenv global 3.7.0

When a project needs older version, just go its root folder and run

pyenv local 2.7

回答 5

使用asdf

asdf的民谣

曾经有一种编程语言,
它有许多版本,
所以人们为它编写了一个版本管理器,以便
在项目的版本之间进行切换
,包括新旧版本。

然后出现了更多的编程语言,
所以出现了更多的版本管理器
以及许多针对他们的命令

我安装了很多,
我学到了很多命令

然后我说,
我将再写一个版本管理器

因此,出现了另一个版本管理器
asdf版本管理-https : //github.com/asdf-vm/asdf

版本管理器可扩展
,任何人都可以为其创建插件
以支持自己喜欢的语言,而
无需再安装更多版本管理器
或学习更多命令

https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins

Use asdf !

Ballad of asdf

Once upon a time there was a programming language
There were many versions of it
So people wrote a version manager for it
To switch between versions for projects
Different, old, new.

Then there came more programming languages
So there came more version managers
And many commands for them

I installed a lot of them
I learnt a lot of commands

Then I said, just one more version manager
Which I will write instead

So, there came another version manager
asdf version managerhttps://github.com/asdf-vm/asdf

A version manager so extendable
for which anyone can create a plugin
To support their favourite language
No more installing more version managers
Or learning more commands

https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins


回答 6

我以为我有相同的要求-在Python版本之间移动-但是通过从源代码构建而不是使用Python3.6实现了我所需的一切homebrew

git clone https://git.<theThingYouWantToInstall>

根据存储库,检查是否已经为该选项设置了MAKE文件。

I thought I had the same requirement – to move between Python versions – but I achieved all I needed with only Python3.6 by building from source instead of using homebrew.

git clone https://git.<theThingYouWantToInstall>

Depending on the repo, check if there is MAKE file already setup for this option.


回答 7

我能够去https://www.python.org/downloads/mac-osx/下载最新的python。它与当前的python一起安装在我的系统中。

I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. It installed along side current python in my system.


回答 8

好的,我正在努力安装Python3,因为我没有pip3

sudo pip3 command not found

所以我做到了

brew uninstall --force --ignore-dependencies python3

并从官方发行版中安装了常规的Python 3.6.2,然后安装了pip3,并且所有组件都正常。

Okay, I was struggling with my brew installation of Python3, because I didn’t have pip3

sudo pip3 command not found

and so I did

brew uninstall --force --ignore-dependencies python3

and installed the regular Python 3.6.2 from official distribution and then I had pip3 and all components were ok.