无法在 PyCharm 中安装 PyTorch(Python 3.9 / macOS)

Posted

技术标签:

【中文标题】无法在 PyCharm 中安装 PyTorch(Python 3.9 / macOS)【英文标题】:Unable to install PyTorch in PyCharm (Python 3.9 / macOS) 【发布时间】:2021-03-05 04:56:04 【问题描述】:

环境:

pip 版本:20.2.4 解释器:Python 3.9.0 操作系统:macOS Big Sur 其他一切都是最新的

我在终端中尝试了以下命令,但都没有成功:

点安装火炬 pip3 安装火炬 pip install torch torchvision torchaudio

我收到每个命令的不同错误消息,但这是“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('"'"'\r\n'"'"', '"'"'\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('"'"'\r\n'"'"', '"'"'\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.

【问题讨论】:

这能回答你的问题吗? ModuleNotFoundError: No module named 'tools.nnwrap' 不幸的是,对于 python 版本 > 3.6,他将你引回到他们的官方网站,它要求你运行:pip install torch torchvision torchaudio 【参考方案1】:

好的,我通过使用 conda 包管理器而不是 pip 解决了这个问题。 Conda 使用 python 3.8.5 版本,而 3.9 版本用于我的 pip 管理器。我不太确定不兼容问题是由于 pip 本身,还是它使用的是 python 3.9 版。无论如何下载图形 macOS 安装程序后:

conda install pytorch torchvision torchaudio -c pytorch

这应该会成功安装 PyTorch!

【讨论】:

而在 PyCharm 中,您可以将项目的解释器更改为 Conda 的。

以上是关于无法在 PyCharm 中安装 PyTorch(Python 3.9 / macOS)的主要内容,如果未能解决你的问题,请参考以下文章

我无法在 jupyter 和 Spyder 中安装 pytorch?

无法从/在 PyCharm 中安装 Kivy

无法在 Pycharm 中安装 pygame

由于 setuptools 无法在 conda 中安装 pytorch 是 conda 的依赖项,无法删除

python环境配置步骤二:Windows中创建虚拟环境安装Pytorch并在PyCharm中配置虚拟环境

在虚拟环境python 2.7 pycharm中安装后无法导入请求