Nitesh Goyal
Nitesh Goyal

Reputation: 147

Python module not found when using 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.

Upvotes: 3

Views: 1920

Answers (2)

Nitesh Goyal
Nitesh Goyal

Reputation: 147

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.

Upvotes: 3

CodeCupboard
CodeCupboard

Reputation: 1585

I think this could be due to the following reasons

Path

Make sure the required file is in the pythonpath. It sounds like you have done that so probably not this one.

Debug Mode

If you are working with a debug mode you will need to rename this module "_trail.pyd" to "_trail_d.pyd"

Missing DLL

The .pyd file may require a dll or other dependency that is not available and cant be imported due to that. there are tools such as depends.exe or this that allow you to find what is required

Name Space Issue

If there was a file already called "_trail.py" in the python path that could create unwanted behavior.

Upvotes: 2

Related Questions