问题阐述:

MacOS默认Python版本是2.7.10,随着Python3的进一步占有市场,Python2.7也将在2020年结束维护,所以在同一台电脑上安装多个Python版本势在必行。

一、pyenv的使用

首先,安装pyenv,参考地址

1,安装Homebrew,参考地址

2,安装pyenv:

$ brew update
$ brew install pyenv

3,添加pyenv init到shell里

$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

4,重启shell,使配置生效

然后,pyenv基本使用方法

1,列出系统安装的所有Python版本

ritchdeMacBook-Pro:~ ritch$ pyenv versions
* system (set by /Users/ritch/.pyenv/version)
3.7.0

2,列出当前Python版本

ritchdeMacBook-Pro:~ ritch$ pyenv version
system (set by /Users/ritch/.pyenv/version)

3,列出pyenv可供安装使用的Python版本

ritchdeMacBook-Pro:~ ritch$ pyenv install -l
Available versions:
2.1.3
2.2.3
2.3.7
...
...
3.6.5
3.6.6
3.7.0
3.7-dev
3.8-dev

4,安装Python版本

ritchdeMacBook-Pro:~ ritch$ pyenv install 3.6.6
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew
Installed Python-3.6.6 to /Users/ritch/.pyenv/versions/3.6.6

5,全局切换Python版本

ritchdeMacBook-Pro:~ ritch$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ritchdeMacBook-Pro:~ ritch$ pyenv versions
* system (set by /Users/ritch/.pyenv/version)
3.6.6
3.7.0
ritchdeMacBook-Pro:~ ritch$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ritchdeMacBook-Pro:~ ritch$ pyenv global 3.6.6
ritchdeMacBook-Pro:~ ritch$ python
Python 3.6.6 (default, Sep 27 2018, 13:24:00)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

其中Python版本控制,分为三个场景:

global:全局范围内Python版本的展示和修改。

local:当前目录下Python版本的展示和修改。

shell:当前shell会话Python版本的展示和修改,适用于脚本执行的情况,当前会话结束后,Python版本回复原样。

二、多版本情况的pip使用

Python好用的地方是,有很多成熟的第三方库。安装了多个Python版本,对应的pip怎么使用呢?

首先,安装

正常情况下,Python2 >= 2.7.9 或者 Python3 >= 3.4,pip已经被安装好了。

保证Upgrading pip:

ritchdeMacBook-Pro:~ ritch$ python -V
Python 3.6.6
ritchdeMacBook-Pro:~ ritch$ python -m pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

如果系统里没有安装好pip,参看安装

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

然后,使用

Python 3.6.6,使用pip安装第三方库bs4:

ritchdeMacBook-Pro:~ ritch$ pyenv global 3.6.6
ritchdeMacBook-Pro:~ ritch$ python -V
Python 3.6.6
ritchdeMacBook-Pro:~ ritch$ python -m pip install bs4
Collecting bs4
Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz
Collecting beautifulsoup4 (from bs4)
Downloading https://files.pythonhosted.org/packages/21/0a/47fdf541c97fd9b6a610cb5fd518175308a7cc60569962e776ac52420387/beautifulsoup4-4.6.3-py3-none-any.whl (90kB)
100% |████████████████████████████████| 92kB 537kB/s
Installing collected packages: beautifulsoup4, bs4
Running setup.py install for bs4 ... done
Successfully installed beautifulsoup4-4.6.3 bs4-0.0.1

Python 3.7.0,使用pip安装第三方库bs4:

ritchdeMacBook-Pro:~ ritch$ pyenv global 3.7.0
ritchdeMacBook-Pro:~ ritch$ python -V
Python 3.7.0
ritchdeMacBook-Pro:~ ritch$ python -m pip install bs4
Collecting bs4
Using cached https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz
Collecting beautifulsoup4 (from bs4)
Using cached https://files.pythonhosted.org/packages/21/0a/47fdf541c97fd9b6a610cb5fd518175308a7cc60569962e776ac52420387/beautifulsoup4-4.6.3-py3-none-any.whl
Installing collected packages: beautifulsoup4, bs4
Running setup.py install for bs4 ... done
Successfully installed beautifulsoup4-4.6.3 bs4-0.0.1

