How to start a โ€œdotโ€ as a command from Python?

I am using Python on Mac OSX Leopard.

I am trying to run the dot program (part of Graphviz) from Python:

# -*- coding: utf-8 -*- import os print os.environ['PATH'] print os.system("ls >> a.txt") print os.system("dot -o9.png -Tpng ./6.dot") 

The ls command is just to make sure python is in the correct directory. It. As a result, I get:

/ Usr / bin: / bin: / usr / sbin: / sbin 0 32512

I understand that error 32512 means that python could not find the file, and since there is a 6.dot file (if I run "dot -o9.png -Tpng./6.dot" from the terminal, I get no error and it turns out 9.png), I believe that Python cannot find the point file.

I probably need to add the dot file to the path. But I do not know where it is. If I run:

 whereis dot 

I do not get an answer.

How can I find a point executable?
Alternatively, can I run a point program as a command from within Python?

+4
source share
12 answers

You must modify the PATH line to include the directory containing dot . This directory is /usr/local/bin , without /dot .

+2
source

whereis finds man pages, which finds binary files. So try which dot .

+10
source

You need to add the path to the dot executable in Python. You can do this by changing the PATH variable in os.environ

+3
source

Try the following:

 # -*- coding: utf-8 -*- import os import sys print os.environ['PATH'] os.environ['PATH'] += ":"+"/usr/local/bin" print os.environ['PATH'] print os.getcwd() from subprocess import check_call print check_call(["dot", "-o9.png", "-Tpng", "./6.dot"]) 

Taken from the question in order to try to keep some sanity here.

+3
source

Troubleshooting Tips:

a. add

print os.getcwd()

in the line before os.system ("dot, etc.

Just to make sure the current directory is the one that has the 6.dot file.

B. Verify that the dot program is in your path.

which dot

C. Use the full path to the dot program in your os.system command, see what happens next.

+2
source

Two suggestions

  • Do not use PATH, use "which" instead to just find the executable instead
  • You do not use ";" (semicolon) to separate paths, but ":" (colon). Once you change this, you can find your spot program.

Change it

 os.environ['PATH'] += ";"+"/usr/local/bin/dot" 

to that

 os.environ['PATH'] += ":"+"/usr/local/bin" 

Then your good.

EDIT: Please note that I forgot to remove / dot from the PATH variable myself (oops) - PATH is a colon-separated list of directories.

+2
source

If you also generate your Dot files in Python, pydot does what you want in a more pythonic way:

 import pydot dot = pydot.Dot() n1, n2 = pydot.Node("a"), pydot.Node("b") dot.add_node(n1) dot.add_node(n2) dot.add_edge(pydot.Edge(n1,n2)) dot.write_png("graph.png", prog='neato') 
+2
source

Often the solution is in front of us,

 print os.system("/usr/local/bin/dot -o9.png -Tpng 6.dot") 

You can also try all the points in the specified folder.

 import glob for filedot in glob.glob('*.dot') print os.system("/usr/local/bin/dot -o9.png -Tpng %(filedot)s"%locals()) #print os.system("/usr/local/bin/dot -o9.png -Tpng %s"%filedot) 

Edit:

I can't remember btw if he

 /usr/local/bin/dot -o9.png -Tpng fdot.dot 

or

 /usr/local/bin/dot -o 9.png -Tpng fdot.dot 
+2
source

Instead:

 print os.system("dot -o9.png -Tpng ./6.dot") 

try the following:

 from subprocess import check_call print check_call("dot -o9.png -Tpng ./6.dot") 

If the exit status of the point program is 0, the status is printed. If the point returns a non-zero status, it raises a CalledProcessError (and shows the returned status). If the dot does not exist in the current path, OSError occurs on Linux or WindowsErroor on Windows (I donโ€™t know what exception is raised on Mac OS, but I assume OSError).

EDIT: The above code will give you a hint if you donโ€™t have a point executable or 6.dot file in your current path settings.

+1
source

check_call does not use the same syntax as os.system , so you should try changing the corresponding line as follows:

 print check_call(["dot", "-o9.png", "-Tpng", "./6.dot"]) 

The name of the executable file is the first element in the array, and each parameter must be in a different element of the array. Otherwise, you will always get the error "There is no such file," because your PATH does not have an executable file named "dot -o9.png ...".

+1
source

One problem is this line:

 os.environ['PATH'] += ":"+"/usr/local/bin/dot" 

You do not put the name of the executable in the path, but the directory containing the executable. So this should be:

 os.environ['PATH'] += ":"+"/usr/local/bin" 

And as pointed out in another comment, the check_call arguments check_call not match the os.system .

+1
source

If you use a graphical interface such as Spyder , you can simply add the correct bin path to the PYTHONPATH manager options menu.

Find the location of the script by running this in the terminal:

 which programname 

then take this location (wherever it is), subtract the name of the program, for example:

 /home/username/seiscomp3/bin/scart #this is the section of the path that you use /home/username/seiscomp3/bin 

Then go to the PYTHONPATH manager options menu and add this path. Then restart Spyder and it will work.

0
source

Source: https://habr.com/ru/post/1285800/


All Articles