在Windows 10 64位,Python 3.6上安装pygraphviz

问题描述 投票:23回答:4

好的,我们在这里......我正在尝试在Windows 10上安装pygraphviz。这个问题在网上有很多解决方案,但还没有一个对我有用。我遇到的确切问题是通过jupyter笔记本 - >

[1] import networkx as nx
import pylab as plt
from networkx.drawing.nx_agraph import graphviz_layout

[2]G = nx.DiGraph()
G.add_node(1,level=1)
G.add_node(2,level=2)
G.add_node(3,level=2)
G.add_node(4,level=3)

G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(2,4)

nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
    node_color=range(len(G)),
    prog='dot')
plt.show()

在[2]后我得到以下错误:

ModuleNotFoundError                       Traceback (most recent call last)
C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py 
in 
pygraphviz_layout(G, prog, root, args)
    254     try:
--> 255         import pygraphviz
    256     except ImportError:

ModuleNotFoundError: No module named 'pygraphviz'

ImportError                               Traceback (most recent call last)
<ipython-input-2-86a15892f0f0> in <module>()
  9 G.add_edge(2,4)
 10 
---> 11 nx.draw(G, pos=graphviz_layout(G), node_size=1600, cmap=plt.cm.Blues,
 12         node_color=range(len(G)),
 13         prog='dot')

