Python包安装在Google Cloud Platform中,但在运行Docker镜像时出现导入错误

cuxqih21  于 2023-06-21  发布在  Docker
关注(0)|答案(1)|浏览(141)

我在我的GCP中安装了这个名为Ta-Lib的库,方法如下:

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

如果我在cloud shell中运行python,然后运行>>> import talib,我就可以导入这个库。
然后,我继续构建我的项目的docker镜像,使用以下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"]

然而,在构建我的项目的Docker镜像并尝试运行它之后,它给出了以下错误:

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'

我不明白这里的问题是什么,我如何解决它?为什么它说talib没有安装,而它显然是?
我创建了一个名为install_talib.sh的shell脚本,并将其放在我的项目目录中,以安装库,如下所示:

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

然后我修改了我的Dockerfile如下:

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"]

这一次,当我尝试运行它时,它给出了以下错误:

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
kokeuurv

kokeuurv1#

在根目录中添加以下install_talib.sh脚本:

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

然后使用以下Dockerfile构建docker镜像:

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"]

解决了问题。

相关问题