Next.js Discord

Discord Forum

How to exec a python script from NextJS?

Answered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
I tried to use spawn to call python script, it works on my local, but it doesn't work on Vercel after deployment.

I put the py script on the root folder API folder

it shows error: Error: Unable to find any supported Python versions.
Answered by Laysan Albatross
you can use Vercel AI SDK (https://sdk.vercel.ai/docs)
View full answer

32 Replies

@!=tgt what do you want to do?
Sloth bearOP
bascially i have a python script, I want to exec it from nodejs, and return it back to FE.
Laysan Albatross
@Laysan Albatross https://vercel.com/templates/python/python-hello-world
Sloth bearOP
i tried it, it is for a Python server only, not with nodejs
Laysan Albatross
oh sry
I know that its a common practice to use python Flask as Backend for Next.js but thats not really what you what since you have a nodejs backend
you could use child processes (https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)

const { exec } = require('child_process');

exec('"file/test.sh" arg1 arg2');
@Laysan Albatross you could use child processes (https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) jsx const { exec } = require('child_process'); exec('"file/test.sh" arg1 arg2');
Sloth bearOP
Thanks @Laysan Albatross ! That is my plan, using spawn/child_process. But the issue is it seems no Python env is there,
const pyprog = spawn("python3", ["sample-script.py"]);
Laysan Albatross
littlebit off topic, but why is it important that it is a python script? wouldn't it be better to use javascript instead?
@Laysan Albatross littlebit off topic, but why is it important that it is a python script? wouldn't it be better to use javascript instead?
Sloth bearOP
Yea, you are right. I am trying to use AutoGen framework for multi agent case, but it is only available for Python
Laysan Albatross
you can use Vercel AI SDK (https://sdk.vercel.ai/docs)
Answer
Laysan Albatross
but since I dont know how to answer your question, and can only deliver workarounds, Imma be outta here 😄
So
Can you give me a bit more of info?
About what you want to do and what you expect from your project?
Sloth bearOP
@Masai Lion sure. My current code is based on NodeJS on backend. I want to use the AutoGen framework for multi agent (OpenAI)which is only available on Python. So I am thinking if my nodeJs code can directly execute a python script, it will resolve my problem
Laysan Albatross
but that sounds like it makes much more sense to use Vercel AI SDK instead of AutoGen and javascript instead of python unless there's a good reason to use AutoGen with python.
Btw if you’re planning to host on vercel
You can deploy Python code to vercel
It can simplify some scenarios
@linesofcode You can deploy Python code to vercel
Masai Lion
You are so very master intelligent
@Masai Lion <@726565187603267654> it’s better to do this
Sloth bearOP
I did a quick check on their doc, does it support multi agent ?
@Sloth bear I did a quick check on their doc, does it support multi agent ?
Masai Lion
I’m not sure
But my colleges might know
Laysan Albatross
seems like it does
Sloth bearOP
@Laysan Albatross thanks!!!
Laysan Albatross
no worries dont forget to mark the answer as solution