pythondockergoogle-cloud-platformimporterrorta-lib

Python package installed in Google Cloud Platform, but getting import error when running Docker image


I have installed this library called Ta-Lib in my GCP in the following way:

wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib/
./configure --prefix=/usr
make
sudo make install
pip install ta-lib

I can then import this library if I run python in cloud shell and then >>> import talib.

I then proceed to build the docker image of my project with the following Dockerfile:

FROM python:3.9

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Give user access to write into the results folder
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
WORKDIR $HOME/app
COPY --chown=user . $HOME/app

# Run the application:
CMD ["python", "-u", "app.py"]

However, after building the Docker image of my project and trying to run it, it gives the following error:

username@cloudshell:~/RoboAdvisor (asom-barta-qna-bot)$ python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import talib
>>> quit()
username@cloudshell:~/RoboAdvisor (asom-barta-qna-bot)$ docker run robo_advisor
Traceback (most recent call last):
  File "/home/user/app/app.py", line 2, in <module>
    from finnlp.data_processors.yahoofinance import Yahoofinance
  File "/home/user/app/finnlp/data_processors/yahoofinance.py", line 29, in <module>
    from finnlp.data_processors._base import _Base, calc_time_zone
  File "/home/user/app/finnlp/data_processors/_base.py", line 12, in <module>
    import talib
ModuleNotFoundError: No module named 'talib'

I don't understand what is the issue here, and how do I fix it? Why is it saying talib is not installed, when it clearly is?


I created a shell script called install_talib.sh and put it in the directory of my project to install the library like so:

wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz \
  && sudo tar -xzf ta-lib-0.4.0-src.tar.gz \
  && sudo rm ta-lib-0.4.0-src.tar.gz \
  && cd ta-lib/ \
  && sudo ./configure --prefix=/usr \
  && sudo make \
  && sudo make install \
  && cd ~ \
  && sudo rm -rf ta-lib/ \
  && pip install ta-lib

And then I modified my Dockerfile like so:

FROM python:3.9

RUN chmod +x install_talib.sh
RUN ./install_talib.sh

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Give user access to write to write in results folder
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
WORKDIR $HOME/app
COPY --chown=user . $HOME/app

# Run the application:
CMD ["python", "-u", "app.py"]

This time when I try to run it, it gives the following error:

username@cloudshell:~/RoboAdvisor (asom-barta-qna-bot)$ docker build -t robo_advisor .
[+] Building 0.6s (6/13)                                                                                                                                                                                       
 => [internal] load build definition from Dockerfile                                                                                                                                                      0.0s
 => => transferring dockerfile: 490B                                                                                                                                                                      0.0s
 => [internal] load .dockerignore                                                                                                                                                                         0.0s
 => => transferring context: 2B                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/python:3.9                                                                                                                                             0.2s
 => CACHED [1/9] FROM docker.io/library/python:3.9@sha256:98f018a1afd67f2e17a4abd5bfe09b998734ba7c1ee54780e7ed216f8b8095c3                                                                                0.0s
 => CANCELED [internal] load build context                                                                                                                                                                0.3s
 => => transferring context: 21.95MB                                                                                                                                                                      0.3s
 => ERROR [2/9] RUN chmod +x install_talib.sh                                                                                                                                                             0.3s
------
 > [2/9] RUN chmod +x install_talib.sh:
#0 0.237 chmod: cannot access 'install_talib.sh': No such file or directory
------
Dockerfile:3
--------------------
   1 |     FROM python:3.9
   2 |     
   3 | >>> RUN chmod +x install_talib.sh
   4 |     RUN ./install_talib.sh
   5 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c chmod +x install_talib.sh" did not complete successfully: exit code: 1

Solution

  • Adding the following install_talib.sh script in the root directory:

    wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz \
      && tar -xzf ta-lib-0.4.0-src.tar.gz \
      && cd ta-lib/ \
      && ./configure --prefix=/usr \
      && make \
      && make install \
      && pip install ta-lib
    

    And then building the docker image with the following Dockerfile:

    FROM python:3.9
    
    WORKDIR /code
    
    COPY ./requirements.txt /code/requirements.txt
    COPY ./install_talib.sh /code/install_talib.sh
    
    RUN chmod +x /code/install_talib.sh
    RUN /code/install_talib.sh
    
    RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
    
    # Give user access to write to write in results folder
    RUN useradd -m -u 1000 user
    USER user
    ENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
    WORKDIR $HOME/app
    COPY --chown=user . $HOME/app
    
    # Run the application:
    CMD ["python", "-u", "app.py"]
    

    Solves the issue.