Python 2.7 辛苦了,你好Python 3.7

1,887 阅读2分钟

Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. 很多小伙伴在使用python的时候,都会报出相应警告,毕竟2020.我们的可爱敬业的 Python 2.7就会退出我们的舞台,辛苦了。不断的更新也是不断功能的健全和完美!今天也替换了 python 3.7.3 体验还不错,暂时没有任何问题,文档记录

image.png

安装python3

直接下载安装

web界面是真的丑

  • 1、下载地址如下:python 3.7

  • 2、点击下载好的pkg文件进行安装

  • 3、安装完成之后,python 3.70的默认安装路径如下: /Library/Frameworks/Python.framework/Versions/3.7

  • 4、修改profile文件

terminal里运行open  ~/.bash_profile,打开~/.bash_profile,直接修改文件即可

open  ~/.bash_profile
  • 4.1、修改方式 - 添加
alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

或者:

source ~/.bash_profile
# Setting PATH for Python 3.7

# The original version is saved in .bash_profile.pysave

export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/3.7/bin:

alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"
  • 5、重启一下Terminal,使修改后的文件生效。

Python 2.7与Python 3.7版本的切换

使用Python 2.7时,直接将~/.bash_profile文件中3.7的版本注释掉,即

#aliaspython="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

使用Python 3.7时,直接将~/.bash_profile文件中3.7的版本打开,即

aliaspython="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

安装Homebrew

image.png

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。 一句话总结 : 用它超爽

要求

  • Xcode命令行工具 xcode-select --install
  • 支持shell (sh或者bash)

安装和卸载

  • 安装
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 卸载 (我觉得安装之后就不需要了卸载了)
$ cd `brew --prefix`
$ rm -rf Cellar
$ brew prune
$ rm `git ls-files`
$ rm -r Library/Homebrew Library/Aliases Library/Formula Library/Contributions
$ rm -rf .git
$ rm -rf ~/Library/Caches/Homebrew

Homebrew基本使用

  • 安装任意包(超简单)
brew install <packageName>
比如:安装node
$ brew install node
  • 卸载包
$ brew uninstall <packageName>
比如:卸载node
$ brew uninstall node
  • Homebrew其他指令
$ brew search <packageName>
* 查看已安装包列表
$ brew list
* 查看任意包信息
$ brew info <packageName>
* 更新Homebrew
$ brew update
* 查看Homebrew版本
$ brew -v
* Homebrew帮助信息
$ brew -h

安装python

* 1、查看自己的python版本
python
* 2、查看python3的安装包
brew search python3
* 3、安装python3
brew install python3
接下来需要等待几分钟,速度有点慢,抽根烟,45度仰望一下天空

image.png

OK,可以看到python3的实际安装目录是/usr/local/Cellar/python/3.7.3

因为每次都需要python3 感觉有点恶心,于是重命名python

  • 4、打开配置文件并写入python的外部环境变量
open ~/.bash_profile
  • 5、重命名python
alias python="/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/bin/python3.7"
  • 6、让配置文件生效
source ~/.bash_profile
  • 7、重启电脑使用python命令查看
python

python的作用 - iOS开发可以自己模拟一个Python服务器

控制台: sudo easy_install pip 安装pip(python的包管理工具) sudo pip instlal flask 安装flask框架(python的web框架) pip list

  • 导入相关库,开始一段简单代码

image.png

  • 配置xcode以来执行环境 - python

image.png

  • 选择python存放路径

image.png

image.png

  • 创建python 文件

image.png

image.png

image.png

  • 工具路径设置

image.png