MacOS下,Python2和Python3完美兼容使用(转)

问题阐述:python

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

 

1、pyenv的使用github

首先,安装pyenv,参考地址shell

1,安装Homebrew,参考地址bootstrap

2,安装pyenv:bash

$ brew update
$ brew install pyenv

3,添加pyenv init到shell里curl

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

4,重启shell,使配置生效编辑器

 

而后,pyenv基本使用方法ui

1,列出系统安装的全部Python版本url

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版本回复原样。

 

2、多版本状况的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
复制代码

 

 3、总结

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

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