MacOS:如何降级自制 Python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44217507/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:50:41  来源:igfitidea点击:

MacOS: How to downgrade homebrew Python?

pythonmacoshomebrew

提问by leota

I'm running MacOS Sierra 10.12.4 and I've realized that homebrew python was upgraded to version 2.7.13. How can I switch back to 2.7.10?

我正在运行 MacOS Sierra 10.12.4,我意识到自制 python 已升级到版本 2.7.13。如何切换回 2.7.10?

采纳答案by mattmc3

First, it's generally considered bad practice to rely on system python for user land code if you can avoid it. You need to assume that system utilities require a specific version of system python, and your user land code may then be locked to that python version forever, which is not wise (unless you're writing system utilities, in which case just use /bin/python, but then you wouldn't be asking this question...).

首先,如果可以避免的话,依赖系统 python 来获取用户土地代码通常被认为是不好的做法。您需要假设系统实用程序需要特定版本的系统 python,然后您的用户土地代码可能会永远锁定到该 python 版本,这是不明智的(除非您正在编写系统实用程序,在这种情况下只需使用 /bin /python,但那样你就不会问这个问题了......)。

Secondly, I am unclear why you need 2.7.10 instead of 2.7.13. All pythons with the same minor revision number (2.7) should always be compatible. If you needed 2.6, that would be a different story since that's a change in minor version. Code written for 2.7.x should all be compatible.

其次,我不清楚你为什么需要 2.7.10 而不是 2.7.13。具有相同次要修订号 (2.7) 的所有 python 应始终兼容。如果您需要 2.6,那将是另一回事,因为这是次要版本的更改。为 2.7.x 编写的代码应该都是兼容的。

However, assuming your use case really does require using a specific Python version - getting to an actual solution now - be sure sure you really upgraded system python to begin with. If you enter the command: which python, do you get /usr/bin/python(system) or /usr/local/bin/python(brew installed user-land python). For example, /usr/bin/python -Vgives me 2.7.10 even though python -Vgives me 2.7.13 (via brew).

但是,假设您的用例确实需要使用特定的 Python 版本——现在就得到一个实际的解决方案——确保你真的从一开始就升级了系统 python。如果您输入命令:which python,您得到/usr/bin/python(system) 或/usr/local/bin/python(brew installed user-land python)。例如,/usr/bin/python -V即使python -V给了我 2.7.13(通过 brew),也给了我 2.7.10 。

It's possible that you installed the latest python 2.7.x via brewwhich puts /usr/local/bin/pythonas a symlink in your $PATH, or you perhaps have a pythonalias pointing somewhere you don't want. Verify your $PATH order.

这有可能是你安装了最新的Python 2.7.x通过brew这使/usr/local/bin/python作为一个符号链接在你$PATH,或者你可能有一个python别名指向某个你不想。验证您的 $PATH 顺序。

You can reset your homebrew python by removing it (brew uninstall python), or by changing the symlink (ln -s -f /usr/bin/python /usr/local/bin/python). However, using virtualenv removes the need for much of these sorts of gymnastics.

您可以通过删除它 ( brew uninstall python) 或更改符号链接 ( ln -s -f /usr/bin/python /usr/local/bin/python)来重置您的自制 python 。然而,使用 virtualenv 消除了对大部分此类体操的需要。

If you want to monkey with prior versions of Python installed via homebrew, this answer should help: How to install older formula using Brew?

如果您想使用通过自制软件安装的先前版本的 Python,这个答案应该会有所帮助:如何使用 Brew 安装旧公式?

One final option: if you absolutely must have a specific python version, pyenvcan help.

最后一个选择:如果你绝对必须有一个特定的 python 版本,pyenv可以提供帮助。

brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10

回答by Joseph Sheedy

You can switch versions with brew switch. For instance I just downgraded Python 3.7.0 to 3.6.5 like this:

