最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中如何安装Python3且和Python2共存

Mac crifan 4833浏览 0评论

折腾:

【已解决】Mac中安装Python的第三方库:openpyxl

期间,需要去,在不影响已有的Mac中的Python2的情况下,去安装Python3

mac python 3

Python Releases for Mac OS X | Python.org

4. Using Python on a Macintosh — Python 3.6.1 documentation

Installing Python 3 on Mac OS X — The Hitchhiker’s Guide to Python

➜  ~ which brew
/usr/local/bin/brew
➜  ~ where brew
/usr/local/bin/brew
➜  ~ brew –version
Homebrew 1.1.10
Homebrew/homebrew-core (git revision 8f25; last commit 2017-03-04)

确认已经安装了brew了。

考虑用brew去安装Python3

但是此处Homebrew并没有帮忙安装pip3:

➜  ~ pip
pip         pipestatus

Mac OSX 正确地同时安装Python 2.7 和Python3 – 简书

Mac OSX 正確地同時安裝 Python 2.7 和 Python3 – 字串小豬

去执行:

brew install python3

➜  ~ brew install python3
Updating Homebrew…
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
adr-tools                         exa                               jvm-mon                           rsync
afuse                             expect                            krb5                              s3-backer
。。。
protobuf                          zurlfpc                               libpng                            protobuf-c
==> Renamed Formulae
bash-completion2 -> bash-completion@2                               maven31 -> [email protected]
。。。
logstash24 -> [email protected]                                          zeromq32 -> [email protected]
mapnik2 -> mapnik@2                                                 zeromq40 -> [email protected]
mariadb100 -> [email protected]
==> Deleted Formulae
ctorrent                   [email protected]          gnupg2                     multirust                  scsh
devhelp                    [email protected]          libgc                      node@5                     webkitgtk
ee                         fb-adb                     libgroove                  s3sync                     xstow
==> Installing dependencies for python3: pkg-config, readline, sqlite, gdbm, openssl, xz
==> Installing python3 dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.2.sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/pkg-config
Target /usr/local/bin/pkg-config
is a symlink belonging to pkg-config. You can unlink it:
  brew unlink pkg-config
To force the link and overwrite all conflicting files:
  brew link –overwrite pkg-config
To list all files that would be deleted:
  brew link –overwrite –dry-run pkg-config
Possible conflicting files are:
/usr/local/bin/pkg-config -> /usr/local/Cellar/pkg-config/0.29.1/bin/pkg-config
/usr/local/share/aclocal/pkg.m4 -> /usr/local/Cellar/pkg-config/0.29.1/share/aclocal/pkg.m4
/usr/local/share/doc/pkg-config/pkg-config-guide.html
/usr/local/share/doc/pkg-config/pkg-config-guide.html
/usr/local/share/man/man1/pkg-config.1 -> /usr/local/Cellar/pkg-config/0.29.1/share/man/man1/pkg-config.1
==> Summary
🍺  /usr/local/Cellar/pkg-config/0.29.2: 11 files, 627KB
==> Installing python3 dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.3_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-7.0.3_1.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.
macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.
For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include
==> Summary
🍺  /usr/local/Cellar/readline/7.0.3_1: 46 files, 1.5MB
==> Installing python3 dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.18.0.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sqlite-3.18.0.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.
macOS provides an older sqlite3
If you need to have this software first in your PATH run:
  echo ‘export PATH="/usr/local/opt/sqlite/bin:$PATH"’ >> ~/.zshrc
For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/sqlite/lib
    CPPFLAGS: -I/usr/local/opt/sqlite/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/sqlite/lib/pkgconfig
==> Summary
🍺  /usr/local/Cellar/sqlite/3.18.0: 11 files, 3.0MB
==> Installing python3 dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.13.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gdbm-1.13.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/gdbm/1.13: 19 files, 554.4KB
==> Installing python3 dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2k.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2k.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs
and run
  /usr/local/opt/openssl/bin/c_rehash
This formula is keg-only, which means it was not symlinked into /usr/local.
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
If you need to have this software first in your PATH run:
  echo ‘export PATH="/usr/local/opt/openssl/bin:$PATH"’ >> ~/.zshrc
For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2k: 1,696 files, 12MB
==> Installing python3 dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.3.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring xz-5.2.3.sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/lzcat
Target /usr/local/bin/lzcat
is a symlink belonging to xz. You can unlink it:
  brew unlink xz
To force the link and overwrite all conflicting files:
  brew link –overwrite xz
To list all files that would be deleted:
  brew link –overwrite –dry-run xz
