已安装python-jose但chalice local运行时提示No module named 'jose'求助
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 yourapp.pyfile to print the Python executable path:import sys print(f"Chalice is using Python: {sys.executable}")Run
chalice localand note the output path (e.g.,/usr/bin/python3.6or/home/sudip/myproj/venv/bin/python3.6). Compare this to the path you get when runningwhich python3.6in your terminal. If they differ, that's the core issue.Install
python-josein Chalice's environment- If Chalice is using a virtual environment, activate it first:
Then run:source /path/to/your/venv/bin/activate # Linux/macOS # or \path\to\your\venv\Scripts\activate.bat # Windowspip install python-jose - If it's using a system-wide or different Python binary, use that specific Python's pip to install the module:
Replace/path/to/chalice/python3.6 -m pip install python-jose/path/to/chalice/python3.6with the path you got from the debug print.
- If Chalice is using a virtual environment, activate it first:
Ensure
python-joseis in yourrequirements.txt
Chalice relies on therequirements.txtfile for consistent dependency management. Add this line to your project'srequirements.txt:python-jose>=3.3.0Then install all dependencies with:
pip install -r requirements.txtThis guarantees the module is installed in whatever environment you're using for your Chalice project.
Double-check for multiple Python 3.6 installations
Runls /usr/bin/python3.6*(Linux) orwhere python3.6(Windows) to see if there are multiple versions. You might have installedpython-joseto/usr/local/bin/python3.6but 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

