ubuntu环境下Pytorch安装及No module named 'torch._C'问题解决
近日,发现很多代码都是基于Pytorch来写的,为了进行测试和学习,于是进行了安装。
安装过程算是比较简单,直接从官网上下载源码,https://github.com/pytorch,然后选择从源码来进行安装。(说是可以直接从WHL文件来安装,只不过没有找到。)
安装过程的步骤:
git clone https://github.com/pytorch/pytorch pip install -r requirements.txt(实质上这一步只需要安装pyyaml)。 sudo python setup.py install
这样就可以实现顺利安装。安装后的截图如下:
不过,为了测试torch的python版本是否可用的时候,直接上来就报了个错。
Python 3.6.0 (default, Jan 16 2017, 12:12:55)
[GCC 6.3.1 20170109] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/torch/__init__.py", line 45, in <module>
from torch._C import *
ModuleNotFoundError: No module named 'torch._C'
官网上对此有一些说明:https://github.com/pytorch/pytorch/issues/574,即下面的这段话。
Yeah, so you can see that there's _C.cpython-35m which means it will be loaded only by Python 3.5. As a quick hack you could try renaming
_C, _dl, and _thnn/_THNN to regular .so files (without that cpython-version-artch-platform part, just _C.so). But it's a hack, we don't provide further support if it fails
根据上面的这段话,也进行了ls -ll 查看:
omnisky@omnisky:/usr/local/lib/python3.6/site-packages/torch$ ls autograd lib storage.py backends multiprocessing _tensor_docs.py _C.cpython-36m-x86_64-linux-gnu.so nn tensor.py cuda optim _tensor_str.py distributed __pycache__ _thnn _dl.cpython-36m-x86_64-linux-gnu.so serialization.py _torch_docs.py