Possible conflicting files are:
/usr/local/bin/lzcat -> /usr/local/Cellar/xz/5.2.2/bin/lzcat
。。。
/usr/local/share/man/man1/xzmore.1 -> /usr/local/Cellar/xz/5.2.2/share/man/man1/xzmore.1
/usr/local/lib/liblzma.5.dylib -> /usr/local/Cellar/xz/5.2.2/lib/liblzma.5.dylib
/usr/local/lib/liblzma.a -> /usr/local/Cellar/xz/5.2.2/lib/liblzma.a
/usr/local/lib/liblzma.dylib -> /usr/local/Cellar/xz/5.2.2/lib/liblzma.dylib
/usr/local/lib/pkgconfig/liblzma.pc -> /usr/local/Cellar/xz/5.2.2/lib/pkgconfig/liblzma.pc
==> Summary
🍺  /usr/local/Cellar/xz/5.2.3: 92 files, 1.4MB
==> Installing python3
Warning: Building python3 from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select –install
==> Downloading https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
######################################################################## 100.0%
==> ./configure –prefix=/usr/local/Cellar/python3/3.6.1 –enable-ipv6 –datarootdir=/usr/local/Cellar/python3/3.6.1/share –datadir=/
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1/share/python3
==> Downloading https://files.pythonhosted.org/packages/87/1a/33d3d05569e857c5c5cc3e90d197bf4d9696dc740a05f66a09599d66e5bd/setuptools-
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.t
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/wheel-0.29.

但是执行到最后,却卡在了:

/usr/local/lib/liblzma.dylib -> /usr/local/Cellar/xz/5.2.2/lib/liblzma.dylib
/usr/local/lib/pkgconfig/liblzma.pc -> /usr/local/Cellar/xz/5.2.2/lib/pkgconfig/liblzma.pc
==> Summary
🍺  /usr/local/Cellar/xz/5.2.3: 92 files, 1.4MB
==> Installing python3
Warning: Building python3 from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select –install
==> Downloading https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
######################################################################## 100.0%
==> ./configure –prefix=/usr/local/Cellar/python3/3.6.1 –enable-ipv6 –datarootdir=/usr/local/Cellar/python3/3.6.1/share –datadir=/
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1/share/python3
==> Downloading https://files.pythonhosted.org/packages/87/1a/33d3d05569e857c5c5cc3e90d197bf4d9696dc740a05f66a09599d66e5bd/setuptools-
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.t
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/wheel-0.29.

重新去试试:

brew install python3

➜  ~ brew install python3
Warning: Building python3 from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select –install
==> Using the sandbox
==> Downloading https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
Already downloaded: /Users/crifan/Library/Caches/Homebrew/python3-3.6.1.tar.xz
==> ./configure –prefix=/usr/local/Cellar/python3/3.6.1 –enable-ipv6 –datarootdir=/usr/local/Cellar/python3/3.6.1/share –datadir=/usr/local/Cel
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python3/3.6.1/share/python3
==> Downloading https://files.pythonhosted.org/packages/87/1a/33d3d05569e857c5c5cc3e90d197bf4d9696dc740a05f66a09599d66e5bd/setuptools-32.2.0.zip
Already downloaded: /Users/crifan/Library/Caches/Homebrew/python3–setuptools-32.2.0.zip
==> Downloading https://files.pythonhosted.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz
Already downloaded: /Users/crifan/Library/Caches/Homebrew/python3–pip-9.0.1.tar.gz
==> Downloading https://files.pythonhosted.org/packages/c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/wheel-0.29.0.tar.gz
######################################################################## 100.0%
==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python3/3.6.
==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python3/3.6.
==> /usr/local/Cellar/python3/3.6.1/bin/python3 -s setup.py –no-user-cfg install –force –verbose –install-scripts=/usr/local/Cellar/python3/3.6.
==> Caveats
Pip, setuptools, and wheel have been installed. To update them
  pip3 install –upgrade pip setuptools wheel
You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.6/site-packages
See: http://docs.brew.sh/Homebrew-and-Python.html
==> Summary
🍺  /usr/local/Cellar/python3/3.6.1: 7,961 files, 110.0MB, built in 3 minutes 27 seconds
➜  ~

安装完毕了,去看看版本:

