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

已安装python-jose但chalice local运行时提示No module named 'jose'求助

ModuleNotFoundError for 'jose' when running chalice local even though python-jose is installed

Problem Description

I've installed the python-jose module, but when running the chalice local command, I still get a ModuleNotFoundError: No module named 'jose' error. Here's the full traceback:

Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/chalice/cli/__init__.py", line 357, in main return cli(obj={}) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 716, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 696, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1060, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 889, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 534, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/click/decorators.py", line 17, in new_func return f(get_current_context(), *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/chalice/cli/__init__.py", line 134, in deploy api_gateway_stage=api_gateway_stage, File "/usr/local/lib/python3.6/dist-packages/chalice/cli/factory.py", line 122, in create_config_obj app_obj = self.load_chalice_app() File "/usr/local/lib/python3.6/dist-packages/chalice/cli/factory.py", line 180, in load_chalice_app app = importlib.import_module('app') File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/sudip/myproj/app.py", line 2, in <module> from chalicelib.common import get_cors_congiguration as get_cors_config File "/home/sudip/abc/dorm.py", line 15, in <module> from jose import jwk, jwt as jjwt ModuleNotFoundError: No module named 'jose'

However, when I run the Python 3.6 console and execute import jose, it works without any errors:

python3.6 Python 3.6.5 (default, Mar 29 2018, 03:28:50) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import jose >>>

Root Cause

This almost always happens because Chalice is using a different Python environment than the one you tested manually. Even if both are Python 3.6, there could be separate environments (like virtual environments, user-specific site-packages vs system-wide, or multiple installed Python 3.6 binaries) where the python-jose module isn't installed in the one Chalice is using.

Solutions

Here are step-by-step fixes to resolve this:

  • Verify which Python interpreter Chalice is using
    Add a quick debug line at the top of your app.py file to print the Python executable path:

    import sys
    print(f"Chalice is using Python: {sys.executable}")
    

    Run chalice local and note the output path (e.g., /usr/bin/python3.6 or /home/sudip/myproj/venv/bin/python3.6). Compare this to the path you get when running which python3.6 in your terminal. If they differ, that's the core issue.

  • Install python-jose in Chalice's environment

    • If Chalice is using a virtual environment, activate it first:
      source /path/to/your/venv/bin/activate  # Linux/macOS
      # or
      \path\to\your\venv\Scripts\activate.bat  # Windows
      
      Then run:
      pip install python-jose
      
    • If it's using a system-wide or different Python binary, use that specific Python's pip to install the module:
      /path/to/chalice/python3.6 -m pip install python-jose
      
      Replace /path/to/chalice/python3.6 with the path you got from the debug print.
  • Ensure python-jose is in your requirements.txt
    Chalice relies on the requirements.txt file for consistent dependency management. Add this line to your project's requirements.txt:

    python-jose>=3.3.0
    

    Then install all dependencies with:

    pip install -r requirements.txt
    

    This guarantees the module is installed in whatever environment you're using for your Chalice project.

  • Double-check for multiple Python 3.6 installations
    Run ls /usr/bin/python3.6* (Linux) or where python3.6 (Windows) to see if there are multiple versions. You might have installed python-jose to /usr/local/bin/python3.6 but Chalice is using /usr/bin/python3.6. Install the module to the correct one using the pip command from the previous step.

Verification

After applying one of these fixes, remove the debug line from app.py and run chalice local again. The ModuleNotFoundError should be resolved if the environment mismatch was the issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:49:02