如何使用自制软件在 macOS 中安装以前版本的 Python 3?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51125013/
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 install a previous version of Python 3 in macOS using homebrew?
提问by ePi272314
How can I install a previous version of Python 3 in macOS using brew?
如何使用 brew 在 macOS 中安装以前版本的 Python 3?
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).
通过该命令,brew install python
我获得了 Python 3 的最新版本(当前为 v3.7.0),但我想要 Python 3.6 的最新版本(当前为 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.
我已经阅读了另一个pyenv
可以帮助处理不同 python 安装的包,但是这个解决方案不适合我。
回答by ePi272314
Short Answer
简答
To make a clean install of Python 3.6.5 use:
要全新安装 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
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.
使用 Homebrew 安装 Python 有两个公式:python@2
和python
.
第一个用于 Python 2,第二个用于 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
!
注意:您可以在网上找到过时的答案,其中提到它python3
作为安装 Python 版本 3 的公式名称。现在它只是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.
默认情况下,您可以使用这些公式安装相应主要 Python 版本的最新版本。所以,你不能直接安装像 3.6 这样的次要版本。
Solution
解决方案
With brew
, you can install a package using the address of the formula, for example in a git repository.
使用brew
,您可以使用公式的地址安装包,例如在 git 存储库中。
brew install https://the/address/to/the/formula/FORMULA_NAME.rb
Or specifically for Python 3
或者专门针对 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
您必须指定的地址是所需版本的公式 (python.rb) 最后一次提交的地址。您可以通过查看 homebrew-core/Formula/python.rb 的历史记录来找到 commint 标识符
https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb
https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb
Python > 3.6.5
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.
在上面的链接中,您找不到 3.6.5 以上 Python 版本的公式。在该(官方)存储库的维护者发布 Python 3.7 之后,他们只提交对 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.
如上所述,使用自制软件,您只有 Python 2 (python@2) 和 Python 3 (python),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.
尽管在大多数情况下和对大多数用户而言,这些小更新大多无关紧要,但我会搜索是否有人为 3.6 制定了明确的公式。
回答by amiabl
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.
要绕过它,请将--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
回答by Mian Asbat Ahmad
What I did was first I installed python 3.7
我所做的首先是我安装了 python 3.7
brew install python3
brew unlink python
then I installed python 3.6.5 using above link
然后我使用上面的链接安装了 python 3.6.5
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.
之后我跑了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
To create Python 3.7 virtual environment.
创建 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
To create Python 3.6 virtual environment
创建 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~ $
回答by vincedjango
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.
我尝试了上面的所有答案来安装 Python 3.4.4。python 的安装有效,但 PIP 不会安装,我无能为力。我使用的是 Mac OSX Mojave,这会导致 zlib、openssl 出现一些问题。
What not to do:
什么不能做:
- Try to avoid using Homebrew for previous version given by the formula Pythonor Python3.
- Do not try to compile Python
- 尽量避免将 Homebrew 用于由公式Python或Python3给出的先前版本。
- 不要尝试编译Python
Solution:
解决方案:
- Download the macOS 64-bit installeror macOS 64-bit/32-bit installer: https://www.python.org/downloads/release/python-365/
- 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-365by python-344
- Download click on the file you downloaded a GUI installer will open
- 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
- 下载macOS 64 位安装程序或macOS 64 位/32 位安装程序:https: //www.python.org/downloads/release/python-365/
- 在上一步中,它将下载Python 3.6.5,例如,如果您要下载Python 3.4.4,请将python-365上面的 url 替换为python-344
- 下载 单击您下载的文件,GUI 安装程序将打开
- 如果你下载了python-365,安装后,要启动这个版本的 python,你将在你的终端输入 python365,同样的 pip ,它将是pip365
p.s: You don't have to uninstall your other version of Python on your system.
ps:您不必在系统上卸载其他版本的 Python。
Edit:
编辑:
I found a much much much better solution that work on MacOSX, Windows, Linux, etc.
我找到了一个更好的解决方案,适用于 MacOSX、Windows、Linux 等。
- It doesn't matter if you have already python installed or not.
- Download Anaconda
- Once installed, in terminal type:
conda init
- In terminal,create virtual environment with anypython version, for example, I picked 3.4.4:
conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
- Then, in terminal, you can check all the virtual environment you ahave created with the command:
conda info --envs
- 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]
- 你是否已经安装了 python 并不重要。
- 下载蟒蛇
- 安装后,在终端类型中:
conda init
- 在终端中,使用任何python版本创建虚拟环境,例如,我选择了3.4.4:
conda create -n [NameOfYour VirtualEnvironment] python=3.4.4
- 然后,在终端中,您可以使用以下命令检查您创建的所有虚拟环境:
conda info --envs
- 然后,在终端中,使用以下命令激活您选择的虚拟环境:
conda activate [The name of your virtual environment that was shown with the command at step 5]
回答by ClementWalter
I have tried everything but could not make it work. Finally I have used pyenv
and it worked directly like a charm.
我已经尝试了一切,但无法使其正常工作。最后我使用了pyenv
它,它像魅力一样直接工作。
So having homebrew
installed, juste do:
因此,homebrew
安装后,请执行以下操作:
brew install pyenv
pyenv install 3.6.5
to manage virtualenvs:
管理虚拟环境:
brew install pyenv-virtualenv
pyenv virtualenv 3.6.5 env_name
See pyenvand pyenv-virtualenvfor more info.
有关更多信息,请参阅pyenv和pyenv-virtualenv。
EDIT (2019/03/19)
编辑 (2019/03/19)
I have found using the pyenv-installereasier than homebrew to install pyenv and pyenv-virtualenv direclty:
我发现使用pyenv-installer比自制软件更容易安装 pyenv 和 pyenv-virtualenv direclty:
curl https://pyenv.run | bash
To manage python version, either globally:
要管理 python 版本,全局:
pyenv global 3.6.5
or locally in a given directory:
或本地在给定目录中:
pyenv local 3.6.5
回答by Justin Shan
In case anyone face pip issue like below
如果有人面临如下 pip 问题
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
根本原因是 openssl 1.1 不再支持 python 3.6。所以你需要安装旧版本的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
回答by Steven Peters
To solve this with homebrew
, you can temporarily backdate homebrew-core
and set the HOMEBREW_NO_AUTO_UPDATE
variable to hold it in place:
要使用 解决此问题homebrew
,您可以暂时回溯homebrew-core
并设置HOMEBREW_NO_AUTO_UPDATE
变量以将其固定到位:
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.
我不建议永久回溯 homebrew-core,因为您会错过安全补丁,但它对于测试目的很有用。
You can also extract old versions of homebrew formulae into your own tap (tap_owner/tap_name) using the brew extract
command:
您还可以使用以下brew extract
命令将旧版本的自制程序公式提取到您自己的水龙头 (tap_owner/tap_name) 中:
brew extract python tap_owner/tap_name --version=3.6.5