➜  ~ which python3
/usr/local/bin/python3
➜  ~ which python
/usr/bin/python
➜  ~ ll /usr/bin/python
-rwxr-xr-x  1 root  wheel    65K  3 23 07:56 /usr/bin/python
➜  ~ python  -V
Python 2.7.10
➜  ~ python3 -V
Python 3.6.1
➜  ~ python3
Python 3.6.1 (default, May  6 2017, 14:32:57)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
KeyboardInterrupt
>>>
[1]  + 31257 suspended  python3
➜  ~ python
Python 2.7.10 (default, Feb  6 2017, 23:53:20)
[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.
>>>
[2]  + 31270 suspended  python

但是还是不敢去直接把环境变量改了

否则感觉是:

默认的python就从python2变成python3了

-》难道不回影响到系统的其他(用到了Python的)软件吗?

Mac OS X系统中如何安装Python| 编程派 | Coding Python

mac python3 安装 python2

在Mac OS X上安装Python — Python最佳实践指南

算了,再去:

brew install python

吧。

➜  ~ brew install python
Updating Homebrew…
Warning: Building python from source:
  The bottle needs the Apple Command Line Tools to be installed.
  You can install them, if desired, with:
    xcode-select –install
==> Using the sandbox
==> Downloading https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
######################################################################## 100.0%
==> ./configure –prefix=/usr/local/Cellar/python/2.7.13 –enable-ipv6 –datarootdir=/usr/local/Cellar/python/2.7.13/share –datadir=/usr/local/Cell
==> make
==> make install PYTHONAPPSDIR=/usr/local/Cellar/python/2.7.13
==> make frameworkinstallextras PYTHONAPPSDIR=/usr/local/Cellar/python/2.7.13/share/python
==> Downloading https://files.pythonhosted.org/packages/26/d1/dc7fe14ce4a3ff3faebf1ac11350de4104ea2d2a80c98393b55c84362b0c/setuptools-32.1.0.tar.gz
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz
######################################################################## 100.0%
==> Downloading https://files.pythonhosted.org/packages/c9/1d/bd19e691fd4cfe908c76c429fe6e4436c9e83583c4414b54f6c85471954a/wheel-0.29.0.tar.gz
######################################################################## 100.0%
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py –no-user-cfg install –force –verbose –single-version-externally-managed –record=inst
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py –no-user-cfg install –force –verbose –single-version-externally-managed –record=inst
==> /usr/local/Cellar/python/2.7.13/bin/python -s setup.py –no-user-cfg install –force –verbose –single-version-externally-managed –record=inst
==> Caveats
Pip and setuptools have been installed. To update them
  pip install –upgrade pip setuptools
You can install Python packages with
  pip install <package>
They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages
See: http://docs.brew.sh/Homebrew-and-Python.html
==> Summary
🍺  /usr/local/Cellar/python/2.7.13: 6,337 files, 86.7MB, built in 6 minutes 35 seconds
➜  ~

然后去修改环境变量的顺序

➜  ~ which python
/usr/bin/python
➜  ~ which python3
/usr/local/bin/python3
➜  ~ ls -l /usr/local/bin/python
lrwxr-xr-x  1 crifan  admin  34  5  6 15:10 /usr/local/bin/python -> ../Cellar/python/2.7.13/bin/python

vim /etc/paths

已经是:

/usr/local/bin

在最开始了。

就不用改了。

然后去关闭后重新打开终端,可以看到已经生效了:

➜  ~ which python
/usr/local/bin/python
➜  ~ which python3
/usr/local/bin/python3
➜  ~ python
Python 2.7.13 (default, May  6 2017, 15:08:03)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
[1]  + 41600 suspended  python
➜  ~ python3
Python 3.6.1 (default, May  6 2017, 14:32:57)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
[2]  + 41605 suspended  python3
➜  ~ ls -l /usr/bin/python
-rwxr-xr-x  1 root  wheel  66848  3 23 07:56 /usr/bin/python
➜  ~ ls -l /usr/local/bin/python
lrwxr-xr-x  1 crifan  admin  34  5  6 15:10 /usr/local/bin/python -> ../Cellar/python/2.7.13/bin/python
➜  ~ ls -l /usr/local/bin/python3
lrwxr-xr-x  1 crifan  admin  35  5  6 14:34 /usr/local/bin/python3 -> ../Cellar/python3/3.6.1/bin/python3

好了,现在应该算是安装成功了。

然后也看到了pip了:

➜  ~ pip
pip         pip2        pip2.7      pip3        pip3.6      pipestatus
➜  ~ which pip
pip: aliased to noglob pip
➜  ~ where pip
pip: aliased to noglob pip
/usr/local/bin/pip
➜  ~ where pip3
/usr/local/bin/pip3
➜  ~ pip -V
pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
➜  ~ pip3 -V
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)

然后基本上有点明白了:

【总结】

此处,之所以说,

用Homebrew去安装了Python2,Python3,然后修改了PATH中的路径顺序后,就可以称得上是:

Mac中的Python2和Python3共存

指的是:

对于Mac中已有的,用到了之前旧的系统的Python2,对应的是/usr/bin/python

而此处的,新安装的

Python2是 /usr/local/bin/python

Python3是 /usr/local/bin/python3

都不会影响到原有的Python的

-》且此处输入了python,系统默认进入的是:

Homebrew安装的新版本的Python2

-》也是和旧的系统的Python2是基本兼容的,而不是Python3

➜  ~ python
Python 2.7.13 (default, May  6 2017, 15:08:03)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
[3]  + 41826 suspended  python
➜  ~ which python
/usr/local/bin/python

转载请注明:在路上 » 【已解决】Mac中如何安装Python3且和Python2共存

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.225 seconds, using 22.13MB memory