简体   繁体   English

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

[英]How can I use Homebrew to install both Python 2 and 3 on Mac?

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. 我需要能够在Python 2和3之间来回切换。我如何使用Homebrew做到这一点,因为我不想弄乱路径并遇到麻烦。 Right now I have 2.7 installed through Homebrew. 现在我通过Homebrew安装了2.7。

I would use pyenv You 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 . 要在启动时自动为Bash执行此操作,请将该行添加到~/.bash_profile 1 1

Usage: 用法:

Once you have installed pyenv and activated it, you can install different versions of python and choose which one you can use. 一旦安装了pyenv并激活它,就可以安装不同版本的python并选择可以使用的版本。 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: 您可以使用以下命令在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

1 Homebrew used to instruct you to do this upon installation of pyenv, but the message was removed . 1 Homebrew用于指示您在安装pyenv时执行此操作,但消息已被删除 For Zsh and other shells, the precise steps may be different. 对于Zsh和其他炮弹,精确的步骤可能会有所不同。

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.7添加一个新的python@2公式。

See changes announcement here or 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. 如果要使用版本2,请使用python可执行文件。 When you want to use version 3, use the python3 executable. 如果要使用版本3,请使用python3可执行文件。

Currently Homebrew provides two different formulas for Python 2 and 3. brew install python installs python3, and brew install python@2 installs python2. 目前Homebrew为Python 2和3提供了两种不同的公式brew install python installs python3, brew install python@2 installs python2。 More details in Homebrew docs: 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.

或者,您可以输入“python3”来运行最新版本的python3.x和“python”或“python2”来运行最新安装的2.x版本。

There are ways to use both , but the simplest solution today is to use pyenv . 有两种方法可以使用,但今天最简单的解决方案是使用pyenv pyenv allows easy switching between versions. pyenv允许在版本之间轻松切换。 Here is what I did to set up: 以下是我设置的内容:

STEP1: 步骤1:

Remove all pythons from your mac 从mac中删除所有蟒蛇

 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"

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" 

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 initbash_profilebashrc

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) system(由/Users/ishandutta2007/.pyenv/version设置)

    2.7 2.7

    3.7.0 3.7.0

STEP5: 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

Use asdf ! 使用asdf

Ballad of asdf 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 manager - https://github.com/asdf-vm/asdf asdf版本管理器 - https://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/asdf-vm/asdf
https://github.com/tuvistavie/asdf-python https://github.com/tuvistavie/asdf-python
https://github.com/asdf-vm/asdf-plugins https://github.com/asdf-vm/asdf-plugins

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来实现我只需要Python3.6。

git clone https://git.<theThingYouWantToInstall>

Depending on the repo, check if there is MAKE file already setup for this option. 根据回购,检查是否已为此选项设置了MAKE文件。

I was able to just go to https://www.python.org/downloads/mac-osx/ and download the latest python. 我能够访问https://www.python.org/downloads/mac-osx/并下载最新的python。 It installed along side current python in my system. 它安装在我系统中的当前python端。

Okay, I was struggling with my brew installation of Python3, because I didn't have pip3 好吧,我在使用Python3的brew安装时遇到了困难,因为我没有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,所有组件都没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM