Docker镜像安装Prophet失败,报ujson相关语法错误求助
在Ubuntu 18.04 Docker镜像中安装Prophet失败的解决方法
问题概述
基于ubuntu:18.04的Docker镜像中安装Prophet时失败,本地执行pip install prophet可正常完成。报错显示安装cmdstanpy==0.9.68依赖的ujson时,python setup.py egg_info执行失败,触发SyntaxError: future feature annotations is not defined。已安装python3.10、python3-pip及flask、pandas等依赖,尝试先安装ujson再装Prophet仍无法解决。
报错详情
19.18 Collecting ujson (from cmdstanpy==0.9.68->prophet) 19.57 Downloading https://files.pythonhosted.org/packages/21/93/ba928551a83251be01f673755819f95a568cda0bfb9e0859be80086dce93/ujson-4.3.0.tar.gz (7.1MB) 23.28 Complete output from command python setup.py egg_info: 23.28 Traceback (most recent call last): 23.28 File "<string>", line 1, in <module> 23.28 File "/tmp/pip-build-hzpc95u_/ujson/setup.py", line 38, in <module> 23.28 "write_to_template": version_template, 23.28 File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 129, in setup 23.28 return distutils.core.setup(**attrs) 23.28 File "/usr/lib/python3.6/distutils/core.py", line 108, in setup 23.28 _setup_distribution = dist = klass(attrs) 23.28 File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 372, in __init__ 23.28 _Distribution.__init__(self, attrs) 23.28 File "/usr/lib/python3.6/distutils/dist.py", line 281, in __init__ 23.28 self.finalize_options() 23.28 File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 528, in finalize_options 23.28 ep.load()(self, ep.name, value) 23.28 File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2324, in load 23.28 return self.resolve() 23.28 File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2330, in resolve 23.28 module = __import__(self.module_name, fromlist=['__name__'], level=0) 23.28 File "/tmp/pip-build-hzpc95u_/ujson/.eggs/setuptools_scm-7.1.0-py3.6.egg/setuptools_scm/__init__.py", line 5 23.28 from __future__ import annotations 23.28 ^ 23.28 SyntaxError: future feature annotations is not defined 23.28 23.28 ---------------------------------------- 23.69 Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-hzpc95u_/ujson/ ------ failed to solve: process "/bin/sh -c python3 -m pip install prophet" did not complete successfully: exit code: 1
原Dockerfile内容
FROM ubuntu:18.04 RUN apt-get update # To avoid the questions related to the geographic area RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone RUN apt-get install -y python3.10 python3-pip RUN pip3 install flask pandas numpy cython RUN python3 -m pip install prophet COPY app.py app.py COPY checks.py checks.py COPY predict.py predict.py EXPOSE 5000 CMD ["python3", "-u", "app.py"]
解决方法
原因分析
报错核心是from __future__ import annotations语法不被支持,这是因为安装过程中实际调用了系统默认的Python 3.6(从报错路径/usr/lib/python3.6/distutils/core.py可确认),而setuptools_scm-7.1.0需要Python 3.7+的特性。Ubuntu 18.04默认的python3和pip3绑定Python 3.6,即使安装了Python3.10,若不明确指定,仍会调用旧版本导致兼容性问题。
修复后的Dockerfile
FROM ubuntu:18.04 RUN apt-get update && apt-get install -y --no-install-recommends tzdata # 直接设置时区,避免环境变量缺失问题 RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone RUN apt-get install -y python3.10 python3.10-distutils wget # 安装Python3.10专属的pip RUN wget https://bootstrap.pypa.io/get-pip.py && python3.10 get-pip.py && rm get-pip.py # 升级setuptools和pip到兼容版本 RUN python3.10 -m pip install --upgrade setuptools pip # 用Python3.10的pip安装基础依赖 RUN python3.10 -m pip install flask pandas numpy cython # 固定ujson为兼容版本,避免自动拉取高版本 RUN python3.10 -m pip install ujson==4.0.2 # 安装Prophet RUN python3.10 -m pip install prophet COPY app.py app.py COPY checks.py checks.py COPY predict.py predict.py EXPOSE 5000 # 明确用Python3.10启动应用 CMD ["python3.10", "-u", "app.py"]
关键改动说明
- 安装python3.10-distutils:Ubuntu 18.04默认没有Python3.10的distutils组件,缺失会导致无法安装pip。
- 明确调用Python3.10:所有pip安装和命令执行都用
python3.10,彻底避免和系统默认的Python3.6混淆。 - 升级setuptools和pip:旧版本无法处理新依赖包的语法特性,升级后可解决兼容性问题。
- 固定ujson版本:指定兼容Python3.10的ujson版本,避免自动安装高版本触发语法错误。
内容的提问来源于stack exchange,提问作者csymvoul
相关产品推荐
相关产品推荐

