Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module not found when using Docker

Tags:

python

docker

I have an Angular-Flask application that I'm trying to Dockerize. One of the Python files used is a file called trial.py. This file uses a module called _trial.pyd. When I run the project on local everything works fine. However, when I dockerize using the following code and run, it gives me error "Module _trial not found".

FROM node:latest as node
COPY . /APP
COPY package.json /APP/package.json
WORKDIR /APP
RUN npm install
RUN npm install -g @angular/[email protected]
CMD ng build --base-href /static/

FROM python:3.6
WORKDIR /root/
COPY --from=0 /APP/ .
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]

Where am I going wrong? Do I need to change some path in my dockerfile?

EDIT: Directory structure:

  • APP [folder with src Angular files]
  • static [folder with js files]
  • templates [folder with index.html]
  • _trial.pyd
  • app.py [Starting point of execution]
  • Dockerfile
  • package.json
  • requirements.txt
  • trial.py

app.py calls trial.py (successfully).

trial.py needs _trial.pyd. I try to call it using import _trial.

It works fine locally, but when I dockerize it, i get the following error message:

Traceback (most recent call last):

File "app.py", line 7, in

from trial import *

File "/root/trial.py", line 15, in

import _trial

ModuleNotFoundError: No module named '_trial'

The commands run are:

docker image build -t prj .

docker container run --publish 5000:5000 --name prj prj

UPDATE It is able to recognize other .py files but not .pyd module.

like image 679
Nitesh Goyal Avatar asked Jul 05 '26 07:07

Nitesh Goyal


1 Answers

Solved it! The issue was that I was using .pyd files in a Linux container. However, it seems that Linux doesn't support .pyd files. This is why it was not able to detect the _trial.pyd module. I had to generate a shared object (.so) file (i.e. _trial.so) and it worked fine.

EDIT: The .so file was generated on a Linux system by me. Generating .so on Windows and then using it in a Linux container gives "invalid ELF header" error.

like image 175
Nitesh Goyal Avatar answered Jul 07 '26 20:07

Nitesh Goyal