你可能遇到的问题包括:
ImportError: No module named sklearn 未安装sklearn包
ImportError: DLL load failed: 找不到指定的模块
ImportError: DLL load failed: The specified module could not be found

Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
Numpy Install RuntimeError: Broken toolchain: cannot link a simple C program
ImportError: numpy.core.multiarray failed to import
ImportError: cannot import name __check_build
ImportError: No module named matplotlib.pyplot

一. 安装过程

最早使用"pip install scikit-learn"命令安装的Scikit-Learn程序,并没有注意需要安装Numpy、Scipy、Matlotlib,然后在报错"No module named Numpy"后,我接着使用PIP或者下载exe程序安装相应的包,同时也不理解安装顺序和版本的重要性。其中最终都会报错" ImportError: DLL load failed: 找不到指定的模块",此时我的解决方法是:

错误:sklearn ImportError: DLL load failed: 找不到指定的模块
重点:安装python第三方库时总会出现各种兼容问题,应该是版本问题,版本需要一致。

第一步:卸载原始版本,包括Numpy、Scipy、Matlotlib、Scikit-Learn
pip uninstall scikit-learn
pip uninstall numpy
pip uninstall scipy
pip uninstall matplotlib

第二步:不使用"pip install package"或"easy_install package"安装,或者去百度\CSDN下载exe文件,而是去到官网下载相应版本。
http://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy
http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib
http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn

安装过程中最重要的地方就是版本需要兼容。
PS:不推荐使用"pip install numpy"安装或下载如"numpy-MKL-1.8.0.win-amd64-py2.7.exe"类似文件。

第三步:去到Python安装Scripts目录下,再使用pip install xxx.whl安装,先装Numpy\Scipy\Matlotlib包,再安装Scikit-Learn。

其中我的python安装路径"G:\software\Program software\Python\python insert\Scripts",同时四个whl文件安装核心代码:
pip install G:\numpy+scipy+matplotlib\numpy-1.10.2-cp27-none-win_amd64.whl
pip install G:\numpy+scipy+matplotlib\scikit_learn-0.17-cp27-none-win_amd64.whl

 

第四步:此时配置完成,关键是Python64位版本兼容问题和Scripts目录。

二. 测试运行环境

Scikit-Learn是基于python的机器学习模块,基于BSD开源许可。Scikit-learn的基本功能主要被分为六个部分,分类,回归,聚类,数据降维,模型选择,数据预处理,具体可以参考官方网站上的文档。

NumPy(Numeric Python)系统是Python的一种开源的数值计算扩展,一个用python实现的科学计算包。它提供了许多高级的数值编程工具,如:矩阵数据类型、矢量处理,以及精密的运算库。专为进行严格的数字处理而产生。
内容包括:1、一个强大的N维数组对象Array;2、比较成熟的(广播)函数库;3、用于整合C/C++和Fortran代码的工具包;4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。

SciPy (pronounced "Sigh Pie") 是一个开源的数学、科学和工程计算包。它是一款方便、易于使用、专为科学和工程设计的Python工具包,包括统计、优化、整合、线性代数模块、傅里叶变换、信号和图像处理、常微分方程求解器等等。

Matplotlib是一个Python的图形框架,类似于MATLAB和R语言。它是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。

第一个代码:斜线坐标,测试matplotlib

1
2
3
4
5
6
7
8
import matplotlib
import numpy
import scipy
import matplotlib.pyplot as plt
 
plt.plot([1,2,3])
plt.ylabel('some numbers')
plt.show()

运行结果:

第二个代码:桃心程序,测试numpy和matplotlib
代码参考:Windows 下 Python easy_install 的安装 - KingsLanding

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt
 
X = np.arange(-5.0, 5.0, 0.1)
Y = np.arange(-5.0, 5.0, 0.1)
 
x, y = np.meshgrid(X, Y)
f = 17 * x ** 2 - 16 * np.abs(x) * y + 17 * y ** 2 - 225
 
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')
plt.show()

运行结果:

第三个程序:显示Matplotlib强大绘图交互功能
代码参考:Python-Matplotlib安装及简单使用 - bery

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
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import matplotlib.pyplot as plt
  