C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in graphviz_layout(G, prog, root, args)
226 
227     """
--> 228     return pygraphviz_layout(G,prog=prog,root=root,args=args)
229 
230 def pygraphviz_layout(G,prog='neato',root=None, args=''):

C:\Users\name\Anaconda3\lib\site-packages\networkx\drawing\nx_agraph.py in pygraphviz_layout(G, prog, root, args)
256     except ImportError:
257         raise ImportError('requires pygraphviz ',
--> 258                           'http://pygraphviz.github.io/')
259     if root is not None:
260         args+="-Groot=%s"%root

ImportError: ('requires pygraphviz ', 'http://pygraphviz.github.io/')

这是我试图解决的问题

(1)常规pip安装:“pip install pygraphviz”这是我最后得到的错误。编辑我得到相同的错误,即使我以管理员身份运行cmd。

Command "C:\Users\name\Anaconda3\python.exe -u -c "import setuptools, 
tokenize;__file__='C:\\Users\\name~1\\AppData\\Local\\Temp\\pip-build-
n81lykqs\\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 C:\Users\name~1\AppData\Local\Temp\pip-
b3jz1lk5-record\install-record.txt --single-version-externally-managed --
compile" failed with error code 1 in C:\Users\name~1\AppData\Local\Temp\pip-
build-n81lykqs\pygraphviz\

(2)下载并安装graphviz-2.38.msi,然后下载64位版本的滚轮。这是结果。

C:\Users\name\Anaconda3>pip install pygraphviz-1.3.1-cp34-none-
win_amd64.whl
pygraphviz-1.3.1-cp34-none-win_amd64.whl is not a supported wheel on this 
platform.

C:\Users\name\Anaconda3>pip install pygraphviz-1.3.1-cp27-none-
win_amd64.whl
pygraphviz-1.3.1-cp27-none-win_amd64.whl is not a supported wheel on this 
platform.

我想尝试一下,但不确定如何正确做到:

编辑setup.py。我已经阅读了很多关于人们在改变路径时找到解决方案的内容,但我不确定如何做到这一点。这种方法看起来非常复杂。

感谢您的帮助/见解!

python windows pip anaconda pygraphviz
4个回答
6
投票

投票最多的答案似乎是安装graphviz,而不是pygraphviz。

如果您使用的是conda环境,可以尝试使用此频道:

conda install graphviz pygraphviz -c alubbock

我用networkx 2.1试了一下,它工作得很好。


2
投票

在Windows 10 64位和Python 3.6上解决了它。

脚步:

  1. 从graphiviz网站下载Graphviz for windows
  2. 在Windows路径中添加Graphviz bin路径C:\Program Files (x86)\Graphviz2.38\bin
  3. 关闭并重新打开终端,以便识别路径更改。
  4. 下载graphviz python 3.6 wheel
  5. 安装graphviz滚轮。 pip install graphviz-0.8.3-py2.py3-none-any.whl
  6. 完成!

2
投票

更新了回购:[GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/Windows/PyGraphviz。 为Python添加了.whls(win_amd64,win32):

  • 3.7
  • 3.6
  • 3.5

对于Python 2.7,它们已经构建:[UCI.LFD]: Unofficial Windows Binaries for Python Extension Packages - PyGraphviz, an interface to the Graphviz graph layout and visualization package.

笔记:

  • 在某些(我猜,大多数)情况下,在运行PyGraphviz的系统上将需要Graphviz安装,因为PyGraphviz使用了Graphviz的一些工具(可执行文件)。它们可以下载或构建(它们不必匹配PyGraphviz架构(32位,64位),因为它们被调用)
  • 此外,修复了一个错误(存在于以前的版本中)
  • 查看[SO]: pygraphviz 1.5 default edge no arrow? (@CristiFati's answer)了解更多详情

任何想要了解有关构建过程的更多详细信息的人,请继续阅读!


1. Intro

差不多2年后,问题(嗯,问题不完全如此)仍然存在。

我想首先强调两个包之间的区别:

在Anaconda环境中,[SO]: Installing PyGraphviz on Windows 10 64-bit, Python 3.6 (@TomHanks's answer)完美运作。

pygraphwiz只能下载一个存档(在本例中为.zip)文件,这意味着它包含(C / C ++)源。

关于包(.whls)的几个词,其名称包含cp34-none-win_amd64之类的东西(详情请查看[SO]: What does version name 'cp27' or 'cp35' mean in Python? (@WayneWerner's answer)):

  • 它们包含二进制文件(.so或.pyd(.dll)),它们与特定的Python库链接 它们只适用于那个Python版本(因此34不适用于Python 3.6) 甚至是某种方式“outmarts”pip并设法安装这样的软件包(实际上并不那么难),它会在导入时失败,或者更糟糕的是,它很可能会崩溃Python

现在,许多软件包都有预先构建的二进制文件,用于在各种操作系统上运行的大多数常见Python版本(例如[PyPI]: mysql-connector-python - Download files),但正如许多软件包那样,并且只包含源代码。不幸的是,PyGraphviz属于第二类。对于后者,pip install将:

  • 下载来源
  • 在本地构建源 通常需要C(C ++)编译器: 尼克斯上的gcc VStudio赢了 他们可能有其他依赖关系
  • 安装构建的文物(二进制文件和.py(c)文件)

作为旁注:pip -v ...为当前命令启用详细模式,在遇到安装错误时非常方便。

回到我们的问题:Python 3.6需要VStudio 2015([Python.Wiki]: WindowsCompilers)。 这是一个非常广泛的主题,我在以下部分介绍了:

您应该在继续之前检查它们并保持它们打开,因为您在接下来的步骤中肯定需要它们。

我安装了VStudio 2015社区(在许多其他版本中),你也应该安装它,它是免费的([MS.VStudio]: Still want an older version?)。

PyGraphviz依赖于[Graphviz]: Graph Visualization Software。因此,在构建时,它将需要(部分)Graphviz(它也有自己的其他依赖项)已经构建。不幸的是,我找不到预建的二进制文件(有[Graphviz]: Windows Packages - graphviz-2.38.zip,但这没有帮助),所以它必须手动构建。

在继续之前:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" -c "import pygraphviz"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'pygraphviz'

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> dir /b
other
src

这是我的顶级目录,任何源都下载在src目录中,二进制文件将放在bin目录中。

2. Build Graphviz

在开始之前,我想提一下,我非常依赖Cygwin(你没必要),我的一些工具安装在那里,所以我将在Cygwin和cmd终端之间交替(这可能会令人困惑)。

[Graphviz]: Graphviz Build Instructions for Windows说:

在Windows上构建:

(Graphviz版本≥2.41)

首先,在存储库的根目录中,执行git submodule update --init。这将下载所有子模块,这些子模块主要是Windows构建的依赖项。接下来,将windows \ dependencies \ graphviz-build-utilities目录添加到PATH(并重新启动Visual Studio或之后执行msbuild的提示)。此文件夹包含Bison,Flex和SED(以及将来添加的)工具以及经过测试的版本。如果一切顺利,现在已设置依赖项,您可以构建Graphviz。

首先,我们需要下载所有内容:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz]> git clone https://gitlab.com/graphviz/graphviz.git .
Cloning into '.'...
remote: Enumerating objects: 71728, done.
remote: Counting objects: 100% (71728/71728), done.
remote: Compressing objects: 100% (19331/19331), done.
remote: Total 71728 (delta 52200), reused 71681 (delta 52157)
Receiving objects: 100% (71728/71728), 163.79 MiB | 480.00 KiB/s, done.
Resolving deltas: 100% (52200/52200), done.
Checking out files: 100% (3870/3870), done.
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz]> git submodule update --init
Submodule 'dependencies/criterion' (https://github.com/Snaipe/Criterion.git) registered for path 'dependencies/criterion'
Submodule 'windows/dependencies/graphviz-build-utilities' (https://github.com/ErwinJanssen/graphviz-build-utilities.git) registered for path 'windows/dependencies/graphviz-build-utilities'
Submodule 'windows/dependencies/libraries' (https://github.com/ErwinJanssen/graphviz-windows-dependencies.git) registered for path 'windows/dependencies/libraries'
Cloning into '/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz/dependencies/criterion'...
Cloning into '/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz/windows/dependencies/graphviz-build-utilities'...
Cloning into '/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz/windows/dependencies/libraries'...
Submodule path 'dependencies/criterion': checked out '301d143ea42c024f22b673b69c72a4cb3c8d151f'
Submodule path 'windows/dependencies/graphviz-build-utilities': checked out '050fff84ce195e0740878748760fd801eeb07b23'
Submodule path 'windows/dependencies/libraries': checked out '141d3a21be904fa8dc2ae3ed01d36684db07a35d'
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz]> git show head
commit 89292b5945933b1501293c04894ed9cf886241be (HEAD -> master, origin/master, origin/HEAD)
Merge: 429d43615 97811bd35
Author: Stephen C North <[email protected]>
Date:   Mon Feb 4 08:09:40 2019 -0500

    Merge branch 'wasbridge/graphviz-master' into HEAD

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz]> git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

你最终会得到一个含有~320 MiB东西的目录。 dir包含graphviz.sln文件,该文件是包含63个项目的VStudio(2015)解决方案文件。

看看Anaconda或Python 2.7 pygraphviz(内置)软件包,它只依赖于cgraph.dll,而cgraph.dll依赖于cdt.dll,因此只有2个项目与我们相关。请注意,这两个项目可能不需要所有的git子模块(因此dir可能会被削减),但我没有进一步调查。

不幸的是,这些项目仅配置为32位(Win32平台)。必须手动添加64位一个(我是从VStudio IDE中完成的 - 并且还在我引用的一个答案中描述了该过程)。保存项目后,它们将显示为由git修改:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q045093811/src/graphviz]> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

        modified:   lib/cdt/cdt.vcxproj
        modified:   lib/cgraph/cgraph.vcxproj
        modified:   windows/dependencies/graphviz-build-utilities (modified content)

no changes added to commit (use "git add" and/or "git commit -a")

第3项是因为我需要重置2个可执行文件的一些安全权限(在构建cgraph时使用):

  • bison.exe
  • flex.exe

没有正确设置(很可能,因为Cygwin)。

您可以从IDE构建2个项目,但我选择命令行([Ms.Docs]: MSBuild command-line reference),因为我发现它更灵活:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> "c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> set PATH=%PATH%;%CD%\src\graphviz\windows\dependencies\graphviz-build-utilities

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> msbuild src\graphviz\lib\cdt\cdt.vcxproj /t:Rebuild /p:Platform=x64;Configuration=Release;SolutionDir=%CD%\src\graphviz\;OutDir=%CD%\bin\Win\dynamic\064\UCRTv140\md\Release\graphviz\ >build_cdt_064.txt 2>&1

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> echo %errorlevel%
0

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> dir /b
bin
build_cdt.txt
other
src

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> msbuild src\graphviz\lib\cgraph\cgraph.vcxproj /t:Rebuild /p:Platform=x64;Configuration=Release;SolutionDir=%CD%\src\graphviz\;OutDir=%CD%\bin\Win\dynamic\064\UCRTv140\md\Release\graphviz\ >build_cgraph_064.txt 2>&1

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> echo %errorlevel%
0

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> dir /b "bin\Win\dynamic\064\UCRTv140\md\Release\graphviz"
cdt.dll
cdt.dll.lastcodeanalysissucceeded
cdt.exp
cdt.lib
cgraph.dll
cgraph.dll.lastcodeanalysissucceeded
cgraph.exp
cgraph.lib

因此,我们拥有所需的一切(2 .lib和2 .dll文件)以便继续。

3. Build PyGraphviz

PyGraphviz来源(从[GitHub]: pygraphviz/pygraphviz - (pygraphviz-1.5) pygraphviz-pygraphviz-1.5.zip下载并在src / pygraphviz / pygraphviz-pygraphviz-1.5中解压缩)。

Graphviz还需要进行一次调整(可能是作为另一个项目的一部分 - 安装步骤完成):准备头文件:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> mkdir include\graphviz

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> copy src\graphviz\lib\cdt\cdt.h include\graphviz
        1 file(s) copied.

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> copy src\graphviz\lib\cgraph\cgraph.h include\graphviz
        1 file(s) copied.

不幸的是,由于[GitHub]: pygraphviz/pygraphviz - Python 3 support,PyGraphviz没有建立OOTB。要解决这个问题,必须使用[GitHub]: eendebakpt/pygraphviz - Workaround for PyIOBase_Type for Python2 on win。我修改它以使用当前源(因为它不起作用OOTB,以及:仅对于graphviz_wrap.cpp的X():

pygraphviz-1.5全pyiobase_b85d12ac22d39063f7dbcc396e825c563431e352.patch:

--- pygraphviz/graphviz_wrap.c.orig 2018-09-10 16:07:12.000000000 +0300
+++ pygraphviz/graphviz_wrap.c  2019-02-26 18:05:20.281741400 +0200
@@ -2988,7 +2988,18 @@


 #if PY_VERSION_HEX >= 0x03000000
-extern PyTypeObject PyIOBase_Type;
+static PyObject *PyIOBase_TypeObj;
+
+static int init_file_emulator(void)
+{
+  PyObject *io = PyImport_ImportModule("_io");
+  if (io == NULL)
+    return -1;
+  PyIOBase_TypeObj = PyObject_GetAttrString(io, "_IOBase");
+  if (PyIOBase_TypeObj == NULL)
+    return -1;
+  return 0;
+}
 #endif


@@ -3449,7 +3460,7 @@
   {
 #if PY_VERSION_HEX >= 0x03000000 || defined(PYPY_VERSION)
 #if !defined(PYPY_VERSION)
-    if (!PyObject_IsInstance(obj0, (PyObject *)&PyIOBase_Type)) {
+    if (!PyObject_IsInstance(obj0, PyIOBase_TypeObj)) {
       PyErr_SetString(PyExc_TypeError, "not a file handle");
       return NULL;
     }
@@ -3523,7 +3534,7 @@
   {
 #if PY_VERSION_HEX >= 0x03000000 || defined(PYPY_VERSION)
 #if !defined(PYPY_VERSION)
-    if (!PyObject_IsInstance(obj1, (PyObject *)&PyIOBase_Type)) {
+    if (!PyObject_IsInstance(obj1, PyIOBase_TypeObj)) {
       PyErr_SetString(PyExc_TypeError, "not a file handle");
       return NULL;
     }
@@ -6051,6 +6062,12 @@

   SWIG_InstallConstants(d,swig_const_table);

+#if PY_VERSION_HEX >= 0x03000000
+  if (init_file_emulator() < 0) {
+    return NULL;
+  }
+#endif
+
   PyDict_SetItemString(md,(char*)"cvar", SWIG_globals());
   SWIG_addvarlink(SWIG_globals(),(char*)"Agdirected",Swig_var_Agdirected_get, Swig_var_Agdirected_set);
   SWIG_addvarlink(SWIG_globals(),(char*)"Agstrictdirected",Swig_var_Agstrictdirected_get, Swig_var_Agstrictdirected_set);

这是一个差异(补丁)。请参阅[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)(修补utrunner部分)了解如何在Win上应用补丁(基本上,每个以一个“+”符号开头的行都会进入,并且以“ - ”符号开头的每一行都会消失)。

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> set _TOP_DIR=%CD%

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> pushd src\pygraphviz\pygraphviz-pygraphviz-1.5

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811\src\pygraphviz\pygraphviz-pygraphviz-1.5]> pushd pygraphviz && "c:\Install\x64\Cygwin\Cygwin\AllVers\bin\patch.exe" -p 1 -buNi ..\pygraphviz-1.5-all-pyiobase_b85d12ac22d39063f7dbcc396e825c563431e352.patch && popd
patching file graphviz_wrap.c

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811\src\pygraphviz\pygraphviz-pygraphviz-1.5]> echo %errorlevel%
0

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811\src\pygraphviz\pygraphviz-pygraphviz-1.5]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" setup.py install --include-path=%_TOP_DIR%\include --library-path=%_TOP_DIR%\bin\Win\dynamic\064\UCRTv140\md\Release\graphviz >%_TOP_DIR%\install_pygraphviz_064.txt 2>&1

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811\src\pygraphviz\pygraphviz-pygraphviz-1.5]> echo %errorlevel%
0

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811\src\pygraphviz\pygraphviz-pygraphviz-1.5]> popd

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> set PATH=%PATH%;%CD%\bin\Win\dynamic\064\UCRTv140\md\Release\graphviz

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> "e:\Work\Dev\VEnvs\py_064_03.06.08_test0\Scripts\python.exe" -c "import pygraphviz;print(dir(pygraphviz), \"\n\", pygraphviz.graphviz._graphviz)"
['AGraph', 'Attribute', 'DotError', 'Edge', 'ItemAttribute', 'Node', '__all__', '__author__', '__builtins__', '__cached__', '__date__', '__doc__', '__file__', '__license__', '__loader__', '__name__', '__package__', '__path__', '__revision__', '__spec__', '__version__', 'absolute_import', 'agraph', 'division', 'graphviz', 'print_function', 'release', 'test', 'tests', 'version']
 <module '_graphviz' (e:\Work\Dev\VEnvs\py_064_03.06.08_test0\lib\site-packages\pygraphviz\_graphviz.cp36-win_amd64.pyd)>

如图所示,该模块已成功导入。

作为评论,导入模块时必须具有2 .dll依赖关系(来自上一节),因此将其dir添加到%PATH%。 当然这只是一个(跛脚)解决方法(获得),每次必须使用包时都不会发生这种情况。

我不知道(还)如何指示setup.py也将它们复制到包build / install目录中,所以作为替代方法(也是解决方法),必须在pygraphviz install dir中手动复制它们(在_graphviz.cp36旁边) -win_amd64.pyd,这是(在我的例子中):“e:\ Work \ Dev \ VEnvs \ py_064_03.06.08_test0 \ lib \ site-packages \ pygraphviz”)。

4. Shortcut

由于整个过程很复杂,并且需要大量的手动干预和黑客攻击,因此我设法构建(使用较小的setup.py修改)(wheel)包。

我不知道一个简单的方法可以公开,所以(虽然我知道这是一个不好的做法,)我上传到[GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/Windows/PyGraphviz/pygraphviz-1.5-cp36-cp36m-win_amd64.whl。 你可以下载它,然后安装它(这是一种方式):

"C:\Path\to\Python-3.6-amd64\pip" install "C:\Path\to\downloaded\pygraphviz-1.5-cp36-cp36m-win_amd64.whl"

注意:它也适用于Anaconda环境!


-1
投票

这对我有用:

Win 7 AMD64

我在github上写了一些关于它的东西。它很乱,使用风险自负:https://github.com/darkhipo/Easy-Digraph-Draw

© www.soinside.com 2019 - 2024. All rights reserved.