numpy、scipy、matplotlib、OpenCV、sklearn安裝及問題解決

1 numpy 概述

numpy是Numerical Python的縮寫,釋義爲數值的Pythonhtml

numpy彌補了做爲通用編程語言的Python在數值計算方面能力弱、速度慢的不足(numpy的底層是經過C語言編寫的甚至彙編語言實現的,能夠直接操做CPU)html5

numpy擁有豐富的數學函數、具備矢量運算能力,快速、節省空間,,強大的多維數組(矩陣運算)和優異的運算能力,此外也針對數組運算提供大量的數學函數庫python

numpy的存儲效率和I/O性能優勝於pythongit

numpy的模塊是用C語言編寫的,故不支持jythongithub

2 numpy 安裝

2.1 在線安裝

python -m pip install numpy 或 pip install numpyweb

該種方式安裝的是標準的numpy,當實際使用過程當中除了安裝標準的numpy外還有另外的組件——數學核心庫mkl(inter(R) Math Kernel Library),即numpy + mkl算法

2.2 離線安裝(一)

離線安裝:pip install 路徑+文件名sql

C:\WINDOWS\system32>pip install D:\numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whlshell

出現報錯:numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.編程

 

查找緣由嘗試了一遍,問題依舊就有解決

>>> import pip
>>> print(pip.pep425tags.get_supported())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pip' has no attribute 'pep425tags'

顯示pip中沒有pep425tags屬性

此時查看pip幫助

>>> help(pip)
Help on package pip:

NAME
    pip

PACKAGE CONTENTS
    __main__
    _internal (package)
    _vendor (package)

VERSION
    10.0.1

FILE
    c:\users\z***g\appdata\local\programs\python\python36-32\lib\site-packages\pip\__init__.py

 

該pip package 中有package contents :_internal(package)和_vendor(package) ;

確實沒有pep425tags,繼續查看package

>>> help(pip._internal)
Help on package pip._internal in pip:

NAME
    pip._internal

PACKAGE CONTENTS
    basecommand
    baseparser
    build_env
    cache
    cmdoptions
    commands (package)
    compat
    configuration
    download
    exceptions
    index
    locations
    models (package)
    operations (package)
    pep425tags
    req (package)
    resolve
    status_codes
    utils (package)
    vcs (package)
    wheel

SUBMODULES
    bazaar
    deprecation
    git
    mercurial
    subversion

FUNCTIONS
    autocomplete()
        Command and option completion for the main option parser (and options)
        and its subcommands (and options).

        Enable by sourcing one of the completion shell scripts (bash, zsh or fish).

    check_isolated(args)

    create_main_parser()

    main(args=None)

    parseopts(args)

