简体   繁体   English

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

[英]Installed Python 3 on Mac OS X but its still Python 2.7

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?有谁知道 Python 3.4.3 解释器的安装位置?
  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?在将全局环境变量(例如 PYTHON_HOME)设置到已安装 Python 3.4.3 的位置之前,我是否需要卸载 Python 2.7.3(如果是这样,我该怎么做)?

尝试输入python3而不仅仅是python

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 not uninstall Python 2.7 which comes with Mac OS X;人们不应卸载的Python 2.7里面自带的Mac OS X; it is supplied by Apple and is needed for applications running on OS X. It is stored in /System/Library/Frameworks/.. .它由 Apple 提供,在 OS X 上运行的应用程序需要它。它存储在/System/Library/Frameworks/.. If it is removed, Mac OS X will have to be reinstalled.如果它被删除,则必须重新安装 Mac OS X。

Hope that helps!希望有帮助! And to reiterate answers given by @rhombidodecahedron and @Nacho Izquierdo, install Python 3.x separately and use python3 if 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 是未来。

What you should not do -你不应该做的——

moving default python binary to an unused name将默认的 python 二进制文件移动到一个未使用的名称

$ 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

What could be done but also shouldn't not be done什么可以做但也不应该做

Since I use zsh by default, I put the following into the .zshrc file:由于我默认使用 zsh,因此我将以下内容放入.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

This will work but it is not the recommended way because making future updates to Python will be difficult.这会起作用,但不是推荐的方法,因为将来对 Python 进行更新将很困难。 It means we have to manually download the new files since Python doesn't include a command-line way to update.这意味着我们必须手动下载新文件,因为 Python 不包含命令行更新方式。

What is the right way什么是正确的方法

The basic premise of all Python development is to never use the system Python.所有 Python 开发的基本前提是永远不要使用系统 Python。 You do not want the Mac OS X 'default Python' to be 'python3'.您不希望 Mac OS X 的“默认 Python”为“python3”。

Usage of pyenv to manage Python environments is recommended.推荐使用pyenv来管理 Python 环境。

$ brew install pyenv

$ pyenv install 3.7.3

$ pyenv global 3.7.3

$ pyenv version

Refresh the current terminal and check刷新当前终端并检查

$ 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

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

要使用Python 3.x,请键入python3而不是python

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找到解释器: /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'

You can easily do this using pyenv which is a Simple Python Version Management.您可以使用pyenv轻松完成此操作,这是一个简单的 Python 版本管理。 It allows one to set specific Python versions to run on specific directories or one can change your version before using shell它允许您设置特定的 Python 版本以在特定目录上运行,或者您可以在使用 shell 之前更改您的版本

ie 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)

After several hours of linking, unlinking, uninstalling and installing python to get a version callback python 3.8.5 rather than python 2.7 adding these 2 lines to .bash_profile file answered above by Sherif Shendidy is what worked for me.经过几个小时的链接、取消链接、卸载和安装 python 以获得版本回调 python 3.8.5 而不是 python 2.7,将这两行添加到 .bash_profile 文件中,由 Sherif Shendidy 回答,这对我有用。

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

I have encountered similar problem on windows as well.I would like to address this issue on windows.我在windows上也遇到过类似的问题。我想在windows上解决这个问题。 If you have already installed python 2 and python 3, Note: While installation make sure you click on "Add to environment variables" or "Add to path".如果您已经安装了 python 2 和 python 3,请注意:安装时请确保单击"Add to environment variables"或“添加到路径”。

Check version in cmd: python -V在cmd中检查版本: python -V

If it shows 2.x, windows > edit the system environment variables > environment variables > user variables > path click edit You should notice a bunch of directories in some order.如果它显示 2.x,windows > 编辑系统环境变量 > 环境变量 > 用户变量 > 路径点击edit你应该注意到一堆按某种顺序排列的目录。 Find a directory similar to below: C:\\Users\\some name\\AppData\\Local\\Programs\\Python\\Python3.x\\ click on Move up until this directory is on top.找到一个类似于下面的目录: C:\\Users\\some name\\AppData\\Local\\Programs\\Python\\Python3.x\\单击Move up直到该目录位于顶部。 click ok .单击ok Now restart the cmd.现在重新启动cmd。 type python -V you will find 3.x输入python -V你会发现3.x

For windows users, I just deleted the folder containing the python2.7.对于windows用户,我刚刚删除了包含python2.7的文件夹。 I just reinstalled the python 3.xx and after that reboot my laptop.我刚刚重新安装了 python 3.xx,然后重新启动了我的笔记本电脑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM