天天看点

macbook air怎么安装python_Python3安装完全攻略 Mac篇-Go语言中文社区

前言:mac系统自带python,不过以当前mac系统的最新版本为例,自带的python版本都是2.X版本,虽然不影响老版本项目的运行,但是python最新的3.X版本的一些语法与2.X版本并不相同,网上的教程大神们也肯定都更新出了最新版的教程,我们不论是学习还是使用,当然用最新版会更好一点。

我的电脑是MacBook Air,系统版本是最新的macOS Catalina 10.15.1

一、熟悉系统自带的python

在安装最新版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’。

二、开始安装Python

1、安装homebrew

$ /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.

2、搜索python3的包

$ 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=httpsnhost=github.comn" | 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=httpsnhost=github.comn" | 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...

3、安装python3

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)

第一个Error:

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这个文件。

第二个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)

更新Ruby失败,Homebrew必须运行在Ruby 2.6下!

我们运行的是Ruby 2.3.3。

解决:

更新Ruby

4、查看Ruby版本

$ rvm list known

#列出已知的ruby版本

Warning, new version of rvm available '1.29.9-next', you are using older version '1.26.11'.

问题:

发现rvm版本也需要更新

5、更新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