在 Jython 2.7.0 上安装 networkx 1.9.1

Installing networkx 1.9.1 on Jython 2.7.0

来自官网xVersion 1.9 notes and API changes:

Basic support is added for Jython 2.7 [...], although they remain not officially supported.

如何在 Jython 上安装 networkx?

我试过的:

sudo /opt/jython2.7.0/bin/pip install networkx

下载了文件 networkx-1.9.1-py2.py3-none-any.whl,但在某些时候出错:

Exception:
Traceback (most recent call last):
  File "/opt/jython2.7.0/Lib/site-packages/pip/basecommand.py", line 133, in main
    status = self.run(options, args)
  File "/opt/jython2.7.0/Lib/site-packages/pip/commands/install.py", line 325, in run
    requirement_set.install(
  File "/opt/jython2.7.0/Lib/site-packages/pip/commands/install.py", line 325, in run
    requirement_set.install(
  File "/opt/jython2.7.0/Lib/site-packages/pip/req/req_set.py", line 633, in install
    requirement.install(
  File "/opt/jython2.7.0/Lib/site-packages/pip/req/req_install.py", line 719, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/opt/jython2.7.0/Lib/site-packages/pip/req/req_install.py", line 990, in move_wheel_files
    move_wheel_files(
  File "/opt/jython2.7.0/Lib/site-packages/pip/wheel.py", line 154, in move_wheel_files
    compileall.compile_dir(source, force=True, quiet=True)
  File "/opt/jython2.7.0/Lib/compileall.py", line 56, in compile_dir
    if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
  File "/opt/jython2.7.0/Lib/compileall.py", line 56, in compile_dir
    if not compile_dir(fullname, maxlevels - 1, dfile, force, rx,
  File "/opt/jython2.7.0/Lib/compileall.py", line 50, in compile_dir
    if not compile_file(fullname, ddir, force, rx, quiet):
  File "/opt/jython2.7.0/Lib/compileall.py", line 99, in compile_file
    ok = py_compile.compile(fullname, None, dfile, True)
  File "/opt/jython2.7.0/Lib/compileall.py", line 99, in compile_file
    ok = py_compile.compile(fullname, None, dfile, True)
  File "/opt/jython2.7.0/Lib/py_compile.py", line 99, in compile
    _py_compile.compile(file, cfile, dfile)
  File "/opt/jython2.7.0/Lib/py_compile.py", line 99, in compile
    _py_compile.compile(file, cfile, dfile)
RuntimeException: java.lang.RuntimeException: Method code too large!

我在 _py_compile.compile(file, cfile, dfile) 函数之前添加了以下打印语句:

print "file: %s" % file
print "cfile: %s" % cfile
print "dfile: %s" % dfile

这给了我:

file: /tmp/pip_build_vagrant/networkx/networkx/generators/atlas.py
cfile: None
dfile: None

有人设法在 Jython2.7.0 上安装 networkx 1.9 吗?

以下是如何在 Jython 中安装 networkx:

  1. 转到 PyPI 并下载 networkx source package
  2. 将 networkx 源解压到一个文件夹中。
  3. 在源文件夹中,删除文件networkx/generators/atlas.py(这个文件太大了,Jython 无法解析。反正在Jython 中不起作用,所以就删除它)。
  4. 在文件 networkx/readwrite/gml.py 中,删除所有提及的 lib2to3(由于 Jython 中的当前错误,lib2to3 不可用):

    • 在第 44-46 行,注释掉从 lib2to3 的导入
    • 在第 75 行左右,更改:

      rtp_fix_unicode = RefactoringTool(['lib2to3.fixes.fix_unicode'], 
                                        {'print_function': True})
      

      至:

      rtp_fix_unicode = None
      
    • 145行左右,在try-except语句中,去掉ParseError和TokenError。
  5. 回到源文件夹,运行:

    jython/pip install .
    

    或者,如果您没有用于 jython 的 pip:

    jython setup.py install
    

它应该安装成功,您现在应该可以在 Jython 中导入 networkx。