Monday, March 30, 2015

Solving the problems with pycallgraph under Windows

I've installed Python Call Graph (pycallgraph) using pip:

pip install pycallgraph

Then I've tried to run it:

pycallgraph graphviz -- ./main.py

But I've got a message:
"pycallgraph" is not recognized as an internal or external command, operable program or batch file.

Ok, then I found pycallgraph location and executed it:

python c:\Python27\Scripts\pycallgraph graphviz -- ./main.py

But alas, I've got error message:

E:\>python c:\Python27\Scripts\pycallgraph graphviz -- ./main.py
Traceback (most recent call last):
  File "c:\Python27\Scripts\pycallgraph", line 25, in
    with __pycallgraph.PyCallGraph(config=__config):
  File "C:\Python27\lib\site-packages\pycallgraph\pycallgraph.py", line 32, in __init__
    self.reset()
  File "C:\Python27\lib\site-packages\pycallgraph\pycallgraph.py", line 53, in reset
    self.prepare_output(output)
  File "C:\Python27\lib\site-packages\pycallgraph\pycallgraph.py", line 97, in prepare_output
    output.sanity_check()
  File "C:\Python27\lib\site-packages\pycallgraph\output\graphviz.py", line 63, in sanity_check
    self.ensure_binary(self.tool)
  File "C:\Python27\lib\site-packages\pycallgraph\output\output.py", line 96, in ensure_binary
    'The command "{}" is required to be in your path.'.format(cmd))
pycallgraph.exceptions.PyCallGraphException: The command "dot" is required to be in your path.

I've installed graphviz:

pip install graphviz

But the problem remained.

Then I've downloaded current stable release of graphviz (graphviz-2.38.msi) from official site: http://www.graphviz.org/Download_windows.php

And installed it.
You can check that graphviz for python is trying to execute dot.exe tool (subprocess.Popen(cmd) in graphviz\files.py), so to solve the problem you can go to the graphviz directory and call pycallgraph:
cd c:\Program Files (x86)\Graphviz2.38\bin
c:\Program Files (x86)\Graphviz2.38\bin>python c:\Python27\Scripts\pycallgraph graphviz -o main.png -- e:\main.py

After that you'll get main.png image inside C:\Program Files (x86)\Graphviz2.38\bin\ directory.

PS. You can add graphviz directory to PATH environment variable.

4 comments:

  1. I get this error. Can you please help to fix it:
    can't find '__main__' module in 'C:\\Users\\Ragesh\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\pycallgraph'

    ReplyDelete
    Replies
    1. Hi!
      Seems like you are trying to pass a directory `pycallgraph` to your Python interpreter.
      Try to run pycallgraph.py in this directory.

      Delete
    2. Look one dir down, i.e. inside pycallgraph dir theres a file named pycallgraph.py

      Delete