MacOS下,Python2和Python3完美兼容使用(转)
2023-09-14 08:57:39 时间
问题阐述:
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如下截图:
相关文章
- python2 和 python3 常见差异及兼容方式梳理
- Ubuntu16.04系统查看已安装的python版本,及Python2与Python3之间切换
- 安装搭建Python2.* 和3.* 环境详细步骤
- 当同时安装Python2和Python3后,如何兼容并切换使用详解(比如pip使用)
- python2与python3中input的区别
- Python3与Python2的一些区别
- python2装饰器_python内置装饰器
- python2装饰器_python进阶路线图
- 公司项目申请软著,看到同事在一行一行的复制,用python2分钟帮了他
- 黑帽编程应用之Python2
- python2 nonlocal_python非零返回
- python2 nonlocal_Python nonlocal
- python2 nonlocal_python关键字及用法
- CentOS7安装python3与python2共存
- Python2安装_python安装不了
- Linux系统自带Python2&yum的卸载及重装
- 如何使用Python2启动一个https服务器?
- 旧酒换新瓶,新版M1/M2芯片Macos系统(Ventura)安装古早版本Python2.7(Python2.x)
- Python2.x 与 3.x 版本区别
- centos7系统下python2与python3共存
- 把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案详解编程语言
- python的基本知识,range在python2.x中和python3.x中的区别详解编程语言
- python2.x和3.x的区别(不定时更新)详解编程语言
- Python3.x与Python2.x的区别详解编程语言
- Python3.x与Python2.x的差异用法详解编程语言
- Linux 卸载Python3:步骤与注意事项(linux卸载python3)
- Python3.x和Python2.x的区别介绍