三、总结

pyenv配合pip,可以很好解决Python多版本的问题。

同时也可以在轻量级编辑器(VS Code、Sublime)上很好的配合使用,VS Code如下截图: 

MacOS下,Python2和Python3完美兼容使用(转)的更多相关文章

  1. windows下python2和python3同时安装ipython

    1.ipython简介: IPython 是一个 python 的交互式 shell,比默认的python shell 好用得多,支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许 ...

  2. Windows下Python2与Python3两个版本共存的方法详解

    来源:http://www.jb51.net/article/105311.htm 这篇文章主要介绍了Windows下Python2与Python3两个版本共存的方法,文中介绍的很详细,对大家具有一定 ...

  3. Windows下同时安装python2和python3如何兼容版本

    引言:因学习需要把python2和python3都安装了,为了避免使用过程中混淆版本在网上找了一些解决方案,亲测可用.方法如下: 分别下载并安装Python2.x和Python3.x. 配置环境变量. ...

  4. Windows下python2与python3兼容设置

    分别安装python2与python3后,我想直接通过命令python2.pip2与python3.pip3区分: 分别进入python安装目录下,修改python.exe为python2.exe.p ...

  5. win10下python2与python3以及pip共存

    一 分别安装python2和python3 注意: 安装时记得勾选 Add Python.exe to Path 二 安装pip Python3最新版本有pip,无需安装 Python2: 下载pip ...

  6. linux下python2升级python3,python2和python3并存

    wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz 解压:tar -xzvf Python-3.6.4.tgz cd Pytho ...

  7. jupyter notebook下python2和python3共存(Ubuntu)

    提示NOTICE 时间:2018/04/06 主题:Ubuntu 下CAFFE框架 主角:Jupyter Notebook 简介: Jupyter Notebook(此前被称为 IPython not ...

  8. win10下 anaconda 环境下python2和python3版本转换

    在cmd的环境下,输入以下命令安装Python2.7的环境 conda create -n python27 python=2.7 anaconda 上面的代码创建了一个名为python27的pyth ...

  9. Linux下Python2升级Python3

    Linux下Python2的升级方法: 一.下载Python3安装包: 1.在线下载 wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2 ...

随机推荐

  1. CodeForces - 1017 C. The Phone Number(数学)

    Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...

  2. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  3. scrollHeight

    scrollHeight=显示内容高度+隐藏内容高度 参考: https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight ...

  4. docker数据卷(volume)

    1.什么是数据卷volume https://blog.csdn.net/dream_broken/article/details/52314993 为了了解什么是Docker Volume,首先我们 ...

  5. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  6. 类方法,实例方法,静态方法,@property的应用

    class test(object): h = 'hello' w = 'world' def demo(self): print("demo") def test_class(s ...

  7. IIS应用池保持激活工具开发

    之前的 开源一个定时任务调度器 webscheduler 中涉及的定时调用应用已经开始在正式环境中启用,发布到IIS下进行测试,发现一旦应用长时间没有访问(大约半个多小时)就会引发 Applicati ...

  8. numpy和matlab计算协方差矩阵的不同(matlab是标准的,numpy相当于转置后计算)

    matlab是标准的,numpy相当于转置后计算 >> x = [2,0,-1.4;2.2,0.2,-1.5;2.4,0.1,-1;1.9,0,-1.2] x = 2.0000    0 ...

  9. linux日常管理-抓包工具tcpdump和tshark

    抓包工具:查看什么数据占用网卡,把带宽跑满了. 命令:tcpdump 选项:host 指定IP port 指定端口 -c 指定包数量 -w 指定写入文件,不加显示的不是流量包而是流量走向 -nn 作用 ...

  10. 树莓派 Learning 002 装机后的必要操作 --- 01 解决上网问题

    树莓派 装机后的必要操作 - 解决上网问题 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 装机后的必要操作 解决上网问题 解决上网 ...