我通过 pip 安装了 pyyaml,但是当我 运行 我的 Python3 脚本时,我不是它找不到模块

I installed pyyaml via pip, but when I run my Python3 script, I'm not it can't find the module

我在 Mac OS X 大苏尔。我通过 brew 安装了 python3,并且有这个版本

$ python3 --version
Python 3.9.13

Pip 版本是

$ pip3 --version
pip 22.1.1 from /Users/davea/Library/Python/3.8/lib/python/site-packages/pip (python 3.8)

我想用pyyaml模块写一个程序。我听说我已经安装了它

$ pip3 install pyyaml
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pyyaml in /Users/david.alvarado/Library/Python/3.8/lib/python/site-packages (6.0)

我有这个脚本

import pyyaml

arg = sys.argv[0]
with open(arg) as f:
    document = f.readlines()
    print(yaml.load(document))

但是当我运行我的程序时,我被告知找不到pyyaml

$ python3 convert_yaml_to_json.py ~/Downloads/myfile.yml 
Traceback (most recent call last):
  File "/Users/davea/scripts/convert_yaml_to_json.py", line 1, in <module>
    import pyyaml

编辑: 添加了一些输出以响应给定的建议

我尝试使用 Python 的完整路径,我被告知要求已经满足

$ /usr/local/bin/python3 -m pip install pyyaml
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Requirement already satisfied: pyyaml in /usr/local/lib/python3.9/site-packages (6.0)
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621

我什至试过 pip3

$ /usr/local/bin/python3 -m pip3 install pyyaml
/usr/local/opt/python@3.9/bin/python3.9: No module named pip3

然而,当 运行 完整路径时,它说找不到 YAML 模块

$ /usr/local/bin/python3 convert_yaml_to_json.py 
Traceback (most recent call last):
  File "/Users/davea/scripts/convert_yaml_to_json.py", line 1, in <module>
    import pyyaml
ModuleNotFoundError: No module named 'pyyaml'

安装正确,模块也在正确的目录中。唯一的问题是,在您的代码中,您必须将命令 import pyyaml 更改为 import yaml

这是例子; (使用您的代码)

import yaml
arg = sys.argv[0]
with open(argv) as f:
     document = f.readlines()
     print(yaml.load(document))

现在应该可以正常工作了