前言:mac系统自带python,不过以当前mac系统的最新版本为例,自带的python版本都是2.X版本,虽然不影响老版本项目的运行,但是python最新的3.X版本的一些语法与2.X版本并不相同,网上的教程大神们也肯定都更新出了最新版的教程,我们不论是学习还是使用,当然用最新版会更好一点。
我的电脑是MacBook Air,系统版本是最新的macOS Catalina 10.15.1
在安装最新版Python3.X之前,我们先熟悉一下系统自带的python。
打开终端,输入:
$ python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Oct 17 2019, 17:14:30)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
翻译下前面三句话:
这个版本包含在macOS中,以便与遗留软件兼容。
macOS的未来版本将不包括Python 2.7。
相反,建议您从终端内部过渡到使用’python3’。
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Caskroom
==> The Xcode Command Line Tools will be installed.
$ brew search python3
Error: You have not agreed to the Xcode license. Please resolve this by running:
sudo xcodebuild -license accept
$ sudo xcodebuild -license accept
Password:****
因为更新了XCODE还未来得及使用。
打开Xcode 统一协议,输入密码,so easy!
$ brew search python3
==> Searching local taps...
python3
==> Searching taps on GitHub...
Error: GitHub
The GitHub credentials in the macOS keychain may be invalid.
Clear them with:
printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
Or create a personal access token:
https://github.com/settings/tokens/new?scopes=gist,public_repo&description=Homebrew
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"
The GitHub credentials in the macOS keychain may be invalid
大体的意思就是说credentials 在钥匙串中的凭证失效了,因为苹果在某些过程中是需要验证的 ,我们的钥匙串中保存了很多不同的证书和凭证,如果过期了,那么就要重新生成或者授权才可以继续使用。
既然知道问题了 ,下面就是动手解决了,上面也提示了给你两种解决方法。
Clear them with:
printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
用上面的语句清理下就可以了。
方法二就没有去尝试了,只能简单的给大家介绍一下,而且方法二比方法一相对麻烦一些 。
这个要自己去GitHub 上重新创建一个token,然后回到本地,重新设置下token
create a personal access token:
https://github.com/settings/tokens/new?scopes=gist,public_repo&description=Homebrew
and then set the token as: export HOMEBREW_GITHUB_API_TOKEN="your_new_token"
相对与方法一,只要一个命令行就搞定不要方便的太多
$ brew search python3
==> Searching local taps...
python3
==> Searching taps on GitHub...
homebrew/linuxbrew-core/boost-python3
==> Searching blacklisted, migrated and deleted formulae...
brew install python3
curl: (18) transfer closed with 3530328 bytes remaining to read
Error: Checksum mismatch.
Expected: ab81211a2052ccaa6d050741c433b728d0641523d8742eef23a5b450811e5104
Actual: 9dbd52ae7bb95ecf6b1511e88651c5630e9b426a7b0b45b212b4459d0b0a0c9e
Archive: /Users/xiatian/Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
To retry an incomplete download, remove the file above.
Error: Failed to upgrade vendor Ruby.
/usr/local/Homebrew/Library/Homebrew/brew.rb:10:in `': Homebrew must be run under Ruby 2.6! You're running 2.3.3. (RuntimeError)
/usr/local/Homebrew/Library/Homebrew/brew.rb:10:in `': Homebrew must be run under Ruby 2.6! You're running 2.3.3. (RuntimeError)
Archive: /Users/xiatian/Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
To retry an incomplete download, remove the file above.
#要重试未完成的下载,请删除上面的文件。
不多说,删掉/Users/xiatian/Library/Caches/Homebrew/portable-ruby-2.6.3.mavericks.bottle.tar.gz
这个文件。
Failed to upgrade vendor Ruby.
/usr/local/Homebrew/Library/Homebrew/brew.rb:10:in `': Homebrew must be run under Ruby 2.6! You're running 2.3.3. (RuntimeError)
更新Ruby失败,Homebrew必须运行在Ruby 2.6下!
我们运行的是Ruby 2.3.3。
更新Ruby
$ rvm list known
#列出已知的ruby版本
Warning, new version of rvm available '1.29.9-next', you are using older version '1.26.11'.
发现rvm版本也需要更新
$ rvm get stable
Downloading https://get.rvm.io
No GPG software exists to validate rvm-installer, skipping.
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc',
but no GPG software exists to validate it, skipping.
Upgrading the RVM installation in /Users/xiatian/.rvm/
RVM PATH line found in /Users/xiatian/.mkshrc /Users/xiatian/.profile /Users/xiatian/.bashrc /Users/xiatian/.zshrc.
RVM sourcing line found in /Users/xiatian/.profile /Users/xiatian/.bash_profile /Users/xiatian/.zlogin.
Upgrade of RVM in /Users/xiatian/.rvm/ is complete.
* RVM 1.30 simplifies behavior of 'rvm wrapper' subcommand
Thanks for installing RVM
Please consider donating to our open collective to help us maintain RVM.
Donate: https://opencollective.com/rvm/donate
RVM reloaded!
$ rvm install ruby-2.6.5
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.15/x86_64/ruby-2.6.5.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Installing requirements for osx.
Updating system...........Failed to update Homebrew, follow instructions at
https://docs.brew.sh/Common-Issues
and make sure `brew update` works before continuing.
.
Error running 'requirements_osx_brew_update_system ruby-2.6.5',
please read /Users/xiatian/.rvm/log/1574165496_ruby-2.6.5/update_system.log
Requirements installation failed with status: 1.
$ brew install ruby
Updating Homebrew...
Error: readline: undefined method `undent' for #
先运行下面命令
$ rvm autolibs read-only
再安装:
$ rvm install ruby-2.6.5
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.15/x86_64/ruby-2.6.5.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Missing required packages: coreutils readline zlib openssl@1.1
Somehow it happened there is no executable 'openssl',
run 'brew doctor' and make sure latest '[email protected]' is installed properly.
Requirements installation successful.
查看版本:
$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
安装成功。
在上面的安装过程,发现我们的homebrew版本也需要更新。
$ brew update
fatal: unable to access 'https://github.com/Homebrew/brew/': LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
$ cd
$ curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
$ vim brew_install
进入到brew_install
按 i 进入编辑模式
更改脚本中的资源链接,替换成中国科学技术大学的镜像
就是把这两句
BREW_REPO = “https://github.com/Homebrew/brew“.freeze
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze
更改为这两句
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "https://mirrors.ustc.edu.cn/homebrew-core.git".freeze
改完如下图。
按ESC退出编辑模式,输入:wq
退出并保存。
$ rm -rf /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
// 执行下面这句命令,更换为中科院的镜像:
$ git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew'...
remote: Counting objects: 656959, done.
remote: Total 656959 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (656959/656959), 213.43 MiB | 2.02 MiB/s, done.
Resolving deltas: 100% (431789/431789), done.
Checking out files: 100% (5104/5104), done.
// 把homebrew-core的镜像地址也设为中科院的国内镜像
$ cd "$(brew --repo)"
Homebrew $ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
Homebrew $ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
-bash: cd: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core: No such file or directory
没有这个文件。
找一下这个目录:
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
发现这个目录竟然不存在,那我们就来手动创建一个:
$ sudo mkdir /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core
再执行一遍:
Homebrew $ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
homebrew-core $ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
这下顺利运行,再来安装python3。
$ brew install python3
还是报homebrew的错误,没办法,卸载了homebrew重新安装吧。
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
curl: (56) LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
-e:254: unterminated string meets end of file
... "The following existing direct
-e:254: syntax error, unexpected end-of-input, expecting end
应该是这个资源访问有问题,那么我们可以尝试使用国内的镜像。
给大家推荐一个中国科学技术大学的镜像站点,里面有各种资源:
https://mirrors.ustc.edu.cn/brew.git
继续爬坑
$ curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install
$ vim brew_install
进入到brew_install
按 i 进入编辑模式
更改脚本中的资源链接,替换成中国科学技术大学的镜像
就是把这两句
BREW_REPO = “https://github.com/Homebrew/brew“.freeze
CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core“.freeze
更改为这两句
BREW_REPO = "https://mirrors.ustc.edu.cn/brew.git".freeze
CORE_TAP_REPO = "https://mirrors.ustc.edu.cn/homebrew-core.git".freeze
按ESC退出编辑模式,输入:wq
退出并保存。
这个前面已经做过了,不多解释。
$ /usr/bin/ruby brew_install
fatal: unable to access 'https://github.com/Homebrew/brew/': LibreSSL SSL_read: SSL_ERROR_SYSCALL, errno 54
Failed during: git fetch origin master:refs/remotes/origin/master --tags --force
出现这个原因是因为源不通,代码来不下来,解决方法就是更换国内镜像源:
执行下面这句命令,更换为中国科学技术大学的镜像:
$ git clone git://mirrors.ustc.edu.cn/homebrew-core.git/ /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core --depth=1
就下载成功了
然后把homebrew-core的镜像地址也设为中国科学技术大学的国内镜像
$ cd "$(brew --repo)"
Homebrew $ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
Homebrew $ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
homebrew-core $ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
$ brew update
这次更新成功。
Updated 1 tap (homebrew/core).
==> Updated Formulae
ansible fwup mlt
auditbeat gatsby-cli paket
aws-cdk ghq picard-tools
aws-sdk-cpp glib redis
breezy goreleaser reminiscence
ccache helmfile scrcpy
cheat jenkins-lts serverless
clojure jhipster swagger-codegen
cointop kubebuilder swagger-codegen@2
convox lerna synscan
darksky-weather libimobiledevice telegraf
diamond librealsense tig
docker-compose-completion libusbmuxd tunnel
elasticsearch libxlsxwriter wxpython
exploitdb lmod zstd
folly mesa
$ brew doctor
Ps:其实查出一堆Warning,不过不影响安装Python,这里暂时不细究了。
直接使用 Homebrew 还需要更改默认源,不然谁用谁想打人,原因你懂的。
以下是将默认源替换为国内 USTC 源的方法。
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
$ cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc
安装前先搜索一下是否已经存在python3的包:
$ brew search python3
==> Formulae
boost-python3 python3 python@3
已经存在,我们可以直接安装了。
$ brew install python3
#以上省略一堆安装信息
==> python
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install python@2
You can install Python packages with
pip3 install
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
$ brew info python3
python: stable 3.7.5 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/3.7.5 (3,972 files, 61.0MB) *
Poured from bottle on 2019-11-21 at 18:59:08
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/python.rb
==> Dependencies
Build: pkg-config ✘
Required: gdbm ✔, openssl@1.1 ✔, readline ✔, sqlite ✔, xz ✔
==> Options
--HEAD
Install HEAD version
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install python@2
You can install Python packages with
pip3 install
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
有同学可能想看下pip是否安装成功,可以用
$ pip list
查看是否安装了pip。
mac一般自带python2.7
可以修改 ~/.bash_profile (具体的path取决于你的python3安装路径):
$ vi ~/.bash_profile
按 i 进入编辑模式
# 添加这一行
alias python="/usr/local/bin/python3"
按ESC退出编辑模式,输入:wq,退出并保存
$ source ~/.bash_profile
1、替换 cask 软件仓库,文件缺失
$ cd "$(brew --repo)"/Library/Taps/caskroom/homebrew-cask
-bash: cd: /usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask: No such file or directory
2、$ brew doctor
中的一堆警告
3、python3安装中Build: pkg-config ✘
4、$ pip install --upgrade pip
pip更新失败