名称 '_C' 未定义 pytorch+jupyter notebook

name '_C' is not defined pytorch+jupyter notebook

我有一些使用 pytorch 的代码,运行在我的 IDE (pycharm) 中没问题。

为了研究,我尝试从 jupyter notebook 运行 它。

笔记本中的代码:

from algorithms import Argparser
from algorithms import Session
def main():
    print("main started")
    args = Argparser.parse()
    session = Session(args)
    session.run()

包裹看起来像:

|-algorithms
|---__init__.py
|---Argparser.py
|---Session.py
|---<many more files that are being used by Session>.py

其中一些文件 import torch

当运行在笔记本中输入代码时,我得到

NameError Traceback (most recent call last) in 1 from algorithms import Argparser ----> 2 from algorithms import Session 3 def main(): 4 print("main started") 5 args = Argparser.parse()

D:\git\stav\stav-rl\algorithms\Session.py in 12 13 ---> 14 from algorithms.Episode import Episode 15 from algorithms.Agent import Agent 16 import torch

D:\git\stav\stav-rl\algorithms\Episode.py in 1 author = 'Noam' 2 ----> 3 import torch 4 import numpy as np 5 import cv2

c:\anaconda3\envs\threadartrl\lib\site-packages\torch__init__.py in 84 from torch._C import * 85 ---> 86 all += [name for name in dir(C) 87 if name[0] != '' and 88 not name.endswith('Base')]

NameError: name '_C' is not defined

错误在from algorithms import Session-->...-->import torch

我怎样才能得到 运行 的代码?

你需要 Cython 才能让 pytorch 工作:

pip3 install Cython

this comment on the issue on github

我的理解是site-packages/torch中有一个叫做_C.cpython-37m-x86_64-linux-gnu.so的库,它提供了共享对象_C并且需要Cython。 PyCharm 提供 Cython 支持,而 Jupyter 环境不支持。

重启内核即可解决问题。

我没有使用笔记本,所以这可能是另一个问题,但我最近在升级系统 libffi 后遇到了同样的错误。我能够通过安装 libffi7 来解决这个问题。

我继续 made a separate question 假设这是一个不同的问题,但我想我会 post 以防万一。