我以这种方式通过子进程从Django应用运行外部脚本:
class ExecutePythonFileView(View):
def get(self, request):
# Execute script
script_path = os.path.join(settings.BASE_DIR, '/Code/zenet/zenet/workers/stats_scraper.py')
subprocess.call(['python', script_path])
# Return response
return HttpResponse("Executed!")
我需要通过Django虚拟环境执行它,我该如何进行?
答案 0 :(得分:1)
您有两种选择,
选项1:
选项2:
import sys
class ExecutePythonFileView(View):
def get(self, request):
# Execute script
script_path = os.path.join(settings.BASE_DIR, '/Code/zenet/zenet/workers/stats_scraper.py')
subprocess.call([sys.executable, script_path])
# Return response
return HttpResponse("Executed!")