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