如何从Django web app中在linux shell上启动python子进程命令?

如何从Django web app中在linux shell上启动python子进程命令?,python,linux,django,django-views,Python,Linux,Django,Django Views,我有一个web应用程序,可以读取和显示python测试文件生成的日志文件。目前,我可以从cli启动python文件,并且工作正常 但是,我希望能够从应用程序内部启动python测试 现在我有一个“启动测试”按钮,它调用views.py文件中的一个函数 def launch_tests(request, test_txt_file): test_list_file = test_txt_file launcher2.main(test_list_file) return

我有一个web应用程序,可以读取和显示python测试文件生成的日志文件。目前,我可以从cli启动python文件,并且工作正常

但是,我希望能够从应用程序内部启动python测试

现在我有一个“启动测试”按钮,它调用views.py文件中的一个函数

def launch_tests(request, test_txt_file):
    test_list_file = test_txt_file
    launcher2.main(test_list_file)

    return render_to_response('front_page.html')
文本文件包含要执行的.py文件的名称

在2.main中

import os,time, string, pdb, sys
import getopt,subprocess
import pdb

bi_python_home = '//belfiler1/scratch-disk/conor.doherty/dev/python/bi_python'


def main(test_list_file):

    # if argument passed in for test list file  
    if test_list_file != None:
        test_list = bi_python_home + "/launcher/" + test_list_file
    else:
        # default
        test_list =  bi_python_home + "/launcher/test_list.txt"



    tests_dir =  bi_python_home + "/tests/"
    log_dir =bi_python_home + "/logs/"
    month = '%Y%m'
    day = '%d'
    try :
        for test_to_run in open(test_list):
            sub_dir=os.path.dirname(test_to_run) 
            test_filename = os.path.basename(test_to_run)

            # Create log directory for date if doesn't exist
            cur_log_dir = log_dir + time.strftime(month, time.localtime()) + '/' + time.strftime(month+day, time.localtime()) + '/' + sub_dir 
            if not os.path.exists(cur_log_dir):
                print "creating cur_log_dir "  + cur_log_dir
                os.makedirs(cur_log_dir)


            full_path = string.rstrip(tests_dir + test_to_run)
            #print ' full_path is "' + full_path + '"'
            cmd = 'python ' + full_path


            if os.path.isfile(full_path) == True :
                print'\n'*2
                print "Processing file " + test_to_run,

                log_timestamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
                log_filename = cur_log_dir + '/' + string.rstrip(test_filename) + "_" + log_timestamp + '.log' 
                print 'log file to use is' + log_filename
                # Redirect stdout and stderr to logfile
                cmd = string.rstrip(cmd) + ' > ' + log_filename
                popen_obj = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (stdout, stderr) = popen_obj.communicate()
                print 'executing: ' + cmd
                print 'stderr is: ', stderr





            # if need to debug , remove stream redirection &> above and print stdout, stderr

        #else :
            #print 'ignoring ' , full_path



except IOError, err:
    print str(err) 
    sys.exit(2)

除了一件事之外,代码运行良好。当我启动子进程时,默认情况下它似乎是在windows上执行的。由于测试使用一个名为pexpect的模块,我需要在linux上执行它

我一直认为必须有一个简单的解决办法,但到目前为止我没有运气

任何帮助都将不胜感激


谢谢

这里没有足够的信息给出正确的答案,但这里的一个常见问题是web前端(例如apache)的运行用户与您的运行用户不同。因此,web处理程序没有权限准备主目录中的文件。它可能还缺少一些重要的环境变量,例如家庭


首先,看看这个。web服务器日志怎么说?

我不太明白这个问题。问题是它不能在linux中执行吗?错误是什么?是的,基本上这就是问题所在。如果我在LinuxCLI甚至cygwin中启动该文件,效果会很好。但是当我在应用程序中尝试时,我得到了一个错误:“没有找到一个关键模块。可能这个操作系统不支持它。Pexpect适用于类似UNIX的操作系统。”“当我启动子进程时,它似乎默认在windows上执行。”-你能详细说明一下这句话背后的原因吗?