无法安装某些 pythons 模块,并且某些 python 模块在我的 macbook 中显示不兼容

Can't install some pythons modules and some python modules are showing incompatible in my macbook

我是 macOS Monterey(12.3.1) 的新手。我正在使用带有 m1 芯片的新 MacBook pro。我已经在我的系统中安装了 python 3.10。 我正在尝试 运行 以下代码:

from urllib.parse import urljoin
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
import numpy as np
import requests, re, random
from keras.preprocessing import text, sequence
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Embedding
from keras.layers import Dropout
from keras.callbacks import TensorBoard
from itertools import chain
from pickle import load, dump
import os

我在学习上述程序时遇到以下错误。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [1], in <cell line: 3>()
      1 from urllib.parse import urljoin
      2 from bs4 import BeautifulSoup
----> 3 import matplotlib.pyplot as plt
      4 import tensorflow as tf
      5 import pandas as pd

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/__init__.py:109, in <module>
    105 from packaging.version import parse as parse_version
    107 # cbook must import matplotlib only within function
    108 # definitions, so it is safe to import from it here.
--> 109 from . import _api, _version, cbook, docstring, rcsetup
    110 from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence
    111 from matplotlib.cbook import mplDeprecation  # deprecated

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/cbook/__init__.py:31, in <module>
     28 import numpy as np
     30 import matplotlib
---> 31 from matplotlib import _api, _c_internal_utils
     32 from matplotlib._api.deprecation import (
     33     MatplotlibDeprecationWarning, mplDeprecation)
     36 @_api.deprecated("3.4")
     37 def deprecated(*args, **kwargs):

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/_c_internal_utils.cpython-310-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/matplotlib/_c_internal_utils.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

这是我尝试在我的 MacBook 上安装 python 模块时的输出。

mac@Mac-MacBook-Pro ML % pip3.10 install os  
ERROR: Could not find a version that satisfies the requirement os (from versions: none)
ERROR: No matching distribution found for os
mac@Mac-MacBook-Pro ML % pip3.10 install matplotlib
Requirement already satisfied: matplotlib in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (3.5.1)
Requirement already satisfied: cycler>=0.10 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (4.31.1)
Requirement already satisfied: packaging>=20.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (21.3)
Requirement already satisfied: pyparsing>=2.2.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (3.0.7)
Requirement already satisfied: numpy>=1.17 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (1.22.3)
Requirement already satisfied: pillow>=6.2.0 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (9.0.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (1.4.0)
Requirement already satisfied: python-dateutil>=2.7 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)

根据 ImportError 的详细信息,看起来它来自您的 matplotlib 是使用不兼容的体系结构构建的。这很可能与 M1 架构有关。看来 对您也有帮助。

为了以防万一,我也在这条消息中分享了 code-block。

python -m pip install cython   
python -m pip install --no-binary :all: --no-use-pep517 numpy
brew install libjpeg
python -m pip install matplotlib

我可以通过使用

删除所有 pip 包来解决我的 MacBook pro m1 上的问题
pip3 freeze | xargs pip3 uninstall -y

然后重新安装所需的模块。在我的例子中,matplotlib 使用

pip install matplotlib