问题:如何使用自制软件在macOS中安装Python 3的早期版本?

如何使用brew在macOS中安装Python 3的早期版本?

通过该命令,brew install python我获得了最新版本的Python 3(当前为v3.7.0),但我想要的是最新版本的Python 3.6(当前为3.6.5)。

我已经阅读了另一个pyenv可以帮助处理不同python安装的软件包,但是此解决方案不适合我。

How can I install a previous version of Python 3 in macOS using brew?

With the command brew install python I got the latest version of Python 3 (currently v3.7.0), but I want the last version of Python 3.6 (currently 3.6.5).

I have read about another package pyenv that can assist in handle different python installation, but this solution is not suitable for me.


回答 0

简短答案

要进行Python 3.6.5的全新安装,请使用:

brew unlink python # ONLY if you have installed (with brew) another version of python 3
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

如果您希望恢复以前安装的版本,则:

brew info python           # To see what you have previously installed
brew switch python 3.x.x_x # Ex. 3.6.5_1

长答案

使用Homebrew安装Python的公式有两个:python@2python
第一个用于Python 2,第二个用于Python 3。

注意:您可以在网上找到过时的答案,这些答案被python3称为安装Python版本3的公式名称python

默认情况下,使用这些公式,您可以安装对应的Python主版本的最新版本。因此,您不能直接安装3.6的次要版本。

使用brew,您可以使用公式的地址安装软件包,例如在git存储库中。

brew install https://the/address/to/the/formula/FORMULA_NAME.rb

或专门针对Python 3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/COMMIT_IDENTIFIER/Formula/python.rb

您必须指定的地址是所需版本的公式的最后提交地址(python.rb)。您可以通过查看homebrew-core / Formula / python.rb的历史记录来找到commint标识符

https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb

Python> 3.6.5

在上面的链接中,您不会找到3.6.5以上版本的Python的公式。该(官方)存储库的维护者发布Python 3.7之后,他们仅提交对Python 3.7配方的更新。

如上所述,使用自制软件时,您只有Python 2(python @ 2)和Python 3(python),而Python 3.6没有明确的公式

尽管那些次要更新在大多数情况下和对于大多数用户而言都是无关紧要的,但我将搜索是否有人对3.6做过明确的公式。

Short Answer

To make a clean install of Python 3.6.5 use:

brew unlink python # ONLY if you have installed (with brew) another version of python 3
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

If you prefer to recover a previously installed version, then:

brew info python           # To see what you have previously installed
brew switch python 3.x.x_x # Ex. 3.6.5_1

Long Answer

There are two formulas for installing Python with Homebrew: python@2 and python.
The first is for Python 2 and the second for Python 3.

Note: You can find outdated answers on the web where it is mentioned python3 as the formula name for installing Python version 3. Now it’s just python!

By default, with these formulas you can install the latest version of the corresponding major version of Python. So, you cannot directly install a minor version like 3.6.

Solution

With brew, you can install a package using the address of the formula, for example in a git repository.

brew install https://the/address/to/the/formula/FORMULA_NAME.rb

Or specifically for Python 3

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/COMMIT_IDENTIFIER/Formula/python.rb

The address you must specify is the address to the last commit of the formula (python.rb) for the desired version. You can find the commint identifier by looking at the history for homebrew-core/Formula/python.rb

https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb

Python > 3.6.5

In the link above you will not find a formula for a version of Python above 3.6.5. After the maintainers of that (official) repository released Python 3.7, they only submit updates to the recipe of Python 3.7.

As explained above, with homebrew you have only Python 2 (python@2) and Python 3 (python), there is no explicit formula for Python 3.6.

Although those minor updates are mostly irrelevant in most cases and for most users, I will search if someone has done an explicit formula for 3.6.


回答 1

作为更新,当做

brew unlink python # If you have installed (with brew) another version of python
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

你可能会遇到

Error: python contains a recursive dependency on itself:
  python depends on sphinx-doc
  sphinx-doc depends on python

要绕过它,请将--ignore-dependencies参数添加到brew install中。

brew unlink python # If you have installed (with brew) another version of python
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

As an update, when doing

brew unlink python # If you have installed (with brew) another version of python
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

You may encounter

Error: python contains a recursive dependency on itself:
  python depends on sphinx-doc
  sphinx-doc depends on python

To bypass it, add the --ignore-dependencies argument to brew install.

brew unlink python # If you have installed (with brew) another version of python
brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

回答 2

我所做的是首先安装python 3.7

brew install python3
brew unlink python

