哎呀吆博客

大前端爱好与探索者-Jerry

Mac下安装pyhton3、ipython以及ipython3

python 0 评

学习和使用python的人肯定离不开python2、python3,ipython以及ipython3,在Mac的系统环境下除了默认带有的python2外,其他的几个都得自己来安装了。

1.首先安装pip为后面的安装作准备

pip是一款方便安装和管理Python 包的工具
sudo easy_install pip

安装到最后会这么一段提示

...省略了n行

Installing pip script to /usr/local/bin

Installing pip2.7 script to /usr/local/bin

Installing pip2 script to /usr/local/bin

Installed /Library/Python/2.7/site-packages/pip-10.0.1-py2.7.egg

Processing dependencies for pip

2.然后就可以使用pip来安装ipython了

sudo pip --user install ipython (这里使用–user是为了解决Mac系统下的sip机制权限问题)安装有一堆进度条提示,速度挺快,耐心等待即可。

3.安装python3

因为直接使用brew install python3命令安装python3会报如下错误提示
-bash: brew: command not found
解决方案就是先安装homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
然后就可以愉快的使用brew来安装python3啦
brew install python3

4.最后一项安装:ipython3

使用如下命令安装
sudo pip3 install ipython
耐心等待一堆安装进度的完成即可.

5.验证测试环境

安装成功的情况下,在/usr/local/bin/目录下分别可以看到ipython、ipython3文件,使用sudo vi ipythonsudo vi ipython3打开这两个文件可以看到如下内容

#!/usr/local/opt/python/bin/python3.6

#-*- coding: utf-8 -*-

import re

import sys

from IPython import start_ipython

if __name__ == '__main__':

    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])

    sys.exit(start_ipython())

这里会发现两个解释器的指向都是/usr/local/opt/python/bin/python3.6,那这肯定不符合我们的初衷了(ipython使用python2对应的解释器,ipyhton3使用python3的解释器)。为此问题,也是查了很多解决方案,众说纷纭,最终在一步步的尝试下问题得到了完美解决。本来想着逻辑很简单(其实逻辑就是这么简单),就是把指向路径指引到os自带的python上即可。只是在找自带python(Os10.8以上默认会带)的安装目录费了一番工夫。 其中一种查找方式是,使用python命令进入python shell。

bogon:bin renjie$ 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.

 >>> import os

 >>> os.path

 <module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

但这个路径直接使用的化肯定会报错,我们要根据这个路径逐级返回上级至Versions下,我们可以发现这个目录下的各个2.X版本的python,这里我选择了2.7作为指向目录。

bogon:Versions renjie$ ls

2.3 2.5 2.6 2.7 Current

bogon:Versions renjie$ cd 2.7/

bogon:2.7 renjie$ ls

Extras      Python      _CodeSignature  include

Mac     Resources   bin     lib  #这里参考python3的做法,进入到/bin目录下

bogon:bin renjie$ ls

2to3            pydoc2          pythonw

2to3-2          pydoc2.7        pythonw2  #这里使用python或python2.7都可以

2to3-2.7        python          pythonw2.7

2to32.7         python-config       smtpd.py

idle            python2         smtpd2.7.py

idle2           python2-config      smtpd2.py

idle2.7         python2.7

pydoc           python2.7-config

这样的话我们就能够确定指向路径了:’/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7’,使用命令sudo vim /usr/local/bin/ipython,将原来的路径”/usr/local/opt/python/bin/python3.6″更换掉即可。
测试

bogon:bin renjie$ python  #python2

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.


bogon:bin renjie$ python3  #python3

Python 3.6.5 (default, Apr 25 2018, 14:23:58) 

[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin

Type "help", "copyright", "credits" or "license" for more information.


bogon:bin renjie$ ipython  #ipython2

Python 2.7.10 (default, Oct  6 2017, 22:29:07) 

Type "copyright", "credits" or "license" for more information.


IPython 5.7.0 -- An enhanced Interactive Python.

?         -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help      -> Python's own help system.

object?   -> Details about 'object', use 'object??' for extra details.


In [1]: 


bogon:bin renjie$ ipython3  #ipython3

Python 3.6.5 (default, Apr 25 2018, 14:23:58) 

Type 'copyright', 'credits' or 'license' for more information

IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.


In [1]: 

很久没写博客了,找找感觉,重回python!

上一篇

发表评论

电子邮件地址不会被公开。 必填项已用*标注