N = 5
menMeans = (20, 35, 30, 35, 27)
menStd =   (2, 3, 4, 1, 2)
  
ind = np.arange(N)  # the x locations for the groups
width = 0.35        # the width of the bars
  
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
  
womenMeans = (25, 32, 34, 20, 25)
womenStd =   (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd)
  
# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )
  
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
  
def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')
  
autolabel(rects1)
autolabel(rects2)
  
plt.show()

运行结果:

第四个代码:矩阵数据集,测试sklearn

1
2
3
4
from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()
print digits.data

运行结果:

第五个代码:计算TF-IDF词语权重,测试scikit-learn数据分析
参考代码:http://blog.csdn.net/liuxuejiang158blog/article/details/31360765

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
26
27
28
29
30
31
32
33
34
35
36
# coding:utf-8
__author__ = "liuxuejiang"
import jieba
import jieba.posseg as pseg
import os
import sys
from sklearn import feature_extraction
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
 
if __name__ == "__main__":
    corpus=["我 来到 北京 清华大学",     #第一类文本切词后的结果 词之间以空格隔开
        "他 来到 了 网易 杭研 大厦",     #第二类文本的切词结果
        "小明 硕士 毕业 与 中国 科学院",  #第三类文本的切词结果
        "我 爱 北京 天安门"]            #第四类文本的切词结果
 
    #该类会将文本中的词语转换为词频矩阵,矩阵元素a[i][j] 表示j词在i类文本下的词频
    vectorizer=CountVectorizer()
 
    #该类会统计每个词语的tf-idf权值
    transformer=TfidfTransformer()
 
    #第一个fit_transform是计算tf-idf,第二个fit_transform是将文本转为词频矩阵
    tfidf=transformer.fit_transform(vectorizer.fit_transform(corpus))
 
    #获取词袋模型中的所有词语
    word=vectorizer.get_feature_names()
 
    #将tf-idf矩阵抽取出来,元素a[i][j]表示j词在i类文本中的tf-idf权重
    weight=tfidf.toarray()
 
    #打印每类文本的tf-idf词语权重,第一个for遍历所有文本,第二个for便利某一类文本下的词语权重
    for i in range(len(weight)):
        print u"-------这里输出第",i,u"类文本的词语tf-idf权重------"
        for j in range(len(word)):
            print word[j],weight[i][j]

运行结果:

三. 其他错误解决方法

这里虽然讲解几个安装时遇到的其他错误及解决方法,但作者更推荐上面的安装步骤。

在这之前,我反复的安装、卸载、升级包,其中遇到了各种错误,改了又改,百度了又谷歌。常见PIP用法如下:

1
2
3
4
5
6
7
8
* pip install numpy             --安装包numpy
* pip uninstall numpy           --卸载包numpy
* pip show --files PackageName  --查看已安装包
* pip list outdated             --查看待更新包信息
* pip install --upgrade numpy   --升级包
* pip install -U PackageName    --升级包
* pip search PackageName        --搜索包
* pip help                      --显示帮助信息

ImportError: numpy.core.multiarray failed to import
python安装numpy时出现的错误,这个通过stackoverflow和百度也是需要python版本与numpy版本一致,解决的方法包括"pip install -U numpy"升级或下载指定版本"pip install numpy==1.8"。但这显然还涉及到更多的包,没有前面的卸载下载安装统一版本的whl靠谱。

Microsoft Visual C++ 9.0 is required(unable to find vcvarsall.bat)
因为Numpy内部矩阵运算是用C语言实现的,所以需要安装编译工具,这和电脑安装的VC++或VS2012有关,解决方法:如果已安装Visual Studio则添加环境变量VS90COMNTOOLS即可,不同的VS版本对应不同的环境变量值:
Visual Studio 2010 (VS10)设置 VS90COMNTOOLS=%VS100COMNTOOLS%
Visual Studio 2012 (VS11)设置 VS90COMNTOOLS=%VS110COMNTOOLS%
Visual Studio 2013 (VS12)设置 VS90COMNTOOLS=%VS120COMNTOOLS%
但是这并没有解决,另一种方法是下载Micorsoft Visual C++ Compiler for Python 2.7的包。

PS:这些问题基本解决方法使用pip升级、版本一致、重新下载相关版本exe文件再安装。

