导入 PyTorch 时出错 - Python

Error importing PyTorch - Python

我尝试在终端中使用以下命令安装 PyTorch:

pip install http://download.pytorch.org/whl/torch-0.2.0.post1-cp27-none-macosx_10_7_x86_64.whl 

然后我运行下面的代码在python:

import torch
torch.__file__

并得到以下错误:

 File "/Users/brian/anaconda/lib/python2.7/site-packages/torch/__init__.py", line 48, in <module>
import torch._dl as _dl_flags

AttributeError: 'module' object has no attribute '_dl'

我试图研究这个问题,但找不到明确的答案。任何帮助将不胜感激。

编辑: 这些是尝试使用以下命令重新安装 PyTorch 的结果:

pip install http://download.pytorch.org/whl/torch-0.2.0.post1-cp27-none-macosx_10_7_x86_64.whl 
pip install torchvision 

结果:

Requirement already satisfied: pyyaml in ./anaconda/lib/python2.7/site-packages (from torch==0.2.0.post1)
Requirement already satisfied: numpy in ./anaconda/lib/python2.7/site-packages (from torch==0.2.0.post1)


Requirement already satisfied: torchvision in ./anaconda/lib/python2.7/site-packages
Requirement already satisfied: pillow in ./anaconda/lib/python2.7/site-packages (from torchvision)
Requirement already satisfied: torch in ./anaconda/lib/python2.7/site-packages (from torchvision)
Requirement already satisfied: numpy in ./anaconda/lib/python2.7/site-packages (from torchvision)
Requirement already satisfied: six in ./anaconda/lib/python2.7/site-packages (from torchvision)
Requirement already satisfied: olefile in ./anaconda/lib/python2.7/site-packages (from pillow->torchvision)
Requirement already satisfied: pyyaml in ./anaconda/lib/python2.7/site-packages (from torch->torchvision)

编辑二

以下命令:

conda install pytorch torchvision cuda80 -c soumith

产生这个结果:

Fetching package metadata ...........

PackageNotFoundError: Packages missing in current channels:

   - cuda80

We have searched for the packages in the following channels:

  - https://conda.anaconda.org/soumith/osx-64
  - https://conda.anaconda.org/soumith/noarch
  - https://repo.continuum.io/pkgs/free/osx-64
  - https://repo.continuum.io/pkgs/free/noarch
  - https://repo.continuum.io/pkgs/r/osx-64
  - https://repo.continuum.io/pkgs/r/noarch
  - https://repo.continuum.io/pkgs/pro/osx-64
  - https://repo.continuum.io/pkgs/pro/noarch

http://pytorch.org/ 上:

  • OS: OSX
  • 包管理器:pip
  • Python: 2.7
  • CUDA:None

我有:

pip install http://download.pytorch.org/whl/torch-0.2.0.post1-cp27-none-macosx_10_7_x86_64.whl 
pip install torchvision 
# OSX Binaries dont support CUDA, install from source if CUDA is needed
  • 您确定select所有选项都正确吗?
  • 你试过第二条命令了吗pip install torchvision?

编辑

在您的日志中,您的 mac 上似乎安装了 anaconda,这意味着您应该有 select 包管理器:conda 而不是 pip.

在这种情况下,您应该删除使用 pip 安装的软件包:

pip uninstall torch

并改用使用 anaconda 的命令:

  • OS: OSX
  • 包管理器:conda
  • Python: 2.7
  • CUDA:None

运行命令:

conda install pytorch torchvision -c soumith

使用Docker 是一个很好的解决方案,可以拥有可重现的环境并解决设置环境的问题。您可以从 here. You can use the Dockerfile 安装 docker。

以防万一,即使您需要在不使用 docker 的情况下安装 pytorch,Dockerfile 提供了一个很好的逐步安装方法,可以在操作系统的情况下在本地安装与其基础 Docker 图像相同。

您也可以尝试使用 datmo 来设置环境并跟踪机器学习项目以使模型可重现。

我遇到了和你一样的问题,这可能是因为 numpy 的版本,我已经通过升级我的 numpy 包解决了这个问题。

pip install -U numpy 

然后事情似乎进展顺利~~