如何在另一个python脚本中调用manage.py?

2024-06-11 01:51:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个shell脚本,可以多次调用./manage.py,并且希望在python 3.9.2脚本中创建相同的功能。我尝试过subprocess.run和os.system,但由于各种原因挂断了。当前shell脚本如下所示

./manage.py dump_object water_testing.watertest '*' > ./water_testing/fixtures/dump_stevens.json
./manage.py dump_object tp.eqsvc '*' >> ./water_testing/fixtures/dump_stevens.json

... 

剖析下面建议的自定义管理命令需要时间,因此我需要制定一个管理审批的时间表。有谁能解释Django是如何试图用它来解决安全问题的?我们需要一个快速修复的开发和一些指针上的产品。这是我们正在寻找向下和肮脏的时间,所以如果有人有一个工作的例子,这将是可怕的

# `input` args/params are necessary
# `capture_output` is good if we need to do something with it later
# `check` the subprocess actually fired off and completed into traces are crucial.

output = subprocess.run(["manage.py"], input="dump_object water_testing.watertest '*' > ./water_testing/fixtures/dump_stevens.json", capture_output=True, text=True, check=True)

# this won't work either
os.system("python ./manage.py dump_object water_testing.watertest '*' > ./water_testing/fixtures/dump_stevens.json")

也许我们只需要一个关于如何从python脚本调用python脚本的链接,以及一个关于如何分解进程以使解决方案得以实施的提示。提前感谢您的考虑


Tags: py脚本jsontrueoutputobjectmanageshell