起初,我使用 Python 运行时创建了一个新项目,并使用 Flask 公开了一些 API 端点。其中一种方法使用 Python 库(tabula-py),我在这里读到因为 tabula-py 需要 Java8+,所以我必须使用具有自定义运行时间的灵活环境。
所以我做到了,我制作了一个 Dockerfile(如下所示),但不幸的是,在将应用程序部署到 gcloud 时仍然出现此错误。我不得不说,本地代码运行良好,但是当我使用“gcloud app deploy”时,我得到了这个错误。
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/main.py", line 4, in <module>
import tabula
ModuleNotFoundError: No module named 'tabula'
主文件
import tabula
.
.
.
df = tabula.read_pdf(str(latest_file), pages=1) ## transforming into list of dataframes.
应用程序.yaml
runtime: custom
env: flex
env_variables:
FLASK_APP : 'main.py'
Dockerfile
FROM python:3
RUN pip uninstall tabula && \
pip install --upgrade pip && \
pip install --no-cache-dir Flask pyvirtualdisplay python-environ Datetime && \
pip install --no-cache-dir glob3 pandas-gbq pandas schedule && \
pip install --no-cache-dir tabula-py beautifulsoup4 Datetime urllib3 && \
pip install --no-cache-dir gunicorn Werkzeug && \
pip install --upgrade pip --user && \
pip3 uninstall -y tabula-py && \
pip3 install tabula-py
### 1. Get Linux
FROM alpine:3.7
### 2. Get Java via the package manager
RUN apk update \
&& apk upgrade \
&& apk add --no-cache bash \
&& apk add --no-cache --virtual=build-dependencies unzip \
&& apk add --no-cache curl \
&& apk add --no-cache openjdk8-jre
### 3. Get Python, PIP
RUN apk add --no-cache python3 \
&& python3 -m ensurepip \
&& pip3 install --upgrade pip setuptools \
&& rm -r /usr/lib/python*/ensurepip && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
ENV FLASK_APP main.py
ENV FLASK_RUN_HOST 0.0.0.0
ENV FLASK_RUN_PORT 8080
### Get Flask for the app
RUN pip install --trusted-host pypi.python.org flask
####
#### OPTIONAL : 4. SET JAVA_HOME environment variable, uncomment the line below if you need it
#ENV JAVA_HOME="/usr/lib/jvm/java-1.8-openjdk"
####
EXPOSE 8080
ADD main.py /
CMD ["flask", "run"]