简体   繁体   中英

How to run Python inside an expressjs Docker container

i am trying to build a container for my express.js application. The express.js-app makes use of python via the npm package PythonShell . I have plenty of python-code, which is in a subfolder of my express-app and with npm start everything works perfectly.

However, i am new to docker and i need to containerize the app. My Dockerfile looks like this:

FROM node:18
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["node", "./bin/www"]

I built the Image with: docker build . -t blahblah-server docker build . -t blahblah-server and ran it with docker run -p 8080:3001 -d blahblah-server .

I make use of imports at the top of the python-script like this:

import datetime
from pathlib import Path  # Used for easier handling of auxiliary file's local path

import pyecma376_2  # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class.
from assi import model

When the pythonscript is executed (only in the container!!!) I get following error-message:

/usr/src/app/public/javascripts/service/pythonService.js:12
          if (err) throw err;
                   ^

PythonShellError: ModuleNotFoundError: No module named 'pyecma376_2'
    at PythonShell.parseError (/usr/src/app/node_modules/python-shell/index.js:295:21)
    at terminateIfNeeded (/usr/src/app/node_modules/python-shell/index.js:190:32)
    at ChildProcess.<anonymous> (/usr/src/app/node_modules/python-shell/index.js:182:13)
    at ChildProcess.emit (node:events:537:28)
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)
    ----- Python Traceback -----
    File "/usr/src/app/public/pythonscripts/myPython/wtf.py", line 6, in <module>
      import pyecma376_2  # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class. {
  traceback: 'Traceback (most recent call last):\n' +
    '  File "/usr/src/app/public/pythonscripts/myPython/wtf.py", line 6, in <module>\n' +
    '    import pyecma376_2  # The base library for Open Packaging Specifications. We will use the OPCCoreProperties class.\n' +
    "ModuleNotFoundError: No module named 'pyecma376_2'\n",
  executable: 'python3',
  options: null,
  script: 'public/pythonscripts/myPython/wtf.py',
  args: null,
  exitCode: 1
}

If I comment the first three imports out, I get the same error:

PythonShellError: ModuleNotFoundError: No module named 'assi'

Please notice, that assi actually is from my own python-code, which is included in the expressjs-app-directory

Python seems to be installed in the container correctly. I stepped inside the container via docker exec -it <container id> /bin/bash and there are the python packages in the #/usr/lib -directory.

I really have absolute no idea how all this works together and why python doesn't find this modules...

You are trying to use libs that are not in Standard Python Library. It seems that you are missing to run pip install , when you build the docker images.

Try adding RUN docker commands that can do this for you. Example:

RUN pip3 install pyecma376_2  
RUN pip3 install /path/to/assi

Maybe, that can solve your problem. Don't forget to check if python are already installed in your container, it semms that it is. And if you have python2 and pyhton3 installed, make sure that you use pip3 instead of only pip.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM