您的位置:

pythonwordcloud库安装失败:可能的原因及解决方案

一、错误描述

当我们使用pip install pythonwordcloud命令进行pythonwordcloud库的安装时,很可能会遇到安装失败的情况,提示信息可能如下所示:

ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2gsnrh17/pythonwordcloud/setup.py'"'"'; __file__='"'"'/tmp/pip-install-2gsnrh17/pythonwordcloud/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-lgz5mny4/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/ubuntu/.local/include/python3.6m/python3.6m
         cwd: /tmp/pip-install-2gsnrh17/pythonwordcloud/
    Complete output (26 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    copying wordcloud_cli.py -> build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/wordcloud.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/stopwords -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/__init__.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/tokenization.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/color_from_image.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/__main__.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/query_integral_image.py -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/compat.py -> build/lib.linux-x86_64-3.6/wordcloud
    running egg_info
    writing wordcloud.egg-info/PKG-INFO
    writing dependency_links to wordcloud.egg-info/dependency_links.txt
    writing requirements to wordcloud.egg-info/requires.txt
    writing top-level names to wordcloud.egg-info/top_level.txt
    reading manifest file 'wordcloud.egg-info/SOURCES.txt'
    reading manifest template 'MANIFEST.in'
    writing manifest file 'wordcloud.egg-info/SOURCES.txt'
    copying wordcloud/stopwords -> build/lib.linux-x86_64-3.6/wordcloud
    copying wordcloud/fonts/SourceCodePro-Bold.ttf -> build/lib.linux-x86_64-3.6/wordcloud/fonts
    copying wordcloud/fonts/Inconsolata-Regular.ttf -> build/lib.linux-x86_64-3.6/wordcloud/fonts
    copying wordcloud/fonts/SourceCodePro-ExtraLight.ttf -> build/lib.linux-x86_64-3.6/wordcloud/fonts
    copying wordcloud/fonts/Inconsolata-Bold.ttf -> build/lib.linux-x86_64-3.6/wordcloud/fonts
    running build_ext
    building 'wordcloud.query_integral_image' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/wordcloud
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/ubuntu/.local/lib/python3.6/site-packages/numpy/core/include -I/usr/include/python3.6m -c wordcloud/query_integral_image.c -o build/temp.linux-x86_64-3.6/wordcloud/query_integral_image.o
    In file included from wordcloud/query_integral_image.c:206:0:
    /home/ubuntu/.local/lib/python3.6/site-packages/numpy/core/include/numpy/npy_deprecated_api.h:11:2: warning: #warning "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
    #warning "Using deprecated NumPy API, disable it by " \
      ^
    wordcloud/query_integral_image.c:207:10: fatal error: Python.h: 没有那个文件或目录
     #include "Python.h"
              ^~~~~~~~~~
    编译中断。
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

从上述提示信息中,我们可以看到编译中断了,并且提示了一个错误:没有Python.h文件或目录,这是导致安装pythonwordcloud库失败的原因之一。

二、可能的原因及解决方案

1. 缺失Python.h文件或目录

Python.h是Python语言的头文件,如果系统中缺少Python.h文件或目录,则会导致编译失败。为了解决这个问题,我们需要先安装python3-dev或者python3-devel依赖。在ubuntu/debian系统中可以通过以下命令进行安装:

sudo apt-get install python3-dev

在CentOS/Redhat系统中可以通过以下命令进行安装:

sudo yum install python3-devel

2. 安装依赖未能顺利完成

pythonwordcloud库安装的时候需要依赖PIL库和numpy库,如果安装依赖未能顺利完成,也会导致pythonwordcloud库安装失败。

为了解决这个问题,我们可以先安装PIL库和numpy库,然后再进行pythonwordcloud库的安装。在ubuntu/debian系统中可以通过以下命令进行安装:

sudo apt-get install python3-pil python3-numpy

在CentOS/Redhat系统中可以通过以下命令进行安装:

sudo yum install python3-pillow python3-numpy

3. 安装过程中网络不够稳定

由于pythonwordcloud库需要从互联网上下载依赖文件,因此安装的过程中需要保证网络的稳定性。如果网络不够稳定,可能会导致文件下载失败,从而导致pythonwordcloud库安装失败。

为了解决这个问题,我们可以尝试多次安装pythonwordcloud库,如果还是失败了,可以手动下载相关依赖文件,然后再进行安装。

三、小结

以上就是pythonwordcloud库安装失败的可能原因及解决方案。在实际开发中,要想保证pythonwordcloud库能够正常使用,我们需要仔细排查错误并采取相应的解决措施。希望本文能够给大家在使用pythonwordcloud库的过程中提供一些帮助。