You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Poetry集成PyJWT后仍报ModuleNotFoundError求助

问题:Streamlit应用抛出「ModuleNotFoundError: No module named jwt」错误(PyJWT已通过Poetry添加)

已通过Poetry从私有仓库将PyJWT包添加至Python 3.11项目,Docker构建成功,但运行Streamlit应用时仍抛出ModuleNotFoundError: No module named jwt错误。已尝试直接导入jwt,确认无JWT包依赖混淆问题。


相关导入代码

from jwt import decode, get_unverified_header
alg = get_unverified_header(token)["alg"]
decoded_token = decode(token, algorithms=[alg], options={"verify_signature": False})

pyproject.toml 依赖配置

[tool.poetry.dependencies]
python = "^3.11"
streamlit = "1.38.0"
ndg-httpsclient = "^0.5.1"
streamlit-javascript = "^0.1.5"
uuid = "^1.30"
certifi = "2024.7.4"
setuptools = "70.0.0"
idna = "3.7"
pytz = "^2024.1"
pillow = "10.3.0"
pyjwt = "2.8.0"

poetry.lock 中PyJWT相关记录

[[package]]
name = "pyjwt"
version = "2.8.0"
description = "JSON Web Token implementation in Python"
optional = false
python-versions = ">=3.7"
files = [
    {file = "PyJWT-2.8.0-py3-none-any.whl", hash = "sha256:59127c392cc44c2da5bb3192169a91f429924e17aff6534d70fdc02ab3e04320"},
    {file = "PyJWT-2.8.0.tar.gz", hash = "sha256:57e28d156e3d5c10088e0c68abb90bfac3df82b40a71bd0daa20c65ccd5c23de"},
]

[package.extras]
crypto = ["cryptography (>=3.4.0)"]
dev = ["coverage[toml] (==5.0.4)", "cryptography (>=3.4.0)", "pre-commit", "pytest (>=6.0.0,<7.0.0)", "sphinx (>=4.5.0,<5.0.0)", "sphinx-rtd-theme", "zope.interface"]
docs = ["sphinx (>=4.5.0,<5.0.0)", "sphinx-rtd-theme", "zope.interface"]
tests = ["coverage[toml] (==5.0.4)", "pytest (>=6.0.0,<7.0.0)"]

[package.source]
type = "legacy"
url = "https://repo.nexuscloud.aws.rabo.cloud/repository/gr-pypi-13/simple"
reference = "pypi-group"

Dockerfile 配置

FROM <link-to-nexus>/python:3.11.8-slim

WORKDIR /app

RUN useradd -ms /bin/bash portal && chown -R portal /home/portal && chown -R portal /app

COPY pyproject.toml poetry.lock ./

ARG NEXUSCLOUD_USERNAME
ARG NEXUSCLOUD_PASSWORD

RUN pip config --user set global.index-url "https://$NEXUSCLOUD_USERNAME:$NEXUSCLOUD_PASSWORD@<link-to-nexus>/repository/<repo>/simple"

#Should be same as poetry.lock file
RUN pip install poetry==1.7.1 streamlit==1.38.0 streamlit-javascript==0.1.5 --no-cache-dir

RUN poetry config repositories.pypi-group "https://<link-to-nexus>/repository/<repo>/simple" \
  && poetry config http-basic.pypi-group $NEXUSCLOUD_USERNAME $NEXUSCLOUD_PASSWORD

RUN poetry install --no-cache

COPY . .

ENV ENVIRONMENT=""
ENV BASE_API_URL=$BASE_API_URL

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENV PATH=“${PATH}:/root/.local/bin”

USER portal

ENTRYPOINT ["streamlit", "run", "portal/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false", "--server.maxUploadSize=1024", "--client.toolbarMode=minimal", "--client.showErrorDetails=false", "--browser.gatherUsageStats=false"]

排查与解决步骤

1. 统一Poetry安装环境与运行环境

Docker中poetry install默认会创建虚拟环境,但切换到portal用户后,Streamlit可能没有激活该虚拟环境。可以通过两种方式解决:

  • 方案一:直接安装到系统Python
    修改Dockerfile中的Poetry配置,禁用虚拟环境:
    RUN poetry config virtualenvs.create false \
      && poetry install --no-cache --no-root --no-dev
    
  • 方案二:运行时激活虚拟环境
    修改ENTRYPOINT,确保启动Streamlit前激活Poetry虚拟环境:
    ENTRYPOINT ["bash", "-c", "source $HOME/.cache/pypoetry/virtualenvs/$(basename $(pwd))-*/bin/activate && streamlit run portal/streamlit_app.py --server.port=8501 --server.address=0.0.0.0 --server.enableXsrfProtection=false --server.maxUploadSize=1024 --client.toolbarMode=minimal --client.showErrorDetails=false --browser.gatherUsageStats=false"]
    

2. 修复PATH配置问题

当前Dockerfile中ENV PATH=“${PATH}:/root/.local/bin”使用了中文引号,且路径指向root目录,切换到portal用户后无法访问。修改为:

ENV PATH="${PATH}:/home/portal/.local/bin"

同时安装Poetry时加上--user参数,确保安装到portal用户目录:

RUN pip install --user poetry==1.7.1 streamlit==1.38.0 streamlit-javascript==0.1.5 --no-cache-dir

3. 验证PyJWT安装状态

在Dockerfile中添加验证步骤,确认PyJWT已正确安装:

# 安装后立即验证
RUN poetry run python -c "import jwt; print('PyJWT installed successfully, version:', jwt.__version__)"

# 切换用户后再次验证
USER portal
RUN python -c "import jwt; print('PyJWT installed successfully, version:', jwt.__version__)"

4. 排除项目内命名冲突

确认项目目录下不存在同名的jwt.py文件或jwt文件夹,避免覆盖第三方库的导入路径。


内容的提问来源于stack exchange,提问作者Faisal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 10:29:54