pytorch geometric torch-scatter和torch-sparse安装报错问题解决

问题

在安装torch-scatter、torch-sparse等报错
报错subprocess.CalledProcessError: Command ‘[‘which‘, ‘g++‘]‘ returned non-zero exit status 1.

Traceback (most recent call last):
    File "", line 1, in <module>
    File "/tmp/pip-install-56zzyn27/torch-sparse/setup.py", line 46, in <module>
      packages=find_packages(),
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
      return distutils.core.setup(**attrs)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/core.py", line 148, in setup
      dist.run_commands()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/dist.py", line 955, in run_commands
      self.run_command(cmd)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/wheel/bdist_wheel.py", line 290, in run
      self.run_command('build')
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/command/build.py", line 135, in run
      self.run_command(cmd_name)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/cmd.py", line 313, in run_command
      self.distribution.run_command(command)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/dist.py", line 974, in run_command
      cmd_obj.run()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 79, in run
      _build_ext.run(self)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/distutils/command/build_ext.py", line 339, in run
      self.build_extensions()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 232, in build_extensions
      self._check_abi()
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 370, in _check_abi
      check_compiler_abi_compatibility(compiler)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 162, in check_compiler_abi_compatibility
      if not check_compiler_ok_for_platform(compiler):
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/site-packages/torch/utils/cpp_extension.py", line 138, in check_compiler_ok_for_platform
      which = subprocess.check_output(['which', compiler], stderr=subprocess.STDOUT)
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/subprocess.py", line 356, in check_output
      **kwargs).stdout
    File "/home/aha/anaconda3/envs/geo/lib/python3.6/subprocess.py", line 438, in run
      output=stdout, stderr=stderr)
  subprocess.CalledProcessError: Command '['which', 'g++']' returned non-zero exit status 1.
  ----------------------------------------
  ERROR: Failed building wheel for torch-sparse

解决方案

step 1: 安装gcc和g++

这是因为gcc和g++没有安装,可以通过以下命令安装

sudo apt-get install build-essential

Ubuntu缺省情况下,并没有提供C/C++的编译环境,因此还需要手动安装。如果单独安装gcc以及g++比较麻烦,幸运的是,为了能够编译Ubuntu的内核,Ubuntu提供了一个build-essential软件包。
安装完户,一部分用户再重新执行pip命令安装scatter和sparse就可以了,但是我又遇到了问题。

step 2: 解决torch_scatter安装版本问题

先说解决方案,就是在安装命令中指明torch_scatter的版本
pip install torch-scatter==2.0.6 -f https://data.pyg.org/whl/torch-1.6.0+cu101.html
也可以只用
pip install torch-scatter==2.0.6

下面说一下解决问题的过程:
首先我是按照官网指导运行的以下命令:
pip install -f https://data.pyg.org/whl/torch-1.6.0+cu101.html
发生报错,一部分报错内容如下:

csrc/scatter.cpp:135:30: error: no matching function for call to ‘at::Tensor::div_(at::Tensor&, const char [6])’
           out.div_(count, "floor");
                                  ^
    In file included from /opt/conda/lib/python3.7/site-packages/torch/include/ATen/Tensor.h:3:0,
                     from /opt/conda/lib/python3.7/site-packages/torch/include/ATen/Context.h:4,
                     from /opt/conda/lib/python3.7/site-packages/torch/include/ATen/ATen.h:5,
                     from /opt/conda/lib/python3.7/site-packages/torch/include/torch/csrc/api/include/torch/types.h:3,
                     from /opt/conda/lib/python3.7/site-packages/torch/include/torch/script.h:3,
                     from csrc/scatter.cpp:2:
    /opt/conda/lib/python3.7/site-packages/torch/include/ATen/core/TensorBody.h:649:12: note: candidate: at::Tensor& at::Tensor::div_(const at::Tensor&) const
       Tensor & div_(const Tensor & other) const;
                ^~~~
    /opt/conda/lib/python3.7/site-packages/torch/include/ATen/core/TensorBody.h:649:12: note:   candidate expects 1 argument, 2 provided
    /opt/conda/lib/python3.7/site-packages/torch/include/ATen/core/TensorBody.h:651:12: note: candidate: at::Tensor& at::Tensor::div_(c10::Scalar) const
       Tensor & div_(Scalar other) const;
                ^~~~
    /opt/conda/lib/python3.7/site-packages/torch/include/ATen/core/TensorBody.h:651:12: note:   candidate expects 1 argument, 2 provided
    error: command 'gcc' failed with exit status 1

仔细看了一下这段命令一开始执行的是:
在这里插入图片描述
也就是试图安装torch_scatter 2.0.9版本。
但是,我到官网给出的组合中去查找时,发现我的系统并不应该用2.0.9版本
https://data.pyg.org/whl/
以上链接中点击torch-1.6.0+cu101
pytorch geometric torch-scatter和torch-sparse安装报错问题解决_第1张图片
列出以下torch_scatter版本
pytorch geometric torch-scatter和torch-sparse安装报错问题解决_第2张图片
最高只有2.0.6版本,按照2.0.9版本与我的系统不符合,所以发生报错。至于为什么会出现这种情况,我也不太清楚。但是我们可以通过指定torch_scatter的版本来解决问题。
pip install torch-scatter==2.0.6 -f https://data.pyg.org/whl/torch-1.6.0+cu101.html

参考博文:
https://blog.csdn.net/klarclm/article/details/8550931
https://blog.csdn.net/Iv_zzy/article/details/109310965

你可能感兴趣的:(pytorch,python,深度学习)