1

I'm deploying node.js web application on custom runtime with flex environment. I'm calling child_process in Node.js to open python3 as such:

const spawn = require("child_process").spawn;
pythonProcess = spawn('python3');

Which runs fine locally but when deployed to GAE, it gives me an error as such:

Error: spawn python3 ENOENT
   at Process.ChildProcess._handle.onexit (child_process.js:240)
   at onErrorNT (internal/child_process.js:415)
   at process._tickCallback (next_tick.js:63)

However, when I run python2, it works fine.

After doing some research and digging, I came across this question on stackoverflow

How to install Python3 in Google Cloud Platform for a Node app

It seems that I have to do something with building custom runtime from docker file to allow multiple runtimes (something like that).

I've tried countless things with docker file such as:

# Trying to install python3
FROM ubuntu as stage0
WORKDIR /python/
RUN apt-get update || : && apt-get install --yes python3;
RUN apt-get install python3-pip -y

# My main node.js docker stuff 
FROM gcr.io/google_appengine/nodejs
COPY . /app/
... etc

and

# From google app engine python runtime docker repo
FROM gcr.io/google-appengine/python
RUN virtualenv /env
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ADD . /app

# My main node.js docker stuff 
FROM gcr.io/google_appengine/nodejs
COPY . /app/
... etc

which none of it worked.

What is the correct approach of doing this and how can I do it?

Thank you.

A. Park
  • 94
  • 1
  • 10

1 Answers1

2

Google's image is based on ubuntu but only has python 2 and 2.7. This answer showed how to use python3.6, but we're going to install 3.5 it via software-properties-common. Putting things together, you get:

FROM launcher.gcr.io/google/nodejs
# same as
# FROM gcr.io/google-appengine/nodejs


RUN apt-get update && apt-get install software-properties-common -y
# RUN unlink /usr/bin/python
# RUN ln -sv /usr/bin/python3.5 /usr/bin/python
# RUN python -V
RUN python3 -V

# Copy application code.
COPY . /app/

# Install dependencies.
RUN npm --unsafe-perm install

If you're just going to call python3 from your spawn, you don't need to unlink (commented lines) which I included so that you can just call python.

zavr
  • 2,049
  • 2
  • 18
  • 28
  • Wow, that did it, thanks!! Also one more question if you dont mind. Would you also know how to install python3 libraries and use them as well? Thanks!! – A. Park Mar 04 '20 at 04:33
  • 1
    hey... for python you need pip as package manager, same as NPM for node. maybe google around it. i was never quite sure how to deal with multiple pythons and pips for them sorry :( i'll have a look when there's a spare min – zavr Mar 05 '20 at 05:00