Windows下python的配置
希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了。一开始我希望安装python、手动配置pip并使用pip安装numpy,然而发现实在麻烦;最终我转向了anaconda阵营。
版本选择
主版本
python27 or python33? or python34, python35?
Python版本这么多,搞得各种不统一,真是自己作死。
果断使用Python2.7,python3各种都还不稳定
小版本
python27下有很多小版本,比如我从百度软件上下载的是2.7.6的
果断用最新的,比如python2.7.11,因为新的版本都包含了pip工具,免得自己下载
32位 or 64位
别和我争说64位代表未来。windows系统自身过多超极缓慢,只要NT内核不是纯64bit,我坚决不换64bit的python:python社区各种支持跟不上,比如numpy包,最新的numpy你只能找到32bit的,装64位的python一直报dll错误。
辅助工具
添加PATH
一定要把python本身加入到环境变量PATH中,命令行用来测试,或者当简易计算器:
添加PYTHON变量,值为%d:/soft/python27
添加PYTHON到PATH中:PATH追加一条;%python%
添加pip
最新的python版本已经添加了pip,但是没有添加到path中,手动添加吧:
PATH追加一条;%python%\Scripts
顺便吐槽下windows的环境变量,都特么十几年过去了,系统上软件这么多,都特么往PATH扔执行路径,搞得用户自己添加的变量都超级麻烦,单行根本编辑不下,每次都要拷到notepad中编辑。windows,你改悔吧!
配置pip
首先添加HOME变量到环境变量:用来放置pip的配置文件。当然不配置HOME也OK,那样就会到C:/Users/zchri(我的用户名)下面找配置文件。但是!,我如果重装系统了,这些配置文件就丢失了!我不希望这样,因此把HOME目录配置到D盘,比如:
HOME值为d:/Chris
其次添加pip目录,目录里添加pip.ini文件,文件内容是用来配置pip的源。一句话,直接访问国外网络各种受阻,换国内源吧!内容如下:
[global]
trusted-host=mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
use-wheel=yes
[wheel]
wheel-dir = d:/Chris/pip/packages
添加编译器
蛋疼的python,用pip安装个numpy,非得要VC9.0(vs2008的编译器)来编译。都特么什么年代了,我用vs2010都被同学笑话了,vs2012、vs2013、vs2015都长大了,还用这么老的装机量这么低的破东西。但是没办法只好委屈一下手动安装个vc9.0,美其名曰:Microsoft Visual C++ Compiler for Python 2.7。下载地址:
http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266
安装第三方包
直接输入名字,在线安装
pip install numpy
安装过程中的特点是:非常慢,CPU占用率也很高,风扇不好的一直嗡嗡叫
pip install matplotlib
pip install pandas
pip install sklearn
pip install scipy
哦,fuck,这个scipy依赖于blas什么的,非常麻烦,
下载wheel包,本地安装
pip install xxx.whl
找不到python安装路径
这种问题出现在手动安装numpy/scipy/matplotlib的二进制包的情况。解决办法是执行下述代码(保存到registry.py文件并用python执行):
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __name__ == "__main__":
RegisterPy()
IDE的选择
用Pycharm吧,以前的老版本的,JetBrain家的各种编译器,通用的破解程序:http://xingzhewujiang.com/usr/uploads/2015/07/3741113777.zip, 解压后执行jar文件得到密钥
最新的Pycharm5,可以到http://idea.lanyus.com/注册
记得在File->Settings中开启显示行号,关掉光标闪烁(caret blink),切换代码字体(Microsoft Yahei Mono)
其他
安装scipy过程中发现pip死活装不上,pip不会自行解决依赖问题。SO上解决方法:http://stackoverflow.com/questions/28190534/windows-scipy-install-no-lapack-blas-resources-found
The following link should solve all problems with Windows and SciPy; just choose the appropriate download. I was able to pip install the package with no problems. Every other solution I have tried gave me big headaches.
Source: http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy Command: pip install [Local File Location][Your specific file such as scipy-0.16.0-cp27-none-win_amd64.whl]
This assumes you have installed the following already:
1) Install Visual Studio 2015/2013 with Python Tools (Is integrated into the setup options on install of 2015)
2) Install Visual Studio C++ Compiler for Python Source: http://www.microsoft.com/en-us/download/details.aspx?id=44266 File Name: VCForPython27.msi
3) Install Python Version of choice Source: python.org File Name (e.g.): python-2.7.10.amd64.msi
然而我选择投降,转阵anaconda2:
虽然anaconda2预装了一堆没用的比如node-webkit,但是预装numpy,scipy,以及pip,还是可以的。
如果先前手动安装了python,最好从系统path中去掉python和pip,另外记得在IDE中修改python解释器的路径。