6

I follow this doc: https://mg.pov.lt/objgraph/

objgraph_test.py:

import objgraph
import os

x = ['a', '1', [2, 3]]
filename = os.path.dirname(__file__) + '/objgraph_test.png'
objgraph.show_refs([x], filename=filename)

When I try to output a .png image file, it throw an error:

(venv) ☁  python-codelab [master] ⚡  python3 /Users/ldu020/workspace/github.com/mrdulin/python-codelab/src/performance-optimization/memory-profile-and-objgraph/objgraph_test.py
Graph written to /var/folders/38/s8g_rsm13yxd26nwyqzdp2shd351xb/T/objgraph-4hy982i9.dot (6 nodes)
Image renderer (dot) not found, not doing anything else

I already installed xdot package.

(venv) ☁  python-codelab [master] ⚡  pip3 list | grep -e 'xdot\|objgraph'
objgraph           3.4.1     
xdot               1.1  

How can I solve this?

Lin Du
  • 88,126
  • 95
  • 281
  • 483

2 Answers2

3

I ran into the same problem using python3. These three steps worked for me:

  1. Install Graphviz package (which contains the dot.exe file that your script is not finding to generate a .png from a .dot) - Either via pip install or directly download from https://graphviz.gitlab.io/

  2. Add dot.exe to path - You need to be able to run dot.exe just by typing dot in the command line. To do this you have to add the entire path of dot.exe to the environment variables.

  3. Re-run your command line or IDE and run the script again -This time you will be able to generate the png image.

Hope it helps!

0

You need to install system package: sudo apt install graphviz, just installation of python package won't help pip install xdot, (installation for other OS)

The issue is objgraph.py:_program_in_path#L1253 can't find binary file!

paths = os.environ.get("PATH", os.defpath).split(os.pathsep)
paths = [os.path.join(dir, program) for dir in path]
paths = [file for file in path
        if os.path.isfile(file) or os.path.isfile(file + '.exe')]
print(paths)

some of working output:

['/usr/bin/dot', '/bin/dot']
akpp
  • 706
  • 10
  • 12
  • The code doesnt seem to work? Should the variables be "path" not "paths"? And what is "program"? – Kurt Aug 18 '22 at 10:21