【问题标题】:python-igraph "'bytes' object has no attribute 'encode'" when plotting in Colab在 Colab 中绘图时,python-igraph “'bytes' 对象没有属性 'encode'”
【发布时间】:2020-01-11 17:49:13
【问题描述】:

尽管安装了 apt-installed libcairo2-dev 和 pip-installed cairocffi,但我无法在 Google Colab 中使用 python-igraph 绘制任何内容。

我想在 Colab 的一个项目中使用 igraph,专门用于绘制图形和节点社区。安装步骤顺利进行,没有错误。但是当我尝试绘制一个简单的图形时,我收到来自igraph/drawing/__init__.py 的关于 utf-8 编码的错误。将pycairo 换成cairocffi 后,我得到了同样的错误。

!sudo apt install build-essential python-dev libxml2 libxml2-dev zlib1g-dev libcairo2-dev
!pip install cairocffi python-igraph
import igraph as ig
g = ig.Graph(edges=[(0,1)])
ig.plot(g)

没有情节出现,我得到:

AttributeError                            Traceback (most recent call last)

/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    336             method = get_real_method(obj, self.print_method)
    337             if method is not None:
--> 338                 return method()
    339             return None
    340         else:

/usr/local/lib/python3.6/dist-packages/igraph/drawing/__init__.py in _repr_svg_(self)
    352         surface.finish()
    353         # Return the raw SVG representation
--> 354         return io.getvalue().encode("utf-8")
    355 
    356     @property

AttributeError: 'bytes' object has no attribute 'encode'

<igraph.drawing.Plot at 0x7f6b34afb160>

【问题讨论】:

    标签: python igraph google-colaboratory


    【解决方案1】:

    显然这是一个已知的错误,但由于某种原因尚未修复......请参阅GtiHub issue tracker

    您将需要手动编辑文件并将错误行替换为(例如)

    result = io.getvalue()
    return result.decode("utf-8")
    

    【讨论】:

      【解决方案2】:

      按照@Silmathoron 的解释,我换了 !pip install cairocffi python-igraph 在我原来的问题中

      !pip install cairocffi
      !pip download python-igraph
      !tar -xf python-igraph-0.7.1.post6.tar.gz
      with open("python-igraph-0.7.1.post6/igraph/drawing/__init__.py", 'r') as file:
        text = file.read()
      assert text[14797:14803] == 'encode'
      with open("python-igraph-0.7.1.post6/igraph/drawing/__init__.py", 'w') as file:
        file.write(text[:14797] + "decode" + text[14803:])
      !pip install --no-index --find-links="." python-igraph
      

      解决了这个问题。 (不过,Colab 在上次安装 pip 时已经挂了很长时间。)

      【讨论】:

        猜你喜欢
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        • 2022-01-21
        • 2015-03-28
        • 1970-01-01
        • 1970-01-01
        • 2013-08-05
        相关资源
        最近更新 更多