python安装包问题小结的更多相关文章

  1. Python 解决Python安装包时提示Unable to find vcvarsall.bat的问题

    解决Python安装包时提示Unable to find vcvarsall.bat的问题   by:授客 QQ:1033553122 问题 Python安装包时,提示Unable to find v ...

  2. python安装包的时候报错

    python安装包的时候报错 今天兴致勃勃的安装了一个paramiko包,过程很顺利,但是到结尾的时候报错,这就让人不爽了. 所以呢,需要安装一个名为python-dev的软件包. 该软件包包括头文件 ...

  3. python安装包及批量更新包

    python安装包 # pip安装 pip install pyecharts # 源码安装 - linux git clone https://github.com/pyecharts/pyecha ...

  4. python 安装包总结

    PIL安装(Centos6.6) 1. 安装PIL所需的系统库 (centos6.6)yum install zlib zlib-devel -yyum install libjpeg libjpeg ...

  5. windows下 Python 安装包的配置

    1.下载安装 Pythonpython-2.7.2.msi  http://www.python.org/download/如下载 Python 2.7.2,安装目录为 C:\Python27 2.添 ...

  6. Python 安装包的导入

    1.安装适合的pip python安装pip的命令: python -m pip install --upgrade pip安装Python包,的确是pip最为方便了,简单快捷,因为它直接是从pypi ...

  7. python安装包API文档

    在python开发过程中,经常会使用第三方包,或者内置的包. 那么这些包,具体有哪些选项,有哪些方法,你知道吗?下面介绍一种万能方法. 使用命令:<注意,命令里python显示的API版本是根据 ...

  8. 如何查找python安装包的路径site-packages?

    使用命令: python -m site python -m site --user-site 注意当查看指定版本的python的安装包时,需要指定python版本,比如python2.7.15 -m ...

  9. Python 安装包时选择 python版本

    安装了两个版本的python 其中一个版本为2.7 专门为python 2.7安装包使用的语句为 升级pip E:\Python27\python -m pip install --upgrade p ...

随机推荐

  1. ios7中使用scrollview来横向滑动图片,自动产生偏移竖向的偏移 问题

    ios7中使用scrollview来横向滑动图片,自动产生偏移竖向的偏移 问题     如图红色为scrollview的背景色,在scrollview上加了图片之后,总会有向下的偏移 设置conten ...

  2. XJOI网上同步训练DAY6 T2

    思路:记得FJ省队集训好像有过这题,可是我太弱了,根本不懂T_T #include<cstdio> #include<iostream> #include<cmath&g ...

  3. ASM-51 宏汇编使用手册

    A51与ASM51基本相同   ASM-51 宏汇编主要用来开发Inter8051系列单片机,它具有宏处理,数据处理,列表处理和条件处理等多种功能.源程序的编写完全采用 Inter标准助记符和行格式. ...

  4. qt http 下载文件

    本文章介绍如何利用HTTP从网站上下载文件.在Qt网络编程中,需要用到协议,即HTTP.它是超文本传输协议,它是一种文件传输协议.对于HTTP就不多解释了. 在Qt网络编程中,需要用到协议,即HTTP ...

  5. 【转】Android中BindService方式使用的理解

    原文网址:http://www.cnblogs.com/onlylittlegod/archive/2011/05/15/2046652.html 最近学习了一下Android里面的Service的应 ...

  6. poj2752 Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  7. HBase面试问题

    一.HBase的特点是什么 1.HBase一个分布式的基于列式存储的数据库,基于hadoop的hdfs存储,zookeeper进行管理. 2.HBase适合存储半结构化或非结构化数据,对于数据结构字段 ...

  8. python3-day4(装饰器)

    一.基本 第一波 #### def foo():     print 'foo'   foo     #表示是函数 foo()   #表示执行foo函数   #### 第二波 #### def foo ...

  9. HLS直播和时移项目上线

    兄弟姐妹们: 天翼视讯的高清HLS直播和时移项目上线,测试观看地址为:http://118.85.192.228/live/,同时支持移动终端和pc终端.移动终端支持ios client和androi ...

  10. [RxJS] Changing Behavior with MapTo

    You often need streams to trigger different behaviors on the data based on which streams triggers. T ...