我有一个在 docker 容器中运行的Python项目,但我无法让convert_from_path
运行(从pdf2image
库).它在我的Windows PC上本地运行,但不能在基于Linux的坞站容器中运行.
我每次得到的误差是Unable to get page count. Is poppler installed and in PATH?
我的代码的相关部分如下所示
from pdf2image import convert_from_path
import os
from sys import exit
def my_function(file_source_path):
try:
pages = convert_from_path(file_source_path, 600, poppler_path=os.environ.get('POPPLER_PATH'))
except Exception as e:
print('Fail 1')
print(e)
try:
pages = convert_from_path(file_source_path, 600)
except Exception as e:
print('Fail 2')
print(e)
try:
pages = convert_from_path(file_source_path, 600, poppler_path=r'\usr\local\bin')
except Exception as e:
print('Fail 3')
print(e)
print(os.environ)
exit('Exiting script')
在try 1中,我try 引用保存在Windows上的原始文件.基本上,路径是指'/code/poppler'
,这是指
[snippet from docker-compose.yml]
- type: bind
source: "C:/Program Files/poppler-0.68.0/bin"
target: /code/poppler
在try 2中,我只是try 将路径留空.在try 3中,我try 了一些我发现在本地其他用户处起作用的东西.
我的文档文件的相关部分如下所示
FROM python:3.10
WORKDIR /code
# install poppler
RUN apt-get update
RUN apt-get install poppler-utils -y
COPY ./requirements.txt ./
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "./app.py"]