您可以使用brew switch. 例如,我只是像这样将 Python 3.7.0 降级到 3.6.5:

brew switch python 3.6.5

Unfortunately, the brew versionscommand has been deprecated, and it's currently pretty complicated to locate the available versions. I'd love to hear a simple solution to this. Meanwhile, if you know the version you want to switch to, try the above command.

不幸的是,该brew versions命令已被弃用,目前查找可用版本非常复杂。我很想听到一个简单的解决方案。同时,如果您知道要切换到的版本,请尝试上述命令。

I agree with the answers here that virtualenvs are a good idea, but having the version of Python you need in homebrew is also a good idea. The way my virtualenvs were created, bin/pythonwas a symlink to /usr/local/bin/python, so things broke when Python was updated via homebrew.

我同意这里的答案,virtualenvs 是一个好主意,但在自制软件中拥有您需要的 Python 版本也是一个好主意。我的 virtualenv 的创建方式bin/python是指向 的符号链接/usr/local/bin/python,所以当 Python 通过自制软件更新时,事情就坏了。

回答by Sidharth Taneja

Download python 3.6.0 from https://www.python.org/downloads/release/python-360/

https://www.python.org/downloads/release/python-360/下载 python 3.6.0

Install it as a normal package.

将其作为普通包安装。

Run cd /Library/Frameworks/Python.framework/Version

cd /Library/Frameworks/Python.framework/Version

Run ls and all installed Python versions will be visible here.

运行 ls 并且所有已安装的 Python 版本都将在此处可见。

Run sudo rm -rf 3.7

sudo rm -rf 3.7

Check the version now by python3 -Vand it will be 3.6 now.

现在检查版本,现在python3 -V是 3.6。

回答by Abhishek Panwar

There is no need to downgrade python as you can use both on your system.

无需降级 python,因为您可以在系统上同时使用它们。

Places where you want your file to compile with python 2-x.

您希望使用 python 2-x 编译文件的位置。

python2 or python2-x filename.py

and where you need python 3

以及你需要 python 3 的地方

python3 or python3-x filename.py

The default usage of python will lead to use the latest version, and downgrading to a particular version is a lot of headache as it is not direct as python is not made backward compatible from 3-x to 2-x.

python 的默认用法将导致使用最新版本,降级到特定版本是一件很头疼的事,因为它不是直接的,因为 python 没有从 3-x 向后兼容 2-x。

回答by apadana

This is not a direct answer to the question, rather it explains a solution to avoid touching the system python.

这不是问题的直接答案,而是解释了避免接触系统 python 的解决方案。

The general idea is that you should always install the independent python for your projects. Each project needs its own python version (for compatibility reasons with libraries), and it's not practical to keep one python version and trying to make it work with multiple projects.

一般的想法是您应该始终为您的项目安装独立的python。每个项目都需要自己的 Python 版本(出于与库的兼容性原因),保留一个 Python 版本并试图使其适用于多个项目是不切实际的。

I assume this issue in your system happened because another project required a higher python version and now for your other project you need a lower version python.

我假设您的系统中发生此问题是因为另一个项目需要更高版本的 Python,而现在对于您的另一个项目,您需要更低版本的 Python。

The best way to handle python versions is to use virtualenv.

处理 python 版本的最好方法是使用 virtualenv。

Each project will have it's own python, so you can have projects that work with python 2.7 and python 3 and they never touch each other's dependency.

每个项目都有自己的python,因此您可以拥有使用python 2.7和python 3的项目,并且它们从不接触彼此的依赖项。

Install different python versions with homebrew and then for each project when you create virtualenv you decide which python to pick. Everytime you work with that project, the python version would be the one you picked yourself when created the virtualenv.

使用自制软件安装不同的 python 版本,然后在创建 virtualenv 时为每个项目决定选择哪个 python。每次使用该项目时,python 版本将是您在创建 virtualenv 时自己选择的版本。