如何运行“dot” 作为来自 Python 的命令?

发布于 2024-07-23 10:36:21 字数 704 浏览 7 评论 0原文

我在 Mac OSX Leopard 上使用 Python。

我正在尝试从 Python 运行程序“dot”(Graphviz 的一部分):

# -*- coding: utf-8 -*-
import os

print os.environ['PATH']
print os.system("ls >> a.txt")
print os.system("dot -o9.png -Tpng ./6.dot")

命令“ls”只是为了确保 python 位于正确的目录中。 这是。 我得到的结果是:

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

我的理解是 32512 错误意味着 python 找不到该文件,并且由于文件 6.dot 在那里(如果我从终端运行“dot -o9.png -Tpng ./6.dot”,我不会收到错误,并且生成了 9.png),我假设 Python 找不到点文件。

我可能需要将点文件添加到路径中。 但我不知道它在哪里。 如果我跑步:

whereis dot

我没有收到任何答复。

如何找到 dot 可执行文件?
或者,我可以从 Python 内部将 dot 程序作为命令运行吗?

I am using Python on Mac OSX Leopard.

I am trying to run the program 'dot' (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 command "ls" is there just to make sure that python is in the correct directory. It is. The result that I get is:

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

My understanding is that 32512 error means that python could not find the file, and since the file 6.dot is there (If I run "dot -o9.png -Tpng ./6.dot" from the terminal I receive no error, and 9.png gets produced), I assume Python can't find the dot file.

I probably need to add the dot file to the path. But I don't know where is it. If I run:

whereis dot

I receive no answer.

How can I find the dot executable?
Alternatively, can I run the dot program as a command from inside Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

┾廆蒐ゝ 2024-07-30 10:36:21

whereis 定位手册页,which 定位二进制文​​件。 所以尝试哪个点

whereis locates man pages, which locates binaries. So try which dot.

失与倦" 2024-07-30 10:36:21

您需要在 Python 环境中添加“dot”可执行文件的路径。 您可以通过修改 os.environ 中的 PATH 变量来做到这一点

You need to add the path to the 'dot' executable in Python's environment. You can do this by modifying the PATH variable in os.environ

国际总奸 2024-07-30 10:36:21

试试这个:

# -*- 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"])

取自问题,尝试在这里保持某种理智。

Try this:

# -*- 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 to try and maintain some sort of sanity here.

坏尐絯℡ 2024-07-30 10:36:21

您应该更改 PATH 行,使其包含包含 dot 的目录。 该目录是 /usr/local/bin,没有 /dot

You should change the PATH line so it includes the directory which contains dot. That directory is /usr/local/bin, without /dot.

俯瞰星空 2024-07-30 10:36:21

疑难解答提示:

添加

print os.getcwd()

A.在 os.system("dot 等) 之前

只是为了确保当前目录是带有 6.dot 的目录 文件。

确保 dot 程序位于您的路径中。

which dot

C. 使用 dot 的完整路径。 > 在 os.system 命令中进行编程,看看会发生什么。

Troubleshooting tips:

A. add

print os.getcwd()

on the line before os.system("dot etc.

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

B. Make sure 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 then.

月朦胧 2024-07-30 10:36:21

如果您还使用 Python 生成 Dot 文件,pydot 会以更 Python 的方式执行您想要的操作方式:

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')

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')
冬天的雪花 2024-07-30 10:36:21

通常解决方案就在我们面前,

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

您也可以尝试指定文件夹中的所有点

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)

编辑:

我不记得顺便说一句,如果是

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

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

Often the solution is in front of us,

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

Also you can try for all the dots in a 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 cannot recall btw if it is

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

or

/usr/local/bin/dot -o 9.png -Tpng fdot.dot
脸赞 2024-07-30 10:36:21

两个建议

  1. 不要使用 PATH,而是使用“which”来查找可执行文件,而
  2. 不是使用“;” (分号)来分隔路径,但使用“:”(冒号)。 一旦你改变了这个,它应该能够找到你的点程序。

把这个改成这个

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

然后

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

就好了。

编辑:请注意,我自己忘记从 PATH 变量中删除 /dot (哎呀) - PATH 是冒号分隔的目录列表。

Two suggestions

  1. Don't use PATH, instead use "which" to just find the executable instead
  2. You don't use ";" (semi-colon) to separate paths, but ":" (colon). Once you change this it should be able to find your dot program.

Change this

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

to this

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

Then your good.

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

单身狗的梦 2024-07-30 10:36:21

而不是:

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

试试这个:

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

如果点程序的退出状态为0,则打印状态。 如果 dot 返回非零状态,则会引发 CalledProcessError (并显示返回的状态)。 如果当前路径中不存在点,则在 Linux 上引发 OSError 或在 Windows 上引发 WindowsError (我不知道在 Mac OS 下引发哪个异常,但我假设 OSError)。

编辑:如果当前路径设置中没有 dot 可执行文件或 6.dot 文件,上面的代码会给您提示。

Instead of:

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

try this:

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

If exit status of dot program is 0, the status is printed. If dot returns non-zero status, it raises CalledProcessError (and shows returned status). If dot doesn't exist in current path, OSError is raised on Linux or WindowsErroor on Windows (I don't know which exception is raised under Mac OS, but I assume OSError).

EDIT: Code above will give you the hint if you have no dot executable or 6.dot file in current path settings.

厌味 2024-07-30 10:36:21

check_call 不使用与 os.system 相同的语法,因此您应该尝试以这种方式更改相应的行:

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

可执行文件名称是数组中的第一项,并且每个参数必须位于数组的另一项中。 否则,您将总是收到“没有这样的文件”错误,因为您的路径中没有名为“dot -o9.png ...”的可执行文件。

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

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

The executable name is the first item in the array, and each parameter must be in another item of the array. Otherwise you will always get a "No such file" error because there is no executable named "dot -o9.png ..." in your PATH.

面如桃花 2024-07-30 10:36:21

这一行有一个问题:

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

您没有将可执行文件的名称放入路径中,而是将包含可执行文件的目录放入其中。 所以应该是:

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

正如另一条评论中指出的,check_call 的参数与 os.system 不同。

One problem is in this line:

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

You don't put the name of the executable in the path, but the directory containing the executable. So that should be:

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

And as pointed out in another comment, the arguments to check_call are not the same as os.system.

凉栀 2024-07-30 10:36:21

如果您使用的是 Spyder 等 GUI,那么您只需将正确的 bin 路径添加到 PYTHONPATH manager 选项菜单中即可。

通过在终端中执行以下操作来搜索脚本位置:

which programname

然后获取该位置(无论它在哪里),减去程序名称,例如:

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

然后进入 PYTHONPATH manager 选项菜单并添加此路径。 然后重新启动Spyder就可以了。

If you are using a GUI such as Spyder then you can simply add the correct bin path into the PYTHONPATH manager options menu.

Search for the script location by doing this in the terminal:

which programname

then take that location (wherever it is), subtract the programname, for example:

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

Then go into the PYTHONPATH manager options menu and add this path. Then restart Spyder and it'll work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文