版本检查中Python2版本设置为Python3

标签 python python-3.x python-2.7 homebrew

所以我尝试按照本教程在 MacOS 上安装 OpenCV:https://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/

在步骤 3 中,python 的版本应该是使用 python --version 检查的 python 2.x 和使用 python3 --version 检查的 python 3.y。然而,在这两个命令中我都得到了Python 3.6.4。我怎样才能解决这个问题?我尝试使用 brew install python@2 再次安装 python 2,输出为 python@2 2.7.14_1 is已经安装which python 显示 /usr/local/bin/pythonwhich python3 显示 /usr/local/bin/python3 >.

最佳答案

如果您使用 Homebrew 安装了 Python 2 和 Python 3,则 Python 2 二进制文件名为 python2,但不会链接到 /usr/local,除非您使用带有 --force 标志的 brew link

参见brew信息python@2:

$ brew info python@2 | grep Caveats -A 4
==> Caveats
This formula installs a python2 executable to /usr/local/opt/python@2/bin
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
  export PATH="/usr/local/opt/python@2/libexec/bin:$PATH"

$ brew info python@2 | grep 'not symlinked' -A 2
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

你可以运行

/usr/local/opt/python\@2/bin/python2

如果您需要直接运行它,或者您可以强制 Homebrew 程序以任何方式链接它:

brew link --force python@2

此时/usr/local/bin/python2将被添加。

该教程相当过时; Homebrew 处理 Python 的方式已经改变。只要在使用 python 的地方使用 python2 即可。

要进一步解决本教程:

  • brew linkapps 已弃用
  • homebrew/science Tap 已弃用,该 Tap 中的公式已迁移,opencv3 无需额外 Tap 即可安装。
  • 当前版本的 opencv3 原生支持 Python 2 和 3,无需配置开关
  • 该公式直接依赖于Python 2和3公式,安装opencv3将自动安装Python。

因此,只需运行 brew install opencv3,遵循命令打印的任何其他说明(可能没有)。

请确保更新 Homebrew,因为缺少一些依赖项(请参阅 my bug report with Homebrew )。如果不这样做,则必须运行 brew install hdf5 tesseract 来安装本应可选的依赖项。

该教程现在几乎完全过时了。

关于版本检查中Python2版本设置为Python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118550/

相关文章:

python - 如何使用Idealista API获取特性数据?

python - 如何遍历 pandas 组并检查每个组中是否有字符串?

python - 使用带有 selenium 的 Firefox 浏览器时,WebDriverError@chrome 而不是 WebDriverError@firefox

python-2.7 - Airflow : DAG marked as "success" if one task fails, 由于触发规则 ALL_DONE

google-app-engine - Python 2.7 - Codenvy - 调试问题

python - 为什么upload_from_file Google Cloud Storage Function 会引发超时错误?

python - 使用 boto3 客户端提交时,spark-submit EMR 步骤失败

Python Moviepy 模块 : output video?

javascript - 使用 python 3.x 提交 onSubmit ="return func();"的表单

python - 如何使用 Python 启用 Google Assistant API 的新 "read it"功能?