python3安裝word2vec模塊錯誤處理


    申請了博客以后都1年多了,一個博客也沒發,今天突然起了這個念頭,記錄一下今天上午處理的一個問題。

    一個朋友想使用word2vec來進行一些分詞方面的工作,但是安裝了之后爆出很多錯誤,拜托我幫個忙。研究了一個多小時,大致搞清楚了前因后果,把這個過程記錄下來。至於為什么不是gensim來做分詞,那又是另外一件事情,前因后果這里就不說了。

 系統環境: windows7,python3.6

 問題一: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 24: invalid continuation byte

 
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\ basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\commands\ install.py", line 342, in run
    prefix=options.prefix_path,
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\req\req_ set.py", line 784, in install
    **kwargs
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\req\req_ install.py", line 878, in install
    spinner=spinner,
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
    line = console_to_str(proc.stdout.readline())
  File "C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\compat\__init__.py", line 75, in console_to_str
    return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 24: invalid continuation byte
  使用pip install word2vec的方式安裝模塊,第一個出現的問題就是這個UnicodeDecodeError,解碼錯誤。看了一下,是文件C:\ProgramData\Anaconda2\envs\python36\Lib\site-packages\pip\compat\__init__.py里的return s.decode("utf-8")報錯
  處理過程:1.直接打開這個文件,找到對應的語句return s.decode("utf-8")。使用chardet.detect函數檢查變量的s的編碼,得到的結果是“GB2312”。
       2.給return s.decode("utf-8")這句話加一個try...except,  except語句塊中加入return s.decode("gbk").
  修改之后如下圖:
if sys.version_info >= (3,):
    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            try:
                return s.decode('utf_8')
            except UnicodeDecodeError:
                return s.decode('gbk')

  再執行命令pip install word2vec, 編碼問題沒有了,可是又出現新的問題。

  問題2:系統找不到指定文件

Complete output from command C:\ProgramData\Anaconda2\envs\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-build-jpsvn2ro\\w
c\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\ADMINI~1\AppData\Loca
\pip-a2480mto-record\install-record.txt --single-version-externally-managed --compile:
running install
Compilation command: gcc C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-jpsvn2ro\word2vec\word2vec\c\win32/word2vec.c -o Scripts\word2vec.exe -O2 -Wall -funroll-loops
error: [WinError 2] 系統找不到指定的文件。

  仔細觀察了一下,發現其實是因為要執行編譯命令 gcc  C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-jpsvn2ro\word2vec\word2vec\c\win32/word2vec.c -o Scripts\word2vec.exe -O2 -Wall -funroll-loops,才出現錯誤。而這個gcc命令,在windows上是沒有的,需要安裝相應的軟件。
  處理過程:
    1. 下載一個帶有C++編譯器的軟件,並且這個編譯器的版本要足夠。我這里是4.9.2。我下載的是DEV-CPP這個軟件,下載好后直接安裝。
    2. 軟件安裝好后分別設置PATH,INCLUDE,LIB這三個環境變量。再檢查有無安裝成功。
    配置環境變量可以參考https://www.cnblogs.com/chenwu128/archive/2012/05/21/2512116.html
  接着繼續執行pip安裝命令,剛才的gcc命令不再是問題了,但是又出現新的錯誤
 

  問題3: fatal error: win32-port.h: No such file or directory

Complete output from command C:\ProgramData\Anaconda2\envs\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-build-21ut_hht\\word
c\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\ADMINI~1\AppData\Local\T
\pip-m1frjwvp-record\install-record.txt --single-version-externally-managed --compile:
running install
Compilation command: gcc C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-21ut_hht\word2vec\word2vec\c\win32/word2vec.c -o Scripts\word2vec.exe -O2 -Wall -funroll-loops
C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-21ut_hht\word2vec\word2vec\c\win32/word2vec.c:21:25: fatal error: win32-port.h: No such file or directory
# include "win32-port.h"
^
compilation terminated.

----------------------------------------
mand "C:\ProgramData\Anaconda2\envs\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-build-21ut_hht\\word2vec\\setup.py';f=getatt
okenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\ADMINI~1\AppData\Local\Temp\pip-m1frjwvp-record\
tall-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\pip-build-21ut_hht\word2vec\

  上面錯誤信息的大意是,在編譯的過程中發現include "win32-port.h"這個導入操作沒有成功,沒有這個文件。

  到pypi網站的word2vec模塊的下載頁面下去看,發現一個github地址https://pypi.python.org/pypi/word2vec,上面有這個模塊的源碼。進去之后看說明發現windows安裝該模塊還需要win32-port,原話是:Windows: Very experimental support based this win32 port,

點擊進入win32_port的開源項目,下載win32-port的zip包。

  處理過程:

    1. 下載一個word2vec的程序包,可以在pypi上下載,也可以在github上下載。因為需要改變安裝包中的內容,我們需要換一種安裝方式;

    2. 下載一個win32-port zip包。

    3. 將win32-port中的所有內容拷貝到word2vec模塊的word2vec-0.9.2\word2vec\c\win32這個目錄下;

    4. 再到word2vec模塊的文件夾下使用python setup.py install 來安裝。

  接下來就沒有新問題了。

  到python3的idle里,

>>> import word2vec
>>> print(word2vec.__version__)
0.9.2
>>> 

   安裝成功! 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM