无法从django执行外部python脚本。

时间:2021-01-05 01:14:17

I am trying to execute a Python script from within my Django app. I tried executing it from the terminal and it works correctly, so I know the called Python script is working fine. The Python scripts calls an external program which overwrites a file on the server. The external program has all required permissions. I even tried using 777 permissions for all files involved, but still no use.

我正在尝试在Django应用程序中执行Python脚本。我尝试在终端上执行它,它工作正常,所以我知道所谓的Python脚本运行得很好。Python脚本调用一个外部程序,该程序覆盖服务器上的文件。外部程序具有所有必需的权限。我甚至尝试对所有涉及的文件使用777权限,但仍然没有用。

Django View:

Django的观点:

import subprocess
subprocess.call("python " + script_dir+"testScript.py", shell=True)

Python - 2.7

Python - 2.7

I can execute simpler commands like

我可以执行更简单的命令,比如

subprocess.call("rm " + script_dir+"test.txt", shell=True)

EDIT: I am not only executing python scripts from the terminal, I also need to execute a blender script, which again works fine by itself(when executed directly from the terminal) but does not generate the required output from within django.

编辑:我不仅要从终端执行python脚本,还需要执行一个blender脚本,它本身也可以工作得很好(直接从终端执行时),而且不从django中生成所需的输出。

This was the error as in my Django console :

这就是我的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

1 个解决方案

#1


1  

First, I'd suggest to run Python code by importing it, not running it in a separate process (unless you have special reasons to do that). Since you mentioned other non-Python scripts you want to run, I'll suggest a generic way.

首先,我建议通过导入Python代码来运行它,而不是在一个单独的进程中运行它(除非您有特殊的理由这样做)。由于您提到了希望运行的其他非python脚本,因此我将建议一种通用的方式。

I would try and run the script using Popen like this:

我会尝试使用Popen这样运行脚本:

from subprocess import Popen, PIPE

# ...

p = Popen(["python", "testScript.py"], cwd=script_dir, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()

Then you have standard output in out and standard error output in err. Even if it doesn't run successfully, you have something to work with after inspecting the two variables.

然后是标准输出和标准错误输出。即使它没有成功运行,在检查这两个变量之后,您仍然有一些要处理的东西。

#1


1  

First, I'd suggest to run Python code by importing it, not running it in a separate process (unless you have special reasons to do that). Since you mentioned other non-Python scripts you want to run, I'll suggest a generic way.

首先,我建议通过导入Python代码来运行它,而不是在一个单独的进程中运行它(除非您有特殊的理由这样做)。由于您提到了希望运行的其他非python脚本,因此我将建议一种通用的方式。

I would try and run the script using Popen like this:

我会尝试使用Popen这样运行脚本:

from subprocess import Popen, PIPE

# ...

p = Popen(["python", "testScript.py"], cwd=script_dir, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()

Then you have standard output in out and standard error output in err. Even if it doesn't run successfully, you have something to work with after inspecting the two variables.

然后是标准输出和标准错误输出。即使它没有成功运行,在检查这两个变量之后,您仍然有一些要处理的东西。