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

时间:2017-07-14 02:50:14

标签: python windows pip anaconda pygraphviz

好的,我们走了...... 我正在尝试在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。我已经阅读了很多关于人们在改变路径时找到解决方案的内容,但我不确定如何做到这一点。这种方法看起来非常复杂。

感谢您的帮助/见解!

4 个答案:

答案 0 :(得分:6)

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

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

conda install graphviz pygraphviz -c alubbock

我用networkx 2.1进行了试用,效果很好。

答案 1 :(得分:5)

更新了存储库: [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/Windows/PyGraphviz
Python 添加了 .whl 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 架构( 32bit 64bit ))< / li>
  • 此外,还修复了一个错误(以前版本中存在)
  • 检查[SO]: pygraphviz 1.5 default edge no arrow? (@CristiFati's answer)了解更多详细信息

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



1。简介

将近2年后,问题仍然存在(当然,与问题不完全相同)。

我首先要强调两个软件包之间的区别:

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

pygraphwiz 仅可用于下载存档文件(在这种情况下为 .zip ),这意味着该文件包含( C / < em> C ++ )源。

关于软件包( .whl s)的几句话,其名称包含诸如 cp34-none-win_amd64 之类的内容(选中{{3 }}以获取详细信息):

  • 它们包含二进制文件( .so .pyd .dll )),这些二进制文件与特定的链接在一起> Python
    • 它们只能与该 Python 版本一起使用(因此, 34 不是 ,而是旨在与 Python 3.6
    • 即使是以某种方式“精明”的 pip 并设法安装这样的软件包(实际上并不难),它在导入时会失败,或者更糟的是,它很有可能使 Python
    • 崩溃

现在,许多软件包已经为运行在各种 OS es(例如[SO]: What does version name 'cp27' or 'cp35' mean in Python? (@WayneWerner's answer))上的大多数常见 Python 版本预先构建了二进制文件,但是有许多软件包没有,并且仅包含来源。不幸的是, PyGraphviz 位于2 nd 类别中。对于后者,pip install将:

  • 下载源文件
  • 在本地构建源
    • C C ++ )编译器是必需的,通常是:
        Nix 上的
      • gcc
      • VStudio Win
    • 他们可能还有其他依赖性
  • 安装已构建的人工制品(二进制文件和 .py(c)文件)

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

回到我们的问题: Python 3.6 需要 VStudio 2015 [PyPI]: mysql-connector-python - Download files)。
这是一个非常广阔的话题,我在以下内容中做了介绍:

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

我已经安装了 VStudio 2015社区(在许多其他版本中),您也应该安装它,它是免费的([SO]: How to build a DLL version of libjpeg 9b? (@CristiFati's answer))。

PyGraphviz 取决于[MS.VStudio]: Still want an older version?。因此,在构建时,将需要已经构建 Graphviz (的一部分)(还具有其他依赖项)。不幸的是,我找不到预构建的二进制文件(有[Graphviz]: Graph Visualization Software,但无济于事),因此必须手动构建。

进一步操作之前:

[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 < / em>目录。

2。建立 Graphviz

在开始之前,我想提一下,我非常依赖[Graphviz]: Windows Packages - graphviz-2.38.zip(您不必这样做),并且我的一些工具已安装在其中,因此我将在 Cygwin < / em>和 cmd 终端(可能会造成混淆)。

Cygwin状态:

  

在Windows上构建:

     

(Graphviz版本≥2.41)

     

首先,在存储库的根目录中,执行git子模块更新--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 <scnorth@gmail.com>
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 内容的目录。该目录包含 graphviz.sln 文件,该文件是 VStudio(2015)解决方案文件,其中包含 63 项目

查看 Anaconda Python 2.7 pygraphviz (内置)软件包,它仅取决于 cgraph.dll ,这又取决于 cdt.dll ,因此只有两个项目与我们相关。请注意,这两个项目可能不需要所有的 git 子模块(因此目录可能会被删减),但我没有做进一步的研究。

不幸的是,项目仅配置为 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 rd 项是因为我需要重置2个可执行文件的某些安全权限(在构建 cgraph 时使用):

  • bison.exe
  • flex.exe

设置不正确(很可能是由于 Cygwin )。

您可以从 IDE 构建2个项目,但是我选择了命令行([Graphviz]: Graphviz Build Instructions for Windows),因为我觉得它更灵活:

[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。建立 PyGraphviz

PyGraphviz 源(从[Ms.Docs]: MSBuild command-line reference下载)并解压缩为 src / pygraphviz / pygraphviz-pygraphviz-1.5

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

[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 - (pygraphviz-1.5) pygraphviz-pygraphviz-1.5.zip PyGraphviz 不会构建 OOTB 。要解决此问题,必须应用[GitHub]: pygraphviz/pygraphviz - Python 3 support。我对其进行了修改以使其与当前的源一起使用(因为它对 OOTB 不起作用,也仅对 graphviz_wrap.cpp 有用:X()

pygraphviz-1.5-all-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);

那是一个 diff patch )。请参阅[GitHub]: eendebakpt/pygraphviz - Workaround for PyIOBase_Type for Python2 on win修补 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 依赖项(来自上一节),因此它们的目录已添加到%PATH%中。
当然,这只是(lam)解决方法( gainarie ),这种情况不会在每次必须使用该程序包时发生。

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

4。快捷方式

由于整个过程很复杂,需要大量的人工干预和修改,因此,我设法(对 setup.py 进行了少量修改)构建了( wheel )包。

我不知道有一种简单的方法可以将其公开发布,因此(尽管我知道这是一种不好的做法),我将其上传到了[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)
您可以下载它,然后像这样安装(这是一种方法):

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

注意它也适用于 Anaconda 环境!

答案 2 :(得分: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. 完成!

答案 3 :(得分:-1)

这对我有用:

赢得7台AMD64

我在github上添加了一些内容。它很乱,使用风险自负: https://github.com/darkhipo/Easy-Digraph-Draw