然后我使用上面的链接安装了python 3.6.5

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies

之后我跑了brew link --overwrite python。现在,我在系统中拥有所有的python来创建虚拟环境。

mian@tdowrick2~ $ python --version
Python 2.7.10
mian@tdowrick2~ $ python3.7 --version
Python 3.7.1
mian@tdowrick2~ $ python3.6 --version
Python 3.6.5

创建Python 3.7虚拟环境。

mian@tdowrick2~ $ virtualenv -p python3.7 env
Already using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/mian/env/bin/python3.7
Also creating executable in /Users/mian/env/bin/python
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.7.1
(env) mian@tdowrick2~ $ deactivate

创建Python 3.6虚拟环境

mian@tdowrick2~ $ virtualenv -p python3.6 env
Running virtualenv with interpreter /usr/local/bin/python3.6
Using base prefix '/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/mian/env/bin/python3.6
Not overwriting existing python script /Users/mian/env/bin/python (you must use /Users/mian/env/bin/python3.6)
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.6.5
(env) mian@tdowrick2~ $ 

What I did was first I installed python 3.7

brew install python3
brew unlink python

then I installed python 3.6.5 using above link

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies

After that I ran brew link --overwrite python. Now I have all pythons in the system to create the virtual environments.

mian@tdowrick2~ $ python --version
Python 2.7.10
mian@tdowrick2~ $ python3.7 --version
Python 3.7.1
mian@tdowrick2~ $ python3.6 --version
Python 3.6.5

To create Python 3.7 virtual environment.

mian@tdowrick2~ $ virtualenv -p python3.7 env
Already using interpreter /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.7'
New python executable in /Users/mian/env/bin/python3.7
Also creating executable in /Users/mian/env/bin/python
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.7.1
(env) mian@tdowrick2~ $ deactivate

To create Python 3.6 virtual environment

mian@tdowrick2~ $ virtualenv -p python3.6 env
Running virtualenv with interpreter /usr/local/bin/python3.6
Using base prefix '/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/mian/env/bin/python3.6
Not overwriting existing python script /Users/mian/env/bin/python (you must use /Users/mian/env/bin/python3.6)
Installing setuptools, pip, wheel...
done.
mian@tdowrick2~ $ source env/bin/activate
(env) mian@tdowrick2~ $ python --version
Python 3.6.5
(env) mian@tdowrick2~ $ 

回答 3

我尝试了以上所有答案来安装Python 3.4.4。python的安装有效,但不会安装PIP,并且我无能为力。我使用的是Mac OSX Mojave,这会导致zlib,openssl出现一些问题。

不该做什么:

  • 尝试避免对公式PythonPython3给定的早期版本使用Homebrew
  • 不要尝试编译Python

解:

  1. 下载macOS 64位安装程序macOS 64位/ 32位安装程序https : //www.python.org/downloads/release/python-365/
  2. 在前面的步骤,它会下载的Python 3.6.5,例如如果你想下载的Python 3.4.4,在上面的网址取代Python-365Python-344
  3. 下载单击您下载的文件,GUI安装程序将打开
  4. 如果您下载了python-365,则在安装后要启动此版本的python,您将在终端python365中键入,对pip相同,即为pip365

ps:您不必在系统上卸载其他版本的Python。


编辑:


我发现了一种更好的解决方案,可在MacOSX,Windows,Linux等系统上运行。

  1. 是否已经安装python都没关系。
  2. 下载Anaconda
  3. 安装后,在终端中输入: conda init
  4. 在终端中,使用任何 python版本创建虚拟环境,例如,我选择了3.4.4:conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
  5. 然后,在终端中,您可以使用以下命令检查已创建的所有虚拟环境: conda info --envs
  6. 然后,在终端中,使用以下命令激活您选择的虚拟环境: conda activate [The name of your virtual environment that was shown with the command at step 5]

I tried all the answers above to install Python 3.4.4. The installation of python worked, but PIP would not be installed and nothing I could do to make it work. I was using Mac OSX Mojave, which cause some issues with zlib, openssl.

What not to do:

  • Try to avoid using Homebrew for previous version given by the formula Python or Python3.
  • Do not try to compile Python

Solution:

  1. Download the macOS 64-bit installer or macOS 64-bit/32-bit installer: https://www.python.org/downloads/release/python-365/
  2. In previous step, it will download Python 3.6.5, if for example, you want to download Python 3.4.4, replace in the url above python-365 by python-344
  3. Download click on the file you downloaded a GUI installer will open
  4. If you downloaded python-365, after installation, to launch this version of python, you will type in your terminal python365, same thing for pip, it will be pip365

