如何使用 Homebrew 在 Mac 上安装 Python 2 和 3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18671253/
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
How can I use Homebrew to install both Python 2 and 3 on Mac?
提问by MostafaMV
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.
我需要能够在 Python 2 和 3 之间来回切换。我如何使用 Homebrew 做到这一点,因为我不想弄乱路径并遇到麻烦。现在我通过 Homebrew 安装了 2.7。
采纳答案by moliware
I would use pyenvYou can install it:
我会使用pyenv你可以安装它:
$ brew install pyenv
To enable pyenv in your Bash shell, you need to run:
要在 Bash shell 中启用 pyenv,您需要运行:
$ eval "$(pyenv init -)"
To do this automatically for Bash upon startup, add that line to your ~/.bash_profile
. 1
要在启动时为 Bash 自动执行此操作,请将该行添加到您的~/.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 并激活后,您可以安装不同版本的 python 并选择可以使用的版本。例子:
$ 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:
您可以使用以下命令在 python 版本之间切换:
$ pyenv global 3.3.1
Also you can set a python version for the current directory with:
您还可以使用以下命令为当前目录设置 python 版本:
$ pyenv local 3.5.2
You can check by running python --version
:
您可以通过运行来检查python --version
:
$ python --version
Python 3.5.2
1Homebrew 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 曾经指示您在安装 pyenv 时执行此操作,但该消息已被删除。对于 Zsh 和其他 shell,精确的步骤可能会有所不同。
回答by Fred Mitchell
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.
或者,您可能只需输入“python3”来运行最新版本的 python3.x,输入“python”或“python2”来运行最新安装的 2.x 版本。
回答by Lenin Raj Rajasekaran
You can have both versions installed at the same time.
您可以同时安装两个版本。
For Homebrew >=1.5.0:
对于 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.
自 2018 年 3 月 1 日起,该python
公式将升级到 Python 3.x,同时python@2
将专门为 Python 2.7 添加一个新公式。
See changes announcement hereor the final doc about using Homebrew for Python here.
请在此处查看更改公告或在此处查看有关使用 Homebrew for Python的最终文档。
For older Homebrew:
对于较旧的 Homebrew:
For Python 2.x:
对于 Python 2.x:
brew install python
For Python 3.x:
对于 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 时,请使用python
可执行文件。当您要使用版本 3 时,请使用python3
可执行文件。
回答by Vladimir Stazhilov
Okay, I was struggling with my brew installation of Python3, because I didn't have pip3
好的,我在用 brew 安装 Python3 时遇到了困难,因为我没有 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.
并从官方发行版安装了常规的 Python 3.6.2,然后我有了 pip3,所有组件都正常。
回答by Matt Schlobohm
Use asdf!
使用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 themI installed a lot of them
I learnt a lot of commandsThen I said, just one more version manager
Which I will write insteadSo, there came another version manager
asdf version manager- https://github.com/asdf-vm/asdfA 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
自卫队的歌谣
从前有一种编程语言,
它有很多版本,
所以人们为它编写了一个版本管理器,用于
在
不同的、旧的、新的项目版本之间切换。然后出现了更多的编程语言
所以出现了更多的版本管理器
和许多命令我安装了很多
我学到了很多命令然后我说,
我会写一个版本管理器来代替所以,出现了另一个版本管理器
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
https://github.com/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins
回答by Guilherme Garnier
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:
目前 Homebrew 为 Python 2 和 3 提供了两种不同的公式。brew install python
安装 python3,brew install python@2
安装 python2。Homebrew 文档中的更多详细信息:
https://docs.brew.sh/Homebrew-and-Python
https://docs.brew.sh/Homebrew-and-Python
If you currently have 2.x installed via Homebrew, Homebrew will give you a message such as:
如果您当前通过 Homebrew 安装了 2.x,Homebrew 会给您一条消息,例如:
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.
查看安装了哪些版本的 Python 2.x 和 3.x。
回答by rustyMagnet
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
.
我以为我有相同的要求 - 在 Python 版本之间移动 - 但我通过从源代码构建而不是使用homebrew
.
git clone https://git.<theThingYouWantToInstall>
git clone https://git.<theThingYouWantToInstall>
Depending on the repo, check if there is MAKE file already setup for this option.
根据 repo,检查是否已经为此选项设置了 MAKE 文件。
回答by ishandutta2007
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:
有两种方法可以同时使用,但今天最简单的解决方案是使用pyenv。pyenv 允许在版本之间轻松切换。这是我所做的设置:
STEP1:
第1步:
Remove all pythons from your mac
从你的 mac 中删除所有 python
brew uninstall --ignore-dependencies --force python
sudo rm -rf ~/miniconda3/
sudo rm -rf ~/.conda/
Remove the following from ~/.bash_profile
从以下内容中删除 ~/.bash_profile
export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"
export PATH="/Users/ishandutta2007/miniconda3/bin:$PATH"
and also the following from ~/.bashrc
还有以下来自 ~/.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"
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:
第2步:
Install pyenv and the python versions you need
安装 pyenv 和你需要的 python 版本
brew update
brew install pyenv
pyenv install 2.7
pyenv install 3.7.0
STEP3:
第3步:
add pyenv init
to bash_profile
or bashrc
添加pyenv init
到bash_profile
或bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
STEP4:
第4步:
Check what got installed
检查安装了什么
pyenv versions
system (set by /Users/ishandutta2007/.pyenv/version)
2.7
3.7.0
系统(由 /Users/ishandutta2007/.pyenv/version 设置)
2.7
3.7.0
STEP5:
第5步:
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
回答by Christian Vielma
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.
我可以去https://www.python.org/downloads/mac-osx/下载最新的 python。它安装在我的系统中的侧面电流 python 中。