安装pygraphviz时出现"找不到'libcgraph'包"的错误

72

我成功安装了graphvizcgraph

$ sudo pip install graphviz
....
Successfully installed graphviz-0.5.1

$ sudo pip install cgraph
...
Successfully installed cgraph-0.1

在运行sudo pip install pygraphviz时,我遇到了问题No package 'libcgraph' found。以下是完整的堆栈跟踪。

$ sudo pip install pygraphviz
The directory '/Users/sparkandshine/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/sparkandshine/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pygraphviz
  Downloading pygraphviz-1.3.1.zip (123kB)
    100% |████████████████████████████████| 133kB 1.5MB/s 
Installing collected packages: pygraphviz
  Running setup.py install for pygraphviz ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-qfFpFG/pygraphviz/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-JmwjA6-record/install-record.txt --single-version-externally-managed --compile:
    running install
    Trying pkg-config
    Package libcgraph was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libcgraph.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libcgraph' found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/tmp/pip-build-qfFpFG/pygraphviz/setup.py", line 87, in <module>
        tests_require=['nose>=0.10.1', 'doctest-ignore-unicode>=0.1.0',],
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 151, in setup
        dist.run_commands()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "setup_commands.py", line 44, in modified_run
        self.include_path, self.library_path = get_graphviz_dirs()
      File "setup_extra.py", line 121, in get_graphviz_dirs
        include_dirs, library_dirs = _pkg_config()
      File "setup_extra.py", line 44, in _pkg_config
        output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
        raise CalledProcessError(retcode, cmd, output=output)
    subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-qfFpFG/pygraphviz/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-JmwjA6-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-qfFpFG/pygraphviz/

我尝试了Python does not see pygraphviz提供的解决方案,但没有成功。


你使用的是什么操作系统? - Moses Koledoye
@MosesKoledoye,macOS Sierra版本10.12 - SparkAndShine
1
尝试运行 brew install graphviz-develbrew install pkg-config - Moses Koledoye
6个回答

120
sudo apt-get install python-dev graphviz libgraphviz-dev pkg-config

如果您使用的是Python 3,则可能需要以下内容:

sudo apt-get install python3-dev graphviz libgraphviz-dev pkg-config

然后

pip install pygraphviz

1
我发现我需要先执行 sudo apt-get install python-dev,否则在执行 pip install pygraphviz 时会出现一个关于缺少 Python.h 的编译错误。 - Brian Stormont
1
我已经更新了python-dev的要求,以防有人遇到类似的问题。谢谢。 - Dhiraj Dhule
在第一步中,您可以指定您想要的Python版本: 例如:sudo apt-get install python3.6-dev。 默认版本是2.7。另外,在第二步中使用sudo会导致pip导入错误,但是去掉它可以解决问题并安装pygraphviz。 - rer
只是一个小提醒。运行“sudo pip…”可能不是个好主意。最好把pip作为虚拟环境的一部分,但如果没有,那就像普通用户一样运行pip。请参阅https://dev59.com/oWEi5IYBdhLWcg3wq93k。 - Harlin
似乎“graphviz”本身并不是必需的。 - Skippy le Grand Gourou
@myself 嗯,尽管没有明确的依赖关系,但实际上是这样的 - Skippy le Grand Gourou

45

对于MacOS,我需要做:

$ brew install graphviz
$ sudo pip install pygraphviz 

4
我在使用Python 3和virtualenv,并且无需额外选项就可以使用pip install graphviz,并且安装成功(在执行了brew install graphviz之后)。 - Trenton
哦,太棒了。非常感谢。这对我来说完美无缺。你救了我的一天,也避免了我被踢出去的尴尬。 :) - Jyotirmay
根据@Trenton的说明 - 在我运行brew install graphviz之后,我发现所有要求都在pip install graphviz上得到满足,请注意,brew install会在字体方面暂停很长时间。 - Rob
不要使用“sudo pip”!相反,请使用“pip install --user”,因为没有理由在系统发行版中安装它。你有可能会陷入错误状态。 - Jakob Guldberg Aaes

15

对于 Fedora 用户:

  1. 运行命令:yum list available graphviz*
  2. 选择适合你的 Fedora 版本的软件包
  3. 运行命令:sudo yum install 'graphviz-devel.x86_64'
  4. 运行命令:pip install pygraphviz

1
我遵循了这个路径来安装CentOS,并且成功了。 - Jersey_Guy
我实际上遵循了这个,但我发现它与你的非常相似!谢谢。 https://github.com/pygraphviz/pygraphviz/issues/86 - Chesco Igual

6
在 macOS 上,我用了以下方法:
$ brew install graphviz
$ sudo pip install pygraphviz

对我没用。在安装了graphviz之后,我必须下载pygraphviz的git项目,并使用sudo python3 setup.py install --include-path=/usr/local/Cellar/graphviz/2.40.1/include/graphviz --library-path=/usr/local/Cellar/graphviz/2.40.1/lib - Itay

2

对于CentOS用户。 为了解决这个问题,需要安装graphviz-devel软件包。您需要使用yum命令来安装此软件包。为此,您需要添加软件包可用的仓库。在我这里,Atomic软件仓库可以正常工作。

wget -q -O - http://www.atomicorp.com/installers/atomic | sh

这将添加原子库,然后...
sudo yum install graphviz-devel

你应该能够执行相关的IT技术操作。
pip install pygraphviz

1

在为Django创建一个使用python-alpine镜像的Dockerfile时,我遇到了这个问题。

幸好,通过参考这篇帖子,我成功解决了这个问题,只需将"graphviz-dev"包添加到我的应用程序的其他依赖项中即可。

示例:

#Install dependencies
RUN apk add --update --no-cache --virtual .build-deps \
    build-base \
    alpine-sdk \
    postgresql-dev \
    libffi-dev \
    python3-dev \
    libffi-dev \
    jpeg-dev \
    zlib-dev \
    musl-dev \
    libpq \
    graphviz-dev \  
    && pip install --no-cache-dir -r /code/requirements_dev.txt \
    && find /usr/local \
        \( -type d -a -name test -o -name tests \) \
        -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
        -exec rm -rf '{}' +

敬礼


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接