gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:03:55 27 4
gpt4 key购买 nike

我正在尝试在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位版本的wheel。这就是结果。

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。我已经读了很多关于人们在改变路径中寻找解决方案的信息,但是我不确定如何做到这一点。这种方法看起来很复杂。

感谢您的帮助/见解!

最佳答案

更新了回购协议:[GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5/Graphviz-2.42.2(作为URL声明,使用了Graphviz 2.42.2官方源代码)。为Python添加了.whls(win_amd64,win32):

3.8
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位))。更新:我还在上述存储库中添加了Graphviz 2.42.2内部版本(32位-因为它适用于64位和32位Win)

检查它是否有较新的软件版本


此外,一个错误(在以前的版本中存在)已得到修复。检查[SO]: pygraphviz 1.5 default edge no arrow? (@CristiFati's answer)了解更多详细信息

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


1.简介
差不多2年后,问题仍然存在(当然,与问题不完全相同)。
我首先要强调两个软件包之间的区别:

[PyPI]: pygraphviz - Download files:相关的那个
[PyPI]: graphviz - Download files :(更简单的)名称相似的

在Anaconda环境中,[SO]: Installing PyGraphviz on Windows 10 64-bit, Python 3.6 (@TomHanks's answer)可以完美工作。
pygraphwiz仅可下载存档文件(在本例中为.zip),这意味着该文件包含(C / C ++)源。
关于名称包含cp34-none-win_amd64之类的软件包(.whls)的两句话(有关详细信息,请参见[SO]: What does version name 'cp27' or 'cp35' mean in Python? (@WayneWerner's answer)):

它们包含二进制文件(.so或.pyd(.dll)),它们与特定的Python库链接

它们仅适用于该Python版本(因此34并不意味着适用于Python 3.6)
甚至有人以某种方式“精明” pip并设法安装了这样一个软件包(实际上并不难),它将在导入时失败,或更糟糕的是,它很有可能使Python崩溃



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

下载资源
在本地构建源

需要一个C(C ++)编译器,通常是:

Nix上的gcc
VStudio在Win


他们可能还有其他依赖性


安装构建的人工制品(二进制文件和.py(c)文件)

附带说明:pip -v ...为当前命令启用详细模式,当遇到安装错误时,该模式非常方便。
回到我们的问题:Python 3.6需要VStudio 2015([Python.Wiki]: WindowsCompilers)。这是一个非常广阔的话题,我在以下内容中做了介绍:

[SO]: Simstring (python) installation in windows (@CristiFati's answer)
[SO]: How to build a DLL version of libjpeg 9b? (@CristiFati's answer)

您应该在继续操作之前检查它们,并保持打开状态,因为在后续步骤中肯定会需要它们。
我安装了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.建立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]> ~/sopr.sh
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[064bit prompt]> 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.
[064bit prompt]>
[064bit prompt]> 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'
[064bit prompt]>
[064bit prompt]> 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

[064bit prompt]> 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修改:

[064bit prompt]> 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")


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

野牛
脚本

设置不正确(很可能是因为Cygwin)。
您可以从IDE构建2个项目,但是我选择了命令行( [MS.Docs]: MSBuild command-line reference),因为我发现它更灵活:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q045093811]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> "c:\Install\x86\Microsoft\Visual Studio Community\2015\vc\vcvarsall.bat" x64

[prompt]> set PATH=%PATH%;%CD%\src\graphviz\windows\dependencies\graphviz-build-utilities

[prompt]> 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

[prompt]> echo %errorlevel%
0

[prompt]> dir /b
bin
build_cdt.txt
other
src

[prompt]> 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

[prompt]> echo %errorlevel%
0

[prompt]> 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源文件(从 [GitHub]: pygraphviz/pygraphviz - (pygraphviz-1.5) pygraphviz-pygraphviz-1.5.zip下载)并解压缩为src / pygraphviz / pygraphviz-pygraphviz-1.5。
需要对Graphviz进行更多调整(可能是作为另一个项目的一部分-安装步骤完成):准备头文件:

[prompt]> mkdir include\graphviz

[prompt]> copy src\graphviz\lib\cdt\cdt.h include\graphviz
1 file(s) copied.

[prompt]> 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-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);

那是一个差异(补丁)。有关如何在Win上应用修补程序的信息,请参见 [SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)(修补程序运行程序部分)(基本上,每行以一个“ +”号开头的行都进入,而每行以一个“-”号开头的行都熄灭)。

[prompt]> :: Restore the original prompt as cwd is important
[prompt]> exit

[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 install dir中(_graphviz.cp36之后) -win_amd64.pyd,(以我为例):“ e:\ Work \ Dev \ VEnvs \ py_064_03.06.08_test0 \ lib \ site-packages \ pygraphviz”)。
4.捷径
由于整个过程很复杂,并且需要大量的人工干预和修改,因此,我设法构建了(wheel.package)程序包(仅需进行少量的setup.py修改)。
我不知道有一种简单的方法可以将其公开发布,因此(尽管我知道这是一种不好的做法),我将其上传到了 [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5/Graphviz-2.42.2/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环境!

关于python - 在Windows 10 64位,Python 3.6上安装pygraphviz,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42074557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com