更新了 repo:[GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5:
- 使用官方 Graphviz 2.42.2 源
-
.whls (win_amd64, win32) 用于当前支持的 Python 版本
- 可能会添加较新的版本(检查上一级)
对于 Python 2.7,它们已经构建:[UCI.LFD]: Unofficial Windows Binaries for Python Extension Packages - PyGraphviz, an interface to the Graphviz graph layout and visualization package.。
注意事项:
任何想了解有关构建过程的更多详细信息的人,继续阅读!
1。简介
差不多 2 年后,问题(嗯,不完全是问题中的问题)仍然存在。
我想首先强调两个包之间的区别:
在 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
现在,许多软件包都为在各种OSes 上运行的最常见的Python 版本(例如[PyPI]: mysql-connector-python - Download files)预构建了二进制文件,但也有很多软件包没有,并且那些只包含来源。不幸的是,PyGraphviz 属于 2nd 类别。对于后者,pip install 将:
- 下载源代码
- 在本地构建源
-
C (C++) 编译器是必需的,通常:
-
gcc 在 Nix 上
-
VStudio 在 Win 上
- 他们可能有其他依赖关系
- 安装构建的工件(二进制文件和 .py(c) 文件)
附带说明:pip -v ... 为当前命令启用 详细 模式,这在遇到安装错误时非常方便。
回到我们的问题:Python 3.6 需要 VStudio 2015 ([Python.Wiki]: WindowsCompilers)。
这是一个非常广泛的话题,我在以下部分介绍了一些部分:
您应该在继续之前检查它们并保持它们打开,因为您在接下来的步骤中肯定会需要它们。
我已经安装了 VStudio 2015 Community(以及许多其他版本),你也应该安装它,它是免费的 ([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
这是我的top dir,任何源都下载到 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.7pygraphviz(内置)包,它只依赖于cgraph.dll,它又依赖于cdt.dll,所以只有两个项目与我们相关。请注意,这 2 个项目可能不需要所有的 git 子模块(因此可能会削减 dir),但我没有进一步调查。
很遗憾,这些项目仅配置为 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")
第 3rd 项是因为我需要重置 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.
不幸的是,PyGraphviz没有构建OOTB,因为[GitHub]: pygraphviz/pygraphviz - Python 3 support。要解决此问题,必须应用 [GitHub]: eendebakpt/pygraphviz - Workaround for PyIOBase_Type for Python2 on win。我对其进行了调整以与当前资源一起使用(因为它也不起作用 OOTB :X( ) 仅适用于 graphviz_wrap.cpp :
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);
那是一个差异(补丁)。请参阅[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)(Patching utrunner 部分)了解如何在 Win 上应用补丁(基本上,以 one 开头的每一行+" 符号进入,以 一个 "-" 符号开头的每一行都退出)。
[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%。
当然,这只是一种(蹩脚的)解决方法 (gainarie),不应该在每次必须使用包时都发生这种情况。
我(还)不知道如何指示 setup.py 将它们也复制到包构建/安装目录中,因此作为替代(也是解决方法)必须手动复制它们在 pygraphviz 中安装目录(在 _graphviz.cp36-win_amd64.pyd 旁边,即(在我的情况下):"e: \Work\Dev\VEnvs\py_064_03.06.08_test0\lib\site-packages\pygraphviz")。
4。快捷方式
由于整个过程很复杂,并且需要大量的人工干预和黑客攻击,我已经设法构建(稍作setup.py 修改)(wheel)包。
我不知道将其公开的简单方法,因此(尽管我知道这是一种不好的做法)我将其上传至 [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyGraphviz/v1.5/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 环境!