Unable to install PyTorch in PyCharm (Python 3.9 / macOS)

Question:

Environment:

  • pip version: 20.2.4
  • interpreter: Python 3.9.0
  • OS: macOS Big Sur
  • Everything else is also up to date

I tried the following commands in the Terminal and none worked:

  • pip install torch
  • pip3 install torch
  • pip install torch torchvision torchaudio

I get different error messages for each command, but this is the output of "pip install torch":

Collecting torch
  Using cached torch-0.1.2.post2.tar.gz (128 kB)
Requirement already satisfied: pyyaml in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (from torch) (5.3.1)
Using legacy 'setup.py install' for torch, since package 'wheel' is not installed.
Installing collected packages: torch
    Running setup.py install for torch: started
    Running setup.py install for torch: finished with status 'error'

DEPRECATION: The -b/--build/--build-dir/--build-directory option is deprecated. pip 20.3 will remove support for this functionality. A possible replacement is use the TMPDIR/TEMP/TMP environment variable, possibly combined with --no-clean. You can find discussion regarding this at https://github.com/pypa/pip/issues/8333.
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py'"'"'; __file__='"'"'/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pip-record-92u0tiue/install-record.txt --single-version-externally-managed --compile --install-headers /Library/Frameworks/Python.framework/Versions/3.9/include/python3.9/torch
         cwd: /private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/
    Complete output (23 lines):
    running install
    running build_deps
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py", line 225, in <module>
        setup(name="torch", version="0.1.2.post2",
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/setuptools/__init__.py", line 153, in setup
        return distutils.core.setup(**attrs)
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 966, in run_commands
        self.run_command(cmd)
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
        cmd_obj.run()
      File "/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py", line 99, in run
        self.run_command('build_deps')
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/distutils/dist.py", line 985, in run_command
        cmd_obj.run()
      File "/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py", line 51, in run
        from tools.nnwrap import generate_wrappers as generate_nn_wrappers
    ModuleNotFoundError: No module named 'tools.nnwrap'
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py'"'"'; __file__='"'"'/private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pycharm-packaging/torch/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/jz/bbl56kxj3tl_wssc22lnxqmc0000gn/T/pip-record-92u0tiue/install-record.txt --single-version-externally-managed --compile --install-headers /Library/Frameworks/Python.framework/Versions/3.9/include/python3.9/torch Check the logs for full command output.
Asked By: Omar AlSuwaidi

||

Answers:

Okay, I got this fixed by using the conda package manager instead of pip’s. Conda uses python version 3.8.5, whereas version 3.9 was used for my pip manager. I’m not quite sure if the issue of incompatibility was due to pip itself, or the fact that it is using python version 3.9. Anyways after downloading the graphical macOS installer:

conda install pytorch torchvision torchaudio -c pytorch

And that should successfully install PyTorch!

Answered By: Omar AlSuwaidi

only need to change the last one to
pip3 install torch torchvision torchaudio

Answered By: Fen