Spawn python3 ENOENT when running a python script in nextjs api

  • netlify site name:
  • problem:
    I have a nextjs app deployed in netlify. it will run a python script when I make a POST request with nextjs api. The python script is in public folder. Everyting works well in development, however when it is deployed in netlify, it will throw a spawn python3 ENOENT if I made the POST request. I have confirmed there is python(3.8.10) installed when deploy and fs.existsSync(scriptPath) returns true.

The full error message is as following:

Error: spawn python3 ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:285:19)
at onErrorNT (node:internal/child_process:485:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)

The building process is also no problem(no errors).

Pls help!

Hi, @Jerenyaoyelu. The function you deploy is running in AWS Lambda. At this time, Netlify Functions support three different language runtimes as documented here:

Quoting that page:

Currently, you can deploy functions built with TypeScript, JavaScript, and Go.

On your local system, you have an entire operating system and all language runtimes installed. Based on your post that obviously included both Node.js and Python.

However, once the function is deployed, Python is no longer available. So, when the Node.js script tries to invoke a Python script, you get the error message from Node.js saying that python3 doesn’t exist. It is expected that python3 doesn’t exist for the deployed function as that language runtime isn’t supported at this time.

If there are other questions about this, please let us know.

hi @luke, thanks for reply. I did not use netlify function in my nextjs app. I only used the nextjs pages/api endpoint, which will trigger a python script. I have set a command in netlify.toml to see the python version when netlify is building.

command = "python3 --version && yarn build"

And I can see the python version of 3.8.10 is logged, so in my understanding python should be available. I understand python is not supported in netlify function(AWS Lambda), but I don’t see why python is not available neither in that case I just use the nextjs pages/api endpoint.

Hi, @Jerenyaoyelu. About this:

That is not true. You may not have been aware that you were using a Function but that is what was used.

The code in question was deployed as a Function. This is a fact that cannot be disputed.

I know this is true because Netlify provides no application runtime for any deployed site outside of the Functions environment. We don’t allow you to run a “server” at Netlify - only Functions. So, if you are running code after the deploy is complete, it can only be a Function (or Edge Function but those never run Python either).

So, the only way to run code at Netlify is as a Function (or as an Edge Function). Once deployed as a function, again, there is no Python language runtime available.

You can use Python during the build and deploy but not after. You won’t be able to run a Python script after the site build is complete at Netlify. It is not possible at this time.

Hi @luke, thanks for the explanation. It looks like what chatgpt told me is not true(it told me my code will run in a netlify server which therotically can support any language :rofl:. So the conclusion is that there is no way to run the python script in apps deployed in netlify, right?

Yup, that’s correct. To run python, outside of build time, at Netlify, is not possible; you’d need to port your code to javascript or go if you want it to run at browse time, or use a different service to host your python script.