1. 程式人生 > >python下的複雜網路程式設計包networkx、matplotlib、numpy安裝及報錯解決方案

python下的複雜網路程式設計包networkx、matplotlib、numpy安裝及報錯解決方案

前言:python和matplotlib 、 numpy、networkx

Python是一種解釋性語言(儘管是高度優化過的),和C或者Fortran相比,這類語言對很多重數值演算法來說執行緩慢。所以或許應該問一問究竟為什麼有這麼多科學家和公司,甚至在高度計算密集型領域內豪賭Python。

答案就是,在Python中很容易把數值計算任務交給下層的C或Fortran擴充套件包。這也正是NumPy和SciPy要做的事情(http://scipy.org/install.html)。在NumPy和SciPy這個組合中,NumPy提供了對高度優化的多維陣列的支援,而這正是大多數新式演算法的基本資料結構。SciPy則通過這些陣列提供了一套快速的數值分析方法庫。最後,用Python來繪製高品質圖形,Matplotlib(

http://matplotlib.org/)也許是使用最方便、功能最豐富的程式庫了。

NetworkX是一個用Python語言開發的圖論與複雜網路建模工具,內建了常用的圖與複雜網路分析演算法,可以方便的進行復雜網路資料分析、模擬建模等

一、NetworkX及Python開發環境的安裝

(1)下載並安裝Python2.7

下載地址

(2)安裝pip和easy_install

這兩個工具是python必備工具。有了他們,各種軟體的安裝不必再親自下載,一句命令即可自動下載安裝。

(3)安裝matplotlib、numpy、networkx

-> 開啟cmd命令列

-> 進入pip目錄:cd c:/Python27/Scripts

-> pip install matplotlib

-> pip install numpy

-> pip install networkx

(4)報錯及解決方案

(a)UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2

原因:編碼錯誤

(b)matplotlib報錯:Python version 2.7 required, which was not found in the registry

原因:版本沒對上,32位python要安裝32位的matplotlib。

(c)報錯:“pip”不是內部或外部命令,也不是可執行的程式。

原因:沒有找到pip.exe所在目錄

解決:

c:\>pip install numpy
'pip' 不是內部或外部命令,也不是可執行的程式
或批處理檔案。

c:\>cd Python34

c:\Python34>pip install numpy
'pip' 不是內部或外部命令,也不是可執行的程式
或批處理檔案。

c:\Python34>cd Scripts

c:\Python34\Scripts>pip install numpy
You are using pip version 6.0.8, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting numpy
  Downloading numpy-1.9.2.tar.gz (4.0MB)
    100% |################################| 4.0MB 57kB/s</span>
解決:切換到python目錄,執行
pip uninstall six ; pip install six
(e)ImportError: matplotlib requires dateutil
pip uninstall dateutil ; pip install dateutil
(f):ImportError: matplotlib requires pyparsing
pip uninstall pyparsing ; pip install pyparsing
如果以上方法仍安裝失敗,則去官網下載壓縮包,解壓縮到Lib\site-packages目錄下,從cmd切換到解壓目錄. 執行python  setup.py install同樣可以安裝成功。 (g)ImportError: No module named 'numpy
pip uninstall numpy ; pip install numpy
(h)error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat).

原因:C環境不匹配,安裝MinGW

解決:

1、下載安裝MinGW,下載地址為:http://sourceforge.net/projects/mingw/files/latest/download?source=files

2、在MinGW的安裝目錄下找到bin資料夾,找到mingw32-make.exe,複製一份更名為make.exe

3、把MinGW的路徑新增到環境變數path中,比如我把MinGW安裝到D:\MinGW\中,就把D:\MinGW\bin新增到path中;

4、在<python安裝目錄>\distutils增加檔案distutils.cfg,在檔案裡輸入

[build]
compiler=mingw32

儲存;

(i) numpy繼續報錯:Symbol不存在。

<span style="font-family:SimSun;font-size:12px;">    ValueError: Symbol table not found

    ----------------------------------------
Command "C:\Python34\python.exe -c "import setuptools, tokenize;__file__='C:\\Users\\zhao\\AppData\\Local\\Temp\\pip-build-vbvzk0v9\\numpy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record C:\Users\zhao\AppData\Local\Temp\pip-dy3ind8_-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\zhao\AppData\Local\Temp\pip-build-vbvzk0v9\numpy</span>

解決:考慮重新安裝numpy。numpy本身沒有64位版本,按照 此帖 指示一步一步先安裝wheel
<span style="font-family:SimSun;font-size:12px;">c:\Python34\Scripts>pip install wheel
Collecting wheel
  Downloading wheel-0.24.0-py2.py3-none-any.whl (63kB)
    12% |████▏                           | 8.2kB 122kB/    19% |██████▏                         | 12kB 180kB    25% |████████▎                       | 16kB 237    32% |██████ ████▍                     | 20kB 2    38% |████████████▍                   | 24kB    45% |██████████████▌                 | 28    51% |████████████████▋               |     58% |██████████████████▋                 64% |███████ █████████████▊             71% |██████████████████████▉         77% |████████████████████████▉     84% |█████████████████████████    90% |█████████████████████████    97% |█████████ ████████████████    100% |█████████████████████████████ ███| 65kB 280kB/s
Installing collected packages: wheel
Successfully installed wheel-0.24.0</span>

然後在加州大學歐文分校下載了64bit、py3.4的numpy。pip install+將檔案拖進cmd,安裝成功!
<span style="font-family:SimSun;font-size:12px;">c:\Python34\Scripts>pip install C:\Users\zhao\Downloads\numpy-1.10.0b1+mkl-cp34-none-win_amd64.whl
Processing c:\users\zhao\downloads\numpy-1.10.0b1+mkl-cp34-none-win_amd64.whl
Installing collected packages: numpy
Successfully installed numpy-1.10.0b1</span>
(j)matplotlib畫圖報錯:AttributeError: 'module' object has no attribute 'plot'、

解決:其實是.pyc檔案存在問題。

問題定位:檢視import庫的原始檔,發現原始檔存在且沒有錯誤,同時存在原始檔的.pyc檔案

問題解決方法:刪除該庫的.pyc檔案,重新執行程式碼;或者找一個可以執行程式碼的環境,拷貝替換當前機器的.pyc檔案即可

pip uninstall matplotlib ; pip install matplotlib
將import matplotlib as plt 改成 import matplotlib.pyplot as plt
--------------------------------------------------------------------------------- 在shell中輸入:

print nx

如果能輸出:

說明Networkx已經安裝好了,可以正常呼叫。