DATA
    absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...
    commands_dict = {'check': <class 'pip._internal.commands.check.CheckCo...
    logger = <Logger pip._internal (WARNING)>

VERSION
    10.0.1

FILE
    c:\users\zyong\appdata\local\programs\python\python36-32\lib\site-packages\pip\_internal\__init__.py

咱們明顯看到了_internal package中有 pep425tags 屬性

咱們繼續查看_vendor package

>>> help(pip._vendor)
Help on package pip._vendor in pip:

NAME
    pip._vendor

DESCRIPTION
    pip._vendor is for vendoring dependencies of pip to prevent needing pip to
    depend on something external.

    Files inside of pip._vendor should be considered immutable and should only be
    updated to versions from upstream.

PACKAGE CONTENTS
    appdirs
    cachecontrol (package)
    certifi (package)
    chardet (package)
    colorama (package)
    distlib (package)
    distro
    html5lib (package)
    idna (package)
    ipaddress
    lockfile (package)
    msgpack (package)
    packaging (package)
    pkg_resources (package)
    progress (package)
    pyparsing
    pytoml (package)
    requests (package)
    retrying
    six
    urllib3 (package)
    webencodings (package)

FUNCTIONS
    vendored(modulename)
        # Define a small helper function to alias our vendored modules to the real ones
        # if the vendored ones do not exist. This idea of this was taken from
        # https://github.com/kennethreitz/requests/pull/2567.

DATA
    DEBUNDLED = False
    WHEEL_DIR = r'C:\Users\zyong\AppData\Local\Programs\Python\Python36-32...
    absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...

FILE
    c:\users\zyong\appdata\local\programs\python\python36-32\lib\site-packages\pip\_vendor\__init__.py

基於此,能夠發現這多是因爲pip版本問題而產生的,依據pip 10.0.1 進行以下操做

>>> import pip._internal
>>> print(pip._internal.pep425tags.get_supported())
[('cp36', 'cp36m', 'win32'), ('cp36', 'none', 'win32'), 
('py3', 'none', 'win32'), ('cp36', 'none', 'any'), 
('cp3', 'none', 'any'), ('py36', 'none', 'any'), 
('py3', 'none', 'any'), ('py35', 'none', 'any'), 
('py34', 'none', 'any'), ('py33', 'none', 'any'),
 ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')]
>>>

從上述內容能夠看到,本pip並不支持numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl,

能夠從python插件中找到32位numpy+mklhttps://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql

也即numpy-1.14.3+mkl-cp36-cp36m-win32.whl

在命令提示符CMD中執行:

C:\WINDOWS\system32>pip install D:\Software\Python\numpy-1.14.3+mkl-cp36-cp36m-win32.whl
Processing d:\software\python\numpy-1.14.3+mkl-cp36-cp36m-win32.whl
Installing collected packages: numpy
Successfully installed numpy-1.14.3+mkl

安裝成功!

2.3 離線安裝(二)

手動將whl改成zip後,將解壓後的numpy,numpy-1.11.0.dist-info兩個文件夾放入Python/Lib/site-packages中便可。
 
備註:pyCharm中File-Settings-Project:[Tutorial]-Projext Interperter中確認。[Tutorial]爲工程文件名,根據實際狀況發生改變。
該方法來源:http://blog.sina.com.cn/s/blog_c26084b90102x2k0.html
 
該方法僅驗證瞭解壓後的文件夾,沒有繼續嘗試。
 
固然也有人依據pip支持形式對numpy+mkl文件名進行修改,也成功安裝,暫何嘗試。詳見 https://blog.csdn.net/yxpyxp12341234/article/details/70436854
 
 
pip升級:
PS C:\Users\zyong\Desktop> python -m pip install -U pip
Collecting pip
  Downloading https://.../pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 81bytes/s
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

 

 

3 scipy

Scipy是一個高級的科學計算庫,它和Numpy聯繫很密切;

Scipy通常都是操控Numpy數組來進行科學計算,因此能夠說是基於Numpy之上了。

Scipy有不少子模塊能夠應對不一樣的應用,例如插值運算,優化算法、圖像處理、數學統計等。

3.1 安裝

SciPy is software for mathematics, science, and engineering.
Install numpy+mkl before installing scipy(在安裝scipy前必須先安裝numpy + mkl).

下載連接 https://www.lfd.uci.edu/~gohlke/pythonlibs/#scipy

安裝:

PS D:\Software\Python> pip install scipy-1.1.0-cp36-cp36m-win32.whl

如今已經更新至:numpy-1.14.5+mkl-cp37-cp37m-win_amd64.whl

4 matplotlib

下載網站:https://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

安裝:PS D:\Software\Python> pip install matplotlib-2.2.2-cp36-cp36m-win32.whl

5 在windows中使用 Windows PowerShell的方式

按住shift ,而後右擊選擇 「在此處打開PowerShell窗口

 

 


 

2018年6月8日早追記

使用上述方法1是能夠安裝上的,可是在運行numpy時會報錯,例如使用numpy.arange函數時,報錯:AttributeError: 'module' object has no attribute 'arange'

在網上找不到緣由,後來在虛擬機上嘗試,能夠正常運行。找緣由!

緣由追記!

(1)在運行numpy.arange(),報錯:AttributeError: 'module' object has no attribute 'arange',此時懷疑多是版本的差別形成的,遂嘗試numpy.array();

(2)嘗試運行numpy.array()時,也一樣報出沒有array這個屬性值,

(3)help(numpy),結果內容太多,查詢很是困難,同時猜測這arange 、array 兩個都是最基本的函數都沒有,多是在裝numpy庫時,沒有同時載入arange、array函數。

(4)當時裝的時候,安裝的是numpy-1.14.3+mkl-cp36-cp36m-win32.whl,而不是numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl,個人電腦又是64位的,因此先win32位的卸載,安裝64位,

C:\WINDOWS\system32>pip uninstall D:\Software\Python\numpy-1.14.3+mkl-cp36-cp36m-win32.whl
Uninstalling numpy-1.14.3+mkl:
  Would remove:
    c:\users\zyong\appdata\local\programs\python\python36-32\lib\site-packages\numpy-1.14.3+mkl.dist-info\*
    c:\users\zyong\appdata\local\programs\python\python36-32\lib\site-packages\numpy\*
    c:\users\zyong\appdata\local\programs\python\python36-32\scripts\f2py.py
Proceed (y/n)? y
  Successfully uninstalled numpy-1.14.3+mkl

備註:卸載時是將 install 替換成 uninstall 。最後章節會講述安裝的庫卸載方法。

(5)以後安裝win_amd64.whl,但是pip 不支持 安裝,pip支持的列表以下:

>>> import pip._internal
>>> print(pip._internal.pep425tags.get_supported())
[('cp36', 'cp36m', 'win32'), ('cp36', 'none', 'win32'), 
('py3', 'none', 'win32'), ('cp36', 'none', 'any'), 
('cp3', 'none', 'any'), ('py36', 'none', 'any'), 
('py3', 'none', 'any'), ('py35', 'none', 'any'), 
('py34', 'none', 'any'), ('py33', 'none', 'any'),
 ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')]

(6)依據 pip安裝報錯:is not a supported wheel on this platform 內容中的說法,將numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl修改成numpy-1.14.3+mkl-cp36-none-win_amd64.whl,此時出現下列內容

C:\WINDOWS\system32>pip install D:\Software\Python\numpy-1.14.3+mkl-cp36-none-win_amd64.whl
numpy-1.14.3+mkl-cp36-none-win_amd64.whl is not a supported wheel on this platform.

(7)不支持狀況下,繼續修改,將numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl 修改成 numpy-1.14.3+mkl-cp36-none-any.whl

安裝結果:

C:\WINDOWS\system32>pip install D:\Software\Python\numpy-1.14.3+mkl-cp36-none-any.whl
Processing d:\software\python\numpy-1.14.3+mkl-cp36-none-any.whl
Installing collected packages: numpy
Successfully installed numpy-1.14.3+mkl

顯示成功了!

(8)查看安裝效果

C:\WINDOWS\system32>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
    from . import multiarray
ImportError: cannot import name 'multiarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\__init__.py", line 142, in <module>
    from . import add_newdocs
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "C:\Users\zyong\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
    raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: cannot import name 'multiarray'

報錯!

(9)此時想 pip 多是32 形成的,也就意味着python解釋器,顯示32位,遂將32位python解釋器卸載,從新下載安裝python 64位解釋器。

(10)安裝完python解釋器(64位)後,查看pip所支持的類型;

C:\WINDOWS\system32>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip._internal
>>> print(pip._internal.pep425tags.get_supported())
[('cp36', 'cp36m', 'win_amd64'), ('cp36', 'none', 'win_amd64'),
('py3', 'none', 'win_amd64'), ('cp36', 'none', 'any'), ('cp3', 'none', 'any'),
('py36', 'none', 'any'), ('py3', 'none', 'any'), ('py35', 'none', 'any'),
('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'),
('py31', 'none', 'any'), ('py30', 'none', 'any')]
>>>

能夠看出,64位python解釋器是支持cp36 win_amd64的,直接安裝輪子

(11)安裝輪子,因pip版本較低,須要更新pip版本狀態;詳細過程以下

C:\WINDOWS\system32>pip install D:\Software\Python\numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl
Processing d:\software\python\numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.14.3+mkl
You are using pip version 9.0.3, however version 10.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\WINDOWS\system32>python -m pip install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
Using cached https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-10.0.1

C:\WINDOWS\system32>pip install D:\Software\Python\numpy-1.14.3+mkl-cp36-cp36m-win_amd64.whl
Requirement already satisfied: numpy==1.14.3+mkl from file:///D:/Software/Python/numpy-1.14.3%2Bmkl-cp36-cp36m-win_amd64.whl in c:\users\zy***g\appdata\local\programs\python\python36\lib\site-packages (1.14.3+mkl)

其實,爲了省事(因基本緣由已基本確認了,因此也就不查看pip所支持的格式了),直接安裝輪子便可。

(12)小插曲:安裝完成後,使用PyCharm時,出現報錯;詳細示例見 pycharm運行報錯unable to load the file system codec ImportError ?

Fatal Python error: Py_Initialize: unable to load the file system codec
ImportError: No module named 'encodings'

Current thread 0x000033c4 (most recent call first):

嘗試修改解釋器路徑後,沒有效果,只得將其卸載。從新安裝。

(13)可使用import numpy及相關函數驗證是否正確安裝。例如

import numpy as np
print(np.random.rand(4,4))

運行後代碼

[[0.985837   0.09206095 0.79042019 0.26074964]
 [0.23293134 0.61763471 0.02468984 0.29329917]
 [0.89250718 0.94265628 0.10534024 0.49142388]
 [0.39379788 0.74113902 0.88442658 0.12648295]]

 

至此,numpy庫已經所有安裝完畢

 

-----------------------------------------2018.09.01更新---------------------------------------------------------

6 安裝OpenCV

安裝OpenCV時可能出現錯誤,因此能夠先卸載OpenCV,更新numpy,而後在安裝新版本OpenCV。

在安裝過程當中出現了 「 ModuleNotFoundError: No module named 'cv2」 ,一直報錯,覺得是opencv安裝錯誤,反覆卸載安裝,後來下載sublime text 3,並用(Sublime Text3 註冊碼激活碼(持續更新中2018-6-29),博主地址 https://blog.csdn.net/qq_29819449)激活了,運行一樣的代碼,運行成功!

安裝過程出現問題參考:

Opencv試用備忘

ImportError: No module named cv2 報錯處理

python導入opencv解決no module named cv2問題 這個貌似安裝後文件 *.pyd 就在Python文件中

No module named 'cv2'出錯

2017 windows python 安裝numpy+mkl+scipy+scikit-learn

解決RuntimeError:module compiled against API version 9 but this version of numpy is 7

國內鏡像:pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

 

==========2019年01月01日 更新===================

7 查看python及相關庫位置、版本

7.1 查看python位置及版本

7.1.1 查看python的位置

1 打開cmd

win + R  -->  cmd ,回車確認,

2 where python

3 進入 python 所在的文件夾(延伸,爲python庫打基礎

方法一:

cd +(絕對)路徑

方法二:

將路徑粘貼在文件地址欄,直接進入python.exe所在的文件中

再shift + 鼠標右鍵,-->  在此處打開powershell窗口便可。

7.1.2 查看 python 版本

打開 cmd --> 輸入 python 

我用的python是 3.7.2rc1

7.2 查看 python 庫

7.2.1 查看安裝庫

依據 7.1 章節 \ 7.1.1 \ 3 中的方法打開 python.exe 文件所在文件夾中的 CMD

輸入下列代碼中的第 1 行 或 第 3 行

1 pip list
2 # or
3 pip freeze

7.2.2 查看全部過時的庫

pip list --outdated

界面以下

7.2.3 將過時庫升級

pip install --upgrade 庫名

備註:此命令不支持全局所有庫升級。

 另:

DOS命令行下輸入python進入python環境,而後利用下面python代碼實現批量更新工具庫

import pip
from subprocess import call
for dist in pip.get_installed_distributions():
    call("pip install --upgrade " + dist.project_name, shell=True)

利用pip批量更新python庫

如何查看Python 安裝位置以及已經安裝的庫

 

8 庫文件卸載

在以上章節用雖然涉及到卸載,可是安裝後馬上卸載將 install 改成 unstall 卻是能夠,若是關閉 命令提示符 後,那種方法就不適用了。

一、打開命令提示符

二、輸入

pip list

此時能夠看到全部安裝庫及版本號。

三、找到所需安裝的庫名。

好比安裝的opencv,若是直接(❌)

pip uninstall opencv

這樣就會失敗,由於在庫列表中沒有 opencv 這個庫名,而是 opencv-python 庫名存在的。

應該輸入(✔)

pip uninstall opencv-python

四、卸載成功

C:\Users>pip uninstall opencv-python
Uninstalling opencv-python-4.0.1:
  Would remove:
    c:\users\appdata\local\programs\python\python36\lib\site-packages\cv.py
...

Proceed (y/n)? y
  Successfully uninstalled opencv-python-4.0.1

 

 9 sklearn 的安裝

9.1 安裝過程的誤會

安裝第三方庫,首先想到的是在 Anacanda 中尋找相關的庫,發現沒有,再去 Python擴展包的非官方Windows二進制文件 中搜索庫文件,結果發現,有兩個

肯定下載參數,選擇目標下載安裝。

import sklearn

失敗。

後來發現緣由是 sklearn 非 sklearn,而是 scikit-learn,引用庫文件方式則是  import sklearn 

9.2 正確安裝方式

明確兩件事

  • 1 正確的庫名 scikit-learn

  • 2 該庫的依賴庫

參考:

GitHub 上的內容 scikit-learn https://github.com/scikit-learn/scikit-learn

scikit-learn:Python中的機器學習 https://scikit-learn.org

 

從安裝可知,scikit-learn 的依賴庫爲 numpy 和 scipy,安裝scikit-learn前請確保 numpy 和 scipy 已安裝。

 安裝方法1

activate tf2.0
pip install -U scikit-learn
# conda install scikit-learn

安裝方法2

直接在 Anaconda 中安裝

安裝方法3

如今第三方庫中下載,而後本地安裝

相關文章
相關標籤/搜索