文章出處

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解釋器的路徑。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()