我在 Mac OS X Big Sur 上。我通過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'
uj5u.com熱心網友回復:
安裝正確,模塊也在正確的目錄下。唯一的問題是,在您的代碼中,您必須將命令更改import pyyaml為import yaml
這是示例;(使用您的代碼)
import yaml
arg = sys.argv[0]
with open(argv) as f:
document = f.readlines()
print(yaml.load(document))
它現在應該可以正常作業
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484319.html
標籤:python-3.x 苹果系统 点子 pyyaml
