[Python] ModuleNotFoundError: No module named 'torch' when installing xformers for Stable...

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

  1. Stack

    Stack Membro Participativo

    I'm working on Stable Diffusion and try to install xformers to train my Lora.

    My Computer is Macbook M2 Max and already installed latest python3.11 and pip 23.1.2

    which python3
    /Library/Frameworks/Python.framework/Versions/Current/bin/python3
    python3 -V
    Python 3.11.3

    which pip3
    /Library/Frameworks/Python.framework/Versions/Current/bin/pip3
    pip3 -V
    pip 23.1.2 from /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip (python 3.11)


    But every time when I use below cmd:

    pip3 install xformers


    I'll get message as below:

    Collecting xformers
    Using cached xformers-0.0.20.tar.gz (7.6 MB)
    Installing build dependencies ... done
    Getting requirements to build wheel ... error
    error: subprocess-exited-with-error

    × Getting requirements to build wheel did not run successfully.
    │ exit code: 1
    ╰─> [21 lines of output]
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
    main()
    File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
    json_out['return_val'] = hook(**hook_input['kwargs'])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
    return hook(config_settings)
    ^^^^^^^^^^^^^^^^^^^^^
    File "/private/var/folders/05/3wghcmr93436gfgb3ty5mnmm0000gn/T/pip-build-env-3da38ps7/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
    return self._get_build_requires(config_settings, requirements=['wheel'])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/private/var/folders/05/3wghcmr93436gfgb3ty5mnmm0000gn/T/pip-build-env-3da38ps7/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
    self.run_setup()
    File "/private/var/folders/05/3wghcmr93436gfgb3ty5mnmm0000gn/T/pip-build-env-3da38ps7/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 488, in run_setup
    self).run_setup(setup_script=setup_script)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/private/var/folders/05/3wghcmr93436gfgb3ty5mnmm0000gn/T/pip-build-env-3da38ps7/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 338, in run_setup
    exec(code, locals())
    File "<string>", line 23, in <module>
    ModuleNotFoundError: No module named 'torch'
    [end of output]

    note: This error originates from a subprocess, and is likely not a problem with pip.
    error: subprocess-exited-with-error

    × Getting requirements to build wheel did not run successfully.
    │ exit code: 1
    ╰─> See above for output.

    note: This error originates from a subprocess, and is likely not a problem with pip.


    I've installed PyTorch using command from offical website:

    pip3 install torch torchvision torchaudio


    And when I run test.py using below code:

    import torch
    x = torch.rand(5, 3)
    print(x)


    I'll get correct result as below:

    python3 a.py
    tensor([[0.8204, 0.4140, 0.5262],
    [0.1857, 0.3604, 0.3496],
    [0.0620, 0.2303, 0.1033],
    [0.8081, 0.2826, 0.2922],
    [0.1685, 0.5701, 0.9935]])


    It means torch has been installed correctly.

    Also I can see torch using pip3 list

    torch 2.0.1
    torchaudio 2.0.2
    torchvision 0.15.2


    I also purged all pip3 cache using

    pip cache purge


    It doesn't work.

    I also restart my computer, doesn't work either.

    I saw a lot of Q&A talking about conda, I haven't installed conda so I believe it should not be a conda relative problem.

    How to fix this problem? Thanks~

    Continue reading...

Compartilhe esta Página