Search code examples
pythondjangomatplotlibibm-cloud

Running python IBM Cloud Apps Cloud Foundry with error in ModuleNotFoundError: No module named 'matplotlib'


After deployment in IBM Cloud - App Cloud Foundry using Python - Django I found the follow error running the container:

APP/PROC/WEB    0   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed    
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "<frozen importlib._bootstrap_external>", line 678, in exec_module 
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   callback, param_dict = resolver.resolve_error_handler(500)  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 141, in handle_uncaught_exception   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 103, in response_for_exception  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   response = response_for_exception(request, exc) 
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 49, in inner    
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   response = self._middleware_chain(request)  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in get_response 
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   response = self.get_response(request)   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/django/core/handlers/wsgi.py", line 133, in __call__ 
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   respiter = self.wsgi(environ, resp.start_response)  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   self.handle_request(listener, req, client, addr)    
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   File "/home/vcap/deps/0/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   Traceback (most recent call last):  
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   During handling of the above exception, another exception occurred: 
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   **ModuleNotFoundError: No module named 'matplotlib'**   
13 de mar. de 2021 10:43:24
APP/PROC/WEB    0   import matplotlib.pyplot as plt

In my local environment running ibmcloud dev build and ibmcloud dev run works fine, but after deploy in IBM Cloud the error in log occurs.

Here is my Dockerfile:

FROM registry.access.redhat.com/ubi8:8.3

WORKDIR /app

COPY Pipfile* /app/

## NOTE - rhel enforces user container permissions stronger ##
USER root
RUN yum -y install python3
RUN yum -y install python3-pip wget

RUN pip3 install --upgrade pip==21.0.1 \
  && pip3 install --upgrade pipenv==2020.11.15 \
  && pipenv install --system --deploy

RUN pip3 install numpy
RUN pip3 install wordcloud
RUN pip3 install requests
RUN pip3 install urllib3
RUN pip3 install matplotlib --force
RUN pip3 install six wikipedia-API --force


USER 1001

COPY . /app

ENV PORT 3000

EXPOSE 3000

CMD ["python3", "manage.py", "start"]

Here is my manifest.yml

---
applications:
- instances: 1
  timeout: 180
  name: biblestatistic
  buildpack: python_buildpack
  command: gunicorn --env DJANGO_SETTINGS_MODULE=pythondjangoapp.settings.production pythondjangoapp.wsgi -b 0.0.0.0:$PORT
  memory: 1024MB
  domain: us-south.cf.appdomain.cloud
  host: <my_host>

Solution

  • Your Dockerfile is for running the app as container. With Cloud Foundry you need to really have a requirements.txt with all the modules your app depends on. You also need a runtime.txt to specify the Python version. Check out this Cloud Foundry documentation on the Python buildpack and the required configuration files.