Python 3.x 我的Macbook上有两个python版本,需要帮助解决这个问题吗

Python 3.x 我的Macbook上有两个python版本,需要帮助解决这个问题吗,python-3.x,macos,python-2.7,homebrew,Python 3.x,Macos,Python 2.7,Homebrew,我最近买了macbook,我对这个操作系统的一切都是新手。遵循一些教程来设置机器的编程和开发。这样,我通过自制安装了python(3.9),后来在brew和terminal中检查路径时,都指向python 2.7.16,然后我意识到Mac OS已经有了自己的2.7.16安装。现在,我正在网上浏览多个建议,如何克服这个问题,并将单个版本作为默认版本。我发现以下命令将brew版本(3.9.15)与系统版本(2.7.16)链接起来。抄袭另一篇文章 以下是让我困惑的原因以及我是如何解决的 $ which

我最近买了macbook,我对这个操作系统的一切都是新手。遵循一些教程来设置机器的编程和开发。这样,我通过自制安装了python(3.9),后来在brew和terminal中检查路径时,都指向python 2.7.16,然后我意识到Mac OS已经有了自己的2.7.16安装。现在,我正在网上浏览多个建议,如何克服这个问题,并将单个版本作为默认版本。我发现以下命令将brew版本(3.9.15)与系统版本(2.7.16)链接起来。抄袭另一篇文章

以下是让我困惑的原因以及我是如何解决的

$ which python
/usr/bin/python 

$ which python3
/usr/local/bin/python3

$ ls /usr/local/bin/python
ls: /usr/local/bin/python: No such file or directory
So notice I didn't have a HomeBrew installation of python2.7, but did have the python3 installation. The version under /usr/bin/python is using the system default. You can tell based on the module search path:

$ /usr/bin/python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
`enter code here`Type "help", "copyright", "credits" or "license" for 
more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/...
Notice the '/Library/Python'... that's Mac OS's version of python. But I want to stay strictly on a user installed version (i.e. HomeBrew).

So here's what I did to fix this:

$ brew install python
...
Warning: python 2.7.13 is already installed, it's just not linked.
You can use `brew link python` to link this version.

$ brew link --overwrite python

$ which python
/usr/local/bin/python

$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/Cellar/python/2.7.13...
Its no longer /Library/.. but /usr/local.
现在可以找到我安装的所有pip模块了!问题已解决!]]

上述步骤实际上是处理Python2的类似版本~2.7的合并

但在我的机器中,我安装了Python3,它在前面安装了Python2

这是我的问题

我是否必须首先将系统版本更新为Python3,然后按照上述命令或任何建议将其与Brew版本(3.9.15)链接起来

我是否必须先将系统版本更新为Python3,然后将其与Brew版本(3.9.15)链接

python2和python3是不兼容的软件。您不应该尝试将该版本“更新”到python3。因为如果您这样做,现有的软件可能会停止工作,因为它可能依赖于python2。最好的做法是明确使用brew中的python3版本,python2仍处于弃用模式。(我使用别名,比如
alias py3=python3
,因此我可以使用
py3
调用它,而不是编写
python3

另一方面,您应该使用virtualenv/venv设置您的项目/程序;这样就不会污染您的系统安装。您甚至可以通过在requirements.txt中提取项目依赖项来与另一台计算机共享这一点。

我的建议(我认为这是相对标准的):不要触摸系统安装,可能最好不要使用自制的python安装。使用virtualenv、conda或docker分别定义与系统版本不同的python依赖项。