boto3 Python模块已安装,但无法加载

-1 投票
2 回答
60 浏览
提问于 2025-04-13 02:59

通过 homebrew 安装了 python3pip3。在 pip3pip 中安装了 boto3 模块,并且可以在列表中看到这个模块,但在 python3 的脚本中却无法导入这个模块。

$ # DEBUG INFO
$
$ which python3 pip3
/opt/homebrew/bin/python3
/opt/homebrew/bin/pip3
$
$ python3 --version ; pip3 --version
Python 3.11.6
pip 24.0 from /opt/homebrew/lib/python3.10/site-packages/pip (python 3.10)
$
$ pip3 install boto3
Requirement already satisfied: boto3 in /opt/homebrew/lib/python3.10/site-packages (1.18.36)
Requirement already satisfied: botocore<1.22.0,>=1.21.36 in /opt/homebrew/lib/python3.10/site-packages (from boto3) (1.21.65)
[...]
$
$ pip3 list | grep boto3
pip3 list | grep boto3
boto3                     1.18.36
$
$ python3
Python 3.11.6 (main, Nov  2 2023, 04:39:43) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
import boto3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'boto3'
>>> quit()

2 个回答

1

你正在使用的是python3.11,但看起来boto3这个库是保存在python3.10里的。

pip3 install boto3
Requirement already satisfied: boto3 in /opt/homebrew/lib/`python3.10`/site-packages (1.18.36)
Requirement already satisfied: botocore<1.22.0,>=1.21.36 in /opt/homebrew/lib/`python3.10`/site-packages (from boto3) (1.21.65)

python3
Python `3.11.6` (main, Nov  2 2023, 04:39:43) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

希望这能帮到你!

2

首先,建议你开始使用虚拟环境。可以参考这个回答:https://stackoverflow.com/a/35017813/17184842

你现在遇到的问题是:

  1. Python 3.11.6 安装在 /opt/homebrew/bin/python3 这个位置。
  2. pip 24.0 是为 Python 3.10 安装的,位置在 /opt/homebrew/lib/python3.10/site-packages。

如果你想在 Python 3.11.6 上使用 boto3,你需要为这个特定版本的 Python 安装它。你可以通过运行以下命令来做到这一点:

/opt/homebrew/bin/python3 -m pip install boto3

这样就会为 Python 3.11.6 安装 boto3。

如果你想明确调用 Python 3.11,可以使用以下命令:

/opt/homebrew/bin/python3

撰写回答