gpt4 book ai didi

python - 如何在Azure中部署运行python脚本的node.js应用程序?

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:51 25 4
gpt4 key购买 nike

我正在尝试部署一个 node.js 应用程序,该应用程序调用 python 脚本来执行后台任务。我实现它的方式是通过 python-shell:

var pythonShell = require('python-shell');

var options = {
pythonPath: 've-env/bin/python3',
args:
[
req.query.term,
req.params.id,
req.session.user.searchName,
req.session.user.searchKey
]
};

pythonShell.run('VideoAnalyzer/Search.py', options, function (err, data) {
if (err)
throw err ;
var values = JSON.parse(data[0]).value;
var resultsByRel = values;
res.render('video', {resultsByRel: resultsByRel, resultsByTime: [], searchTerm: req.query.term, url: req.query.url});
});

Python 的路径在 options.pythonPath 中指定(在名为“ve-env”的 Python 虚拟环境中)。

这适用于我的本地环境。但是,当我将应用程序部署到 Azure 应用服务时,收到以下错误消息:

Error: spawn Unknown system error -8
at _errnoException (util.js:992:11)
at ChildProcess.spawn (internal/child_process.js:323:11)
at exports.spawn (child_process.js:502:9)
at new PythonShell (/home/site/wwwroot/node_modules/python-shell/index.js:59:25)
at Function.PythonShell.run (/home/site/wwwroot/node_modules/python-shell/index.js:160:19)
at Object.exports.search_result_video (/home/site/wwwroot/controllers/searchController.js:20:15)
at /home/site/wwwroot/routes/video.js:15:21
at Layer.handle [as handle_request] (/home/site/wwwroot/node_modules/express/lib/router/layer.js:95:5)
at next (/home/site/wwwroot/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/site/wwwroot/node_modules/express/lib/router/route.js:112:3)

该应用部署在Linux环境中, Node 版本为v8.9。

在部署之前我应该​​执行任何Python环境配置吗?

最佳答案

Is there any python environment configuration I should have performed before deployment?

答案是肯定的。如果您想在Azure App Service中运行python脚本,您需要在您的应用程序中安装python环境。请引用以下步骤:

请引用我的工作步骤,看看错误是否仍然出现。

第1步:在门户上添加扩展(这里是Python 3.6.1 x64)

enter image description here

第2步:切换到Kudu CMD并命令cd Python364x64touch get-pip.py并复制url内容https://通过编辑按钮将 bootstrap.pypa.io/get-pip.py 复制到 get-pip.py 中,然后运行 ​​python get-pip.py 进行安装点工具。

enter image description here

第 3 步:通过 python -m pip install requests 安装您需要的任何软件包

enter image description here

然后你需要修改代码的python配置:

var express = require('express');
var pythonShell = require('python-shell');
var router = express.Router();

var options = {
pythonPath: 'D:/home/python364x64/python',
scriptPath: 'D:/home/site/wwwroot'
// args:
// [
// req.query.term,
// req.params.id,
// req.session.user.searchName,
// req.session.user.searchKey
// ]
};

var resultsByRel;

pythonShell.run('TestRequests.py', options, function (err, data) {
if (err) throw err;
// results is an array consisting of messages collected during execution
var values = JSON.parse(data[0]);
resultsByRel = values;
console.log('results: %j', resultsByRel);
});

router.get('/', function(req, res, next) {
res.send(resultsByRel);
// res.render('executePython', resultsByRel );
});

module.exports = router;

我的简单 python 脚本:

import requests

r= requests.get("https://www.bing.com")
print (r.status_code)

希望对您有帮助。如有任何疑问,请告诉我。

关于python - 如何在Azure中部署运行python脚本的node.js应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com