我最近买了macbook,我对这个操作系统的一切都很陌生。遵循一些教程来设置机器来进行编程和开发。通过这种方式,我已经通过Homebrew安装了python(3.9),稍后在检查brew和终端中的路径时,两者都指向python 2.7.16,然后我发现Mac已经有了自己的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)链接起来??
发布于 2021-05-30 00:22:23
我是否必须先将系统的版本更新到Python3,然后将其链接到Brew的版本(3.9.15)
python2和python3是不兼容的软件。您不应该尝试将该版本“更新”到python3。因为如果您这样做,现有的软件可能会停止工作,因为它可能依赖于python2。您最好的操作方法是显式地使用brew的python3版本,python2仍然处于弃用模式。(我使用类似于alias py3=python3
的别名,所以我可以使用py3
来调用它,而不是编写python3
)
顺便提一句--您应该使用virtualenv/文夫来设置您的项目/程序;这将允许不污染您的系统安装。您甚至可以通过在requirements.txt中提取项目依赖项来与另一台计算机共享该数据。
https://stackoverflow.com/questions/67757528
复制