[Python] Install Tensorflow on M1 Max Mac using Conda and VS Code

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having the hardest time getting Tensorflow to install on my Mac and I've tried several approaches. Searching the internet for solutions led to recreating my Conda environments, trying pip, uninstalling things and trying again, downgrading Python, etc. Now I'm hoping someone can recreate my problem or see what I hope to be something very stupid I'm doing that makes this not work on my machine.

    First, here is the problem.

    import tensorflow as tf
    from tensorflow.keras import layers, Sequential
    from tensorflow.keras.optimizers import Adam


    results in

    {
    "name": "ModuleNotFoundError",
    "message": "No module named 'tensorflow.keras'",
    "stack": "---------------------------------------------------------------------------
    ModuleNotFoundError Traceback (most recent call last)
    Cell In[16], line 20
    17 from xgboost import XGBRegressor
    19 import tensorflow as tf
    ---> 20 from tensorflow.keras import layers, Sequential
    21 from tensorflow.keras.optimizers import Adam

    ModuleNotFoundError: No module named 'tensorflow.keras'"
    }


    In this current iteration of attempting to fix the problem, here are my settings.

    (cpsproject) ben@BenBook final % which python
    /Users/ben/opt/anaconda3/envs/cpsproject/bin/python
    (cpsproject) ben@BenBook final % python --version
    Python 3.11.10
    (cpsproject) ben@BenBook final % conda list
    # packages in environment at /Users/ben/opt/anaconda3/envs/cpsproject:
    #
    # Name Version Build Channel
    _py-xgboost-mutex 2.0 cpu_0
    absl-py 2.1.0 py311hecd8cb5_0
    anyio 4.6.2 py311hecd8cb5_0
    appnope 0.1.2 py311hecd8cb5_1001
    argon2-cffi 21.3.0 pyhd3eb1b0_0
    argon2-cffi-bindings 21.2.0 py311h6c40b1e_0
    asttokens 2.0.5 pyhd3eb1b0_0
    astunparse 1.6.3 py_0
    async-lru 2.0.4 py311hecd8cb5_0
    attrs 24.2.0 py311hecd8cb5_0
    babel 2.11.0 py311hecd8cb5_0
    beautifulsoup4 4.12.3 py311hecd8cb5_0
    blas 1.0 openblas
    bleach 4.1.0 pyhd3eb1b0_0
    bottleneck 1.4.2 py311h9b7fc35_0
    brotli 1.0.9 h6c40b1e_8
    brotli-bin 1.0.9 h6c40b1e_8
    brotli-python 1.0.9 py311hcec6c5f_8
    bzip2 1.0.8 h6c40b1e_6
    c-ares 1.19.1 h6c40b1e_0
    ca-certificates 2024.9.24 hecd8cb5_0
    certifi 2024.8.30 py311hecd8cb5_0
    cffi 1.17.1 py311h9205ec4_0
    charset-normalizer 3.3.2 pyhd3eb1b0_0
    comm 0.2.1 py311hecd8cb5_0
    contourpy 1.2.0 py311ha357a0b_0
    cycler 0.11.0 pyhd3eb1b0_0
    cyrus-sasl 2.1.28 h3973f50_1
    debugpy 1.6.7 py311hcec6c5f_0
    decorator 5.1.1 pyhd3eb1b0_0
    defusedxml 0.7.1 pyhd3eb1b0_0
    executing 0.8.3 pyhd3eb1b0_0
    flatbuffers 24.3.25 h6d0c2b6_0
    fonttools 4.51.0 py311h6c40b1e_0
    freetype 2.12.1 hd8bbffd_0
    gast 0.5.3 pyhd3eb1b0_0
    gettext 0.21.0 h4e8c18a_2
    giflib 5.2.2 h46256e1_0
    glib 2.78.4 hcec6c5f_0
    glib-tools 2.78.4 hcec6c5f_0
    google-pasta 0.2.0 pyhd3eb1b0_0
    grpcio 1.62.2 py311hab2016f_0
    gst-plugins-base 1.14.1 hcec6c5f_1
    gstreamer 1.14.1 h6c40b1e_1
    h11 0.14.0 py311hecd8cb5_0
    h5py 3.12.1 py311ha611a00_0
    hdf5 1.12.1 ha01d115_3
    httpcore 1.0.2 py311hecd8cb5_0
    httpx 0.27.0 py311hecd8cb5_0
    icu 73.1 hcec6c5f_0
    idna 3.7 py311hecd8cb5_0
    ipykernel 6.29.5 py311hecd8cb5_0
    ipython 8.27.0 py311hecd8cb5_0
    ipywidgets 8.1.2 py311hecd8cb5_0
    jedi 0.19.1 py311hecd8cb5_0
    jinja2 3.1.4 py311hecd8cb5_1
    joblib 1.4.2 py311hecd8cb5_0
    jpeg 9e h46256e1_3
    json5 0.9.25 py311hecd8cb5_0
    jsonschema 4.23.0 py311hecd8cb5_0
    jsonschema-specifications 2023.7.1 py311hecd8cb5_0
    jupyter 1.0.0 py311hecd8cb5_9
    jupyter-lsp 2.2.0 py311hecd8cb5_0
    jupyter_client 8.6.0 py311hecd8cb5_0
    jupyter_console 6.6.3 py311hecd8cb5_0
    jupyter_core 5.7.2 py311hecd8cb5_0
    jupyter_events 0.10.0 py311hecd8cb5_0
    jupyter_server 2.14.1 py311hecd8cb5_0
    jupyter_server_terminals 0.4.4 py311hecd8cb5_1
    jupyterlab 4.2.5 py311hecd8cb5_0
    jupyterlab_pygments 0.1.2 py_0
    jupyterlab_server 2.27.3 py311hecd8cb5_0
    jupyterlab_widgets 3.0.10 py311hecd8cb5_0
    keras 3.4.1 py311hecd8cb5_0
    kiwisolver 1.4.4 py311hcec6c5f_0
    krb5 1.20.1 h428f121_1
    lcms2 2.12 hf1fd2bf_0
    lerc 3.0 he9d5cce_0
    libabseil 20240116.2 cxx17_h548131f_0
    libbrotlicommon 1.0.9 h6c40b1e_8
    libbrotlidec 1.0.9 h6c40b1e_8
    libbrotlienc 1.0.9 h6c40b1e_8
    libclang 14.0.6 default_hd95374b_1
    libclang13 14.0.6 default_habbcc1a_1
    libcurl 8.9.1 h3a17b82_0
    libcxx 14.0.6 h9765a3e_0
    libdeflate 1.17 hb664fd8_1
    libedit 3.1.20230828 h6c40b1e_0
    libev 4.33 h9ed2024_1
    libffi 3.4.4 hecd8cb5_1
    libgfortran 5.0.0 11_3_0_hecd8cb5_28
    libgfortran5 11.3.0 h9dfd629_28
    libglib 2.78.4 h19e1a8f_0
    libgrpc 1.62.2 hf2926fd_0
    libiconv 1.16 h6c40b1e_3
    libllvm14 14.0.6 h26321d7_4
    libnghttp2 1.57.0 h9beae6a_0
    libopenblas 0.3.21 h54e7dc3_0
    libpng 1.6.39 h6c40b1e_0
    libpq 17.0 h184c1cd_0
    libprotobuf 4.25.3 h34eed0b_0
    libsodium 1.0.18 h1de35cc_0
    libssh2 1.11.0 hf20ceda_0
    libtiff 4.5.1 hcec6c5f_0
    libwebp-base 1.3.2 h46256e1_1
    libxgboost 2.1.1 hab2016f_0
    libxml2 2.13.1 h6070cd6_2
    llvm-openmp 14.0.6 h0dcd299_0
    lz4-c 1.9.4 hcec6c5f_1
    markdown 3.4.1 py311hecd8cb5_0
    markdown-it-py 2.2.0 py311hecd8cb5_1
    markupsafe 2.1.3 py311h6c40b1e_0
    matplotlib 3.9.2 py311hecd8cb5_0
    matplotlib-base 3.9.2 py311hc25a08b_0
    matplotlib-inline 0.1.6 py311hecd8cb5_0
    mdurl 0.1.0 py311hecd8cb5_0
    mistune 2.0.4 py311hecd8cb5_0
    ml_dtypes 0.4.0 py311he327ffe_0
    mysql 8.4.0 hbfece55_1
    namex 0.0.7 py311hecd8cb5_0
    nbclient 0.8.0 py311hecd8cb5_0
    nbconvert 7.16.4 py311hecd8cb5_0
    nbformat 5.10.4 py311hecd8cb5_0
    ncurses 6.4 hcec6c5f_0
    nest-asyncio 1.6.0 py311hecd8cb5_0
    notebook 7.2.2 py311hecd8cb5_1
    notebook-shim 0.2.3 py311hecd8cb5_0
    numexpr 2.10.1 py311hc59c7be_0
    numpy 1.26.4 py311h91b6869_0
    numpy-base 1.26.4 py311hb3ec012_0
    openjpeg 2.5.2 hbf2204d_0
    openldap 2.6.4 h6e6fb41_0
    openssl 3.0.15 h46256e1_0
    opt_einsum 3.3.0 pyhd3eb1b0_1
    optree 0.12.1 py311h1962661_0
    overrides 7.4.0 py311hecd8cb5_0
    packaging 24.1 py311hecd8cb5_0
    pandas 2.2.2 py311he327ffe_0
    pandocfilters 1.5.0 pyhd3eb1b0_0
    parso 0.8.3 pyhd3eb1b0_0
    pcre2 10.42 h9b97e30_1
    pexpect 4.8.0 pyhd3eb1b0_3
    pillow 11.0.0 py311h9c91434_0
    pip 24.2 py311hecd8cb5_0
    platformdirs 3.10.0 py311hecd8cb5_0
    ply 3.11 py311hecd8cb5_0
    prometheus_client 0.21.0 py311hecd8cb5_0
    prompt-toolkit 3.0.43 py311hecd8cb5_0
    prompt_toolkit 3.0.43 hd3eb1b0_0
    protobuf 4.25.3 py311h43dadc6_0
    psutil 5.9.0 py311h6c40b1e_0
    ptyprocess 0.7.0 pyhd3eb1b0_2
    pure_eval 0.2.2 pyhd3eb1b0_0
    py-xgboost 2.1.1 py311hecd8cb5_0
    pycparser 2.21 pyhd3eb1b0_0
    pygments 2.15.1 py311hecd8cb5_1
    pyparsing 3.2.0 py311hecd8cb5_0
    pyqt 5.15.10 py311hcec6c5f_0
    pyqt5-sip 12.13.0 py311h6c40b1e_0
    pysocks 1.7.1 py311hecd8cb5_0
    python 3.11.10 h4d6d9e5_0
    python-dateutil 2.9.0post0 py311hecd8cb5_2
    python-fastjsonschema 2.20.0 py311hecd8cb5_0
    python-flatbuffers 24.3.25 py311hecd8cb5_0
    python-json-logger 2.0.7 py311hecd8cb5_0
    python-tzdata 2023.3 pyhd3eb1b0_0
    pytz 2024.1 py311hecd8cb5_0
    pyyaml 6.0.2 py311h46256e1_0
    pyzmq 25.1.2 py311hcec6c5f_0
    qt-main 5.15.2 hf83fbd5_11
    qtconsole 5.6.0 py311hecd8cb5_0
    qtpy 2.4.1 py311hecd8cb5_0
    re2 2022.04.01 he9d5cce_0
    readline 8.2 hca72f7f_0
    referencing 0.30.2 py311hecd8cb5_0
    requests 2.32.3 py311hecd8cb5_1
    rfc3339-validator 0.1.4 py311hecd8cb5_0
    rfc3986-validator 0.1.1 py311hecd8cb5_0
    rich 13.7.1 py311hecd8cb5_0
    rpds-py 0.10.6 py311hf2ad997_0
    scikit-learn 1.5.1 py311he327ffe_0
    scipy 1.14.1 py311hedc7b93_0
    seaborn 0.13.2 py311hecd8cb5_0
    send2trash 1.8.2 py311hecd8cb5_0
    setuptools 75.1.0 py311hecd8cb5_0
    sip 6.7.12 py311hcec6c5f_0
    six 1.16.0 pyhd3eb1b0_1
    snappy 1.2.1 h6d0c2b6_0
    sniffio 1.3.0 py311hecd8cb5_0
    soupsieve 2.5 py311hecd8cb5_0
    sqlite 3.45.3 h6c40b1e_0
    stack_data 0.2.0 pyhd3eb1b0_0
    tensorboard 2.17.0 py311hecd8cb5_0
    tensorboard-data-server 0.7.0 py311hf1618be_1
    tensorflow 2.17.0 cpu_py311hb3d9b59_0
    tensorflow-base 2.17.0 cpu_py311h0426836_0
    tensorflow-estimator 2.17.0 cpu_py311h7aa49ff_0
    termcolor 2.1.0 py311hecd8cb5_0
    terminado 0.17.1 py311hecd8cb5_0
    threadpoolctl 3.5.0 py311h85bffb1_0
    tinycss2 1.2.1 py311hecd8cb5_0
    tk 8.6.14 h4d00af3_0
    tornado 6.4.1 py311h46256e1_0
    traitlets 5.14.3 py311hecd8cb5_0
    typing-extensions 4.11.0 py311hecd8cb5_0
    typing_extensions 4.11.0 py311hecd8cb5_0
    tzdata 2024b h04d1e81_0
    unicodedata2 15.1.0 py311h6c40b1e_0
    urllib3 2.2.3 py311hecd8cb5_0
    wcwidth 0.2.5 pyhd3eb1b0_0
    webencodings 0.5.1 py311hecd8cb5_1
    websocket-client 1.8.0 py311hecd8cb5_0
    werkzeug 3.0.6 py311hecd8cb5_0
    wheel 0.44.0 py311hecd8cb5_0
    widgetsnbextension 4.0.10 py311hecd8cb5_0
    wrapt 1.14.1 py311h6c40b1e_0
    xgboost 2.1.1 py311hecd8cb5_0
    xz 5.4.6 h6c40b1e_1
    yaml 0.2.5 haf1e3a3_0
    zeromq 4.3.5 hcec6c5f_0
    zlib 1.2.13 h4b97444_1
    zstd 1.5.6 h138b38a_0


    Any ideas?

    I've tried both updating and downgrading tensorflow and keras. Originally I was using Python 3.12.x, but I learned that was a no-no. Downgrading Python to 3.11.10 didn't work either, but the tensorflow documentation suggests that it's compatible with 3.11. The environment cpsproject is my third iteration of my virtual environment and it's the second iteration where I started by installing tensorflow first in hopes that any conflicts would be hashed out by Conda with subsequent package additions. That didn't work either, and that's the latest state I'm at now.

    Continue reading...

Compartilhe esta Página