p.s: You don’t have to uninstall your other version of Python on your system.


Edit:


I found a much much much better solution that work on MacOSX, Windows, Linux, etc.

  1. It doesn’t matter if you have already python installed or not.
  2. Download Anaconda
  3. Once installed, in terminal type: conda init
  4. In terminal,create virtual environment with any python version, for example, I picked 3.4.4: conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
  5. Then, in terminal, you can check all the virtual environment you ahave created with the command: conda info --envs
  6. Then, in terminal, activate the virtual environment of your choice with: conda activate [The name of your virtual environment that was shown with the command at step 5]

回答 4

我已经尝试了所有方法,但无法使其正常工作。最终,我使用了pyenv它,并且像魅力一样直接工作。

因此,homebrew安装完毕后,只需执行以下操作:

brew install pyenv
pyenv install 3.6.5

管理virtualenvs:

brew install pyenv-virtualenv
pyenv virtualenv 3.6.5 env_name

有关更多信息,请参见pyenvpyenv-virtualenv

编辑(2019/03/19)

我发现使用pyenv-installer比自制软件更容易安装pyenv和pyenv-virtualenv direclty:

curl https://pyenv.run | bash

要全局管理python版本:

pyenv global 3.6.5

或本地给定目录中:

pyenv local 3.6.5

I have tried everything but could not make it work. Finally I have used pyenv and it worked directly like a charm.

So having homebrew installed, juste do:

brew install pyenv
pyenv install 3.6.5

to manage virtualenvs:

brew install pyenv-virtualenv
pyenv virtualenv 3.6.5 env_name

See pyenv and pyenv-virtualenv for more info.

EDIT (2019/03/19)

I have found using the pyenv-installer easier than homebrew to install pyenv and pyenv-virtualenv direclty:

curl https://pyenv.run | bash

To manage python version, either globally:

pyenv global 3.6.5

or locally in a given directory:

pyenv local 3.6.5

回答 5

万一有人遇到如下点子问题

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

根本原因是openssl 1.1不再支持python 3.6。所以你需要安装旧版本的openssl 1.0

这是解决方案:

brew uninstall --ignore-dependencies openssl
brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

In case anyone face pip issue like below

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

The root cause is openssl 1.1 doesn’t support python 3.6 anymore. So you need to install old version openssl 1.0

here is the solution:

brew uninstall --ignore-dependencies openssl
brew install https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb

回答 6

要解决此问题homebrew,您可以临时回溯日期homebrew-core并设置HOMEBREW_NO_AUTO_UPDATE变量以将其保留在适当的位置:

cd `brew --repo homebrew/core`
git checkout f2a764ef944b1080be64bd88dca9a1d80130c558
export HOMEBREW_NO_AUTO_UPDATE=1
brew install python

我不建议永久回溯自制内核的日期,因为您会错过安全补丁,但是它对于测试很有用。

您还可以使用以下brew extract命令将旧版本的自制程序公式提取到自己的水龙头(tap_owner / tap_name)中:

brew extract python tap_owner/tap_name --version=3.6.5

To solve this with homebrew, you can temporarily backdate homebrew-core and set the HOMEBREW_NO_AUTO_UPDATE variable to hold it in place:

cd `brew --repo homebrew/core`
git checkout f2a764ef944b1080be64bd88dca9a1d80130c558
export HOMEBREW_NO_AUTO_UPDATE=1
brew install python

I don’t recommend permanently backdating homebrew-core, as you will miss out on security patches, but it is useful for testing purposes.

You can also extract old versions of homebrew formulae into your own tap (tap_owner/tap_name) using the brew extract command:

brew extract python tap_owner/tap_name --version=3.6.5

回答 7

对我而言,最简单的方法是安装Anaconda:https : //docs.anaconda.com/anaconda/install/

在这里,我可以根据需要创建任意数量的具有不同Python版本的环境,然后单击鼠标即可在它们之间切换。再简单不过了。

要安装不同的Python版本,只需按照以下说明进行操作即可:https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html

在2分钟内完成了具有其他Python版本的新开发环境。将来我可以轻松地来回切换。

The easiest way for me was to install Anaconda: https://docs.anaconda.com/anaconda/install/

There I can create as many environments with different Python versions as I want and switch between them with a mouse click. It could not be easier.

To install different Python versions just follow these instructions https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-python.html

A new development environment with a different Python version was done within 2 minutes. And in the future I can easily switch back and forth.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。