在 Mac OS X 上安装了 Python 3 但它仍然是 Python 2.7

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

Installed Python 3 on Mac OS X but its still Python 2.7

pythonpython-2.7python-3.xosx-yosemite

提问by PacificNW_Lover

I am currently running OS X Yosemite (10.10.2) on my MacBook Pro... By default, Apple ships Python 2.7.6 on Yosemite.

我目前在我的 MacBook Pro 上运行 OS X Yosemite (10.10.2)...默认情况下,Apple 在 Yosemite 上提供 Python 2.7.6。

Just downloaded and ran this installer for Python 3: python-3.4.3-macosx10.6.pkg

刚刚为 Python 3 下载并运行了这个安装程序: python-3.4.3-macosx10.6.pkg

When I opened up my Terminal and typed in python, this is what came up:

当我打开我的终端并输入时python,出现了以下内容:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Question(s):

问题):

  1. Does anyone know where the Python 3.4.3 interpreter was installed?
  2. Do I need to uninstall Python 2.7.3 (if so, how do I go about doing this) before setting a global environmental variable such as PYTHON_HOME to the location of the installed Python 3.4.3?
  1. 有谁知道 Python 3.4.3 解释器的安装位置?
  2. 在将全局环境变量(例如 PYTHON_HOME)设置到已安装 Python 3.4.3 的位置之前,我是否需要卸载 Python 2.7.3(如果是这样,我该怎么做)?

采纳答案by rhombidodecahedron

Try typing python3instead of just python.

尝试键入python3而不仅仅是python.

回答by nachoiz

In the version OS X El Capitan, you can find the interpreter in: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

在 OS X El Capitan 版本中,您可以在以下位置找到解释器: /opt/local/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

By dragging this path into the Terminal and pressing enter you will be able to run this version.

通过将此路径拖入终端并按 Enter 键,您将能够运行此版本。

To run it faster you can either create an alias by typing in the Terminal: alias python = 'python3.4'.

要更快地运行它,您可以通过在终端中键入来创建别名:alias python = 'python3.4'

回答by abhi

While @rhombidodecahedron's answer is concise and to-the-point and @Nacho Izquierdo addresses your first question perfectly, my answer aims to answer your second question in some more detail:

虽然@rhombidodedecahedron 的回答简明扼要,@Nacho Izquierdo 完美地解决了您的第一个问题,但我的回答旨在更详细地回答您的第二个问题:

One should notuninstall Python 2.7 which comes with Mac OS X; it is supplied by Apple and is needed for applications running on OS X. It is stored in /System/Library/Frameworks/... If it is removed, Mac OS X will have to be reinstalled.

一个应该卸载的Python 2.7里面自带的Mac OS X; 它由 Apple 提供,在 OS X 上运行的应用程序需要它。它存储在/System/Library/Frameworks/... 如果它被删除,则必须重新安装 Mac OS X。

Hope that helps! And to reiterate answers given by @rhombidodecahedron and @Nacho Izquierdo, install Python 3.x separately and use python3if you would like to use that version.

希望有帮助!并重申@rhombidodecahedron 和@Nacho Izquierdo 给出的答案,请单独安装 Python 3.x 并使用(python3如果您想使用该版本)。

Python 2.7 is the standard, Python 3.x is the future.

Python 2.7 是标准,Python 3.x 是未来。

回答by serasmus

You can easily do this using pyenvwhich is a Simple Python Version Management. It allows one to set specific Python versions to run on specific directories or one can change your version before using shell

您可以使用pyenv轻松完成此操作,这是一个简单的 Python 版本管理。它允许您设置特定的 Python 版本以在特定目录上运行,或者您可以在使用 shell 之前更改您的版本

i.e.

IE

$ pyenv install 2.7.6
$ pyenv install 2.6.8
$ pyenv local 2.7.6
$ pyenv versions
  system
  2.6.8
* 2.7.6 (set by /home/yyuu/.pyenv/version)

回答by Sherif Shendidy

Since I know I'll only use python3, I added these 2 lines to .bash_profile file:

因为我知道我只会使用 python3,所以我将这两行添加到 .bash_profile 文件中:

alias python="python3" # to use python3 rather than python2.7
alias idle="idle3" # to use python3 idle rather than 2.7

回答by Vivek Kumar

What you should not do -

你不应该做的——

moving default python binary to an unused name

将默认的 python 二进制文件移动到一个未使用的名称

$ sudo mv /usr/bin/python /usr/bin/python2

$ sudo mv /usr/bin/python /usr/bin/python2

and then moving the new binary to the default path

然后将新的二进制文件移动到默认路径

$ sudo mv $PATHTOBINARY/python3 /usr/bin/python

$ sudo mv $PATHTOBINARY/python3 /usr/bin/python

What could be done but also shouldn't not be done

什么可以做但也不应该做

Since I use zsh by default, I put the following into the .zshrcfile:

由于我默认使用 zsh,因此我将以下内容放入.zshrc文件中:

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc

If you are using the default Bash shell, you can append this same text to your .bashrc:

如果您使用默认的 Bash shell,您可以将相同的文本附加到您的.bashrc

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

This will work but it is not the recommended way because making future updates to Python will be difficult. It means we have to manually download the new files since Python doesn't include a command-line way to update.

这会起作用,但不是推荐的方法,因为将来对 Python 进行更新将很困难。这意味着我们必须手动下载新文件,因为 Python 不包含命令行更新方式。

What is the right way

什么是正确的方法

The basic premise of all Python development is to never use the system Python. You do not want the Mac OS X 'default Python' to be 'python3'.

所有 Python 开发的基本前提是永远不要使用系统 Python。您不希望 Mac OS X 的“默认 Python”为“python3”。

Usage of pyenvto manage Python environments is recommended.

建议使用pyenv来管理 Python 环境。

$ brew install pyenv

$ brew install pyenv

$ pyenv install 3.7.3

$ pyenv install 3.7.3

$ pyenv global 3.7.3

$ pyenv global 3.7.3

$ pyenv version

$ pyenv version

Refresh the current terminal and check

刷新当前终端并检查

$ python -V

$ python -V

It should give Python 3.7.3

它应该给 Python 3.7.3

This way you are good to go.

这样你就可以走了。

For further reference - https://opensource.com/article/19/5/python-3-default-mac

进一步参考 - https://opensource.com/article/19/5/python-3-default-mac