【发布时间】:2015-07-10 11:51:31
【问题描述】:
我正在尝试从我的 Django 应用程序中执行 Python 脚本。我尝试从终端执行它并且它工作正常,所以我知道被调用的 Python 脚本工作正常。 Python 脚本调用一个覆盖服务器上文件的外部程序。外部程序具有所有必需的权限。我什至尝试对所有涉及的文件使用 777 权限,但仍然没有用。
Django 视图:
import subprocess
subprocess.call("python " + script_dir+"testScript.py", shell=True)
Python - 2.7
我可以执行更简单的命令,例如
subprocess.call("rm " + script_dir+"test.txt", shell=True)
编辑: 我不仅要从终端执行 python 脚本,还需要执行一个搅拌机脚本,它本身也可以正常工作(直接从终端执行时),但不会从 django 中生成所需的输出。
这是我的 Django 控制台中的错误:
OSError at /getObj/
[Errno 2] No such file or directory
Request Method: GET
Request URL: http://192.168.1.21:8081/getObj/?width=5.0&height=6.0&depth=7.0
Django Version: 1.7.8
Exception Type: OSError
Exception Value:
[Errno 2] No such file or directory
Exception Location: /usr/lib/python2.7/subprocess.py in _execute_child, line 1327
Python Executable: /usr/bin/python
Python Version: 2.7.6
【问题讨论】:
-
为什么要通过
subprocess调用Python脚本?为什么不导入直接调用呢? -
实际上我需要从 Django 应用程序运行 3 个脚本,其中 2 个不是直接的 python 脚本。所以无论如何我都需要使用子进程。虽然你确实有一点。
-
你遇到了什么错误?
-
您指定了错误的文件路径。写入以记录字符串
script_dir+"testScript.py"确保它是绝对的 -
我在函数调用之前检查了路径。确实是绝对的。
标签: python django python-2.7