安装Homebrew
Mac最好能安装Homebrew,这样你安装软件会方便得多,当然也可以手工类似Linux下进行编译安装,这个看个人喜好。
1$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
注意:这个命令因github地址调整过一次,因此和大部分文章提到的地址不太一样,可去官网查看https://brew.sh
通过brew安装 Python3.61$ brew install python3
编译安装 Python3.61
2
3
4
5
6
7# 如果没有安装wget的可以直接网络下载
$ wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
$ tar -zxf Python-3.6.1.tgz
$ cd Python-3.6.1
$ ./configure --prefix=/usr/local/python3
$ make && make install
$ sudo chown -R $(whoami):admin /usr/local
替换配置
因Mac OS X 10.11中引入Rootless机制,作用如下:
/System文件夹下的所有文件都不能被苹果应用以外的程序修改(例如各种安装器和升级部件)
当前的API例如task_for_pid不能在系统进程下被调用了。这意味着以前注入系统进程(Finder、Messages或者系统内核)的程序都不能用了。
rootless依然允许已签名的KEXT内核拓展被载入。而且KEXT可以进行许多无限制的系统及操作。
所以我们不能直接修改/System文件夹下的所有文件, 如果是在OS X 10.11系统下,这里需要先将这个机制关掉。
关闭
重启电脑, 重启过程中按住command+R, 进入恢复模式
选择任意语言,进入选择磁盘界面
在顶部状态栏打开terminal,键入: csrutil disable
重启电脑
开启
重启电脑, 重启过程中按住command+R, 进入恢复模式
选择任意语言,进入选择磁盘界面
在顶部状态栏打开terminal,键入: csrutil enable
重启电脑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25# 通过brew安装的情况下操作如下
$ sudo mv /Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions
# 通过编译安装的执行
$ sudo /usr/local/python3 /System/Library/Frameworks/Python.framework/Versions/3.6
$ sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/3.6
# 更新Current
$ sudo rm /System/Library/Frameworks/Python.framework/Versions/Current
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6 /System/Library/Frameworks/Python.framework/Versions/Current
# 删除系统原有执行文件
$ sudo rm /usr/bin/pydoc
$ sudo rm /usr/bin/python
$ sudo rm /usr/bin/pythonw
$ sudo rm /usr/bin/python-config
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/pydoc3.6 /usr/bin/pydoc
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /usr/bin/python
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/pythonw3.6 /usr/bin/pythonw
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config /usr/bin/python-config
$ echo "PATH='/System/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}'
export PATH" >> ~/.bash_profile
$ python
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
遇到的问题
pip安装包出现SSL module缺失1
2$ pip install pandas
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not availabl
原因:Apple has deprecated use of the system-supplied OpenSSL libraries and with the latest releases no longer supply the header files needed to build with them. They are very old anyway. So, you need to supply a version of them. The easiest way is to get them from a third-party package manager like Homebrew or MacPorts but you certainly can download an OpenSSL source release from https://www.openssl.org and build the libraries themselves. If you do not have administrator access, you will probably need to modify the OpenSSL build using at least –prefix to install to a non-system location and then rerun Python’s ./configure with CFLAGS and LDFLAGS pointing to the installed location of your OpenSSL.
(大致意思就是说,Apple已经在高版本系统中移除了系统提供的OpenSSL库,新版本不再提供与它们一起构建所需的头文件
1
2
3$ export CFLAGS="-I/usr/local/opt/openssl/include"
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ pip install pandas
然后卸载pkg包即可。
dtype(‘float64’) to dtype(‘int64’)原因: Python3.x不向下兼容出现TypeError: Cannot cast ufunc subtract output from dtype(‘float64’) to dtype(‘int64’)
解决方案:将数据进行astype(“float64”)转换
如 ARMA(data, order = (p, q)) 转换为
ARMA(data.astype(“float64”), order = (p, q))
‘float’ object cannot be interpreted as an integer原因:Python3.x 因为出现类似
for i in range(100 / 10)的情况 ,(100 / 10) 得到10.0,如果参与循环就会出现问题。
解决方案:将代码修改为:
for i in range(100 // 10) 既可避免
如果后面遇到问题继续补充!
(The End)