name #39;_C#39; is not defined pytorch+jupyter notebook(名称 _C 未定义 pytorch+jupyter notebook)

本文介绍了名称 '_C' 未定义 pytorch+jupyter notebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用 pytorch 的代码,它们在我的 IDE (pycharm) 中运行良好.

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

笔记本中的代码:

from 算法导入 Argparser从算法导入会话定义主():打印(主要开始")args = Argparser.parse()会话 = 会话(参数)session.run()

包看起来像:

|-算法|---__init__.py|---Argparser.py|---会话.py|---<Session 正在使用的更多文件>.py

其中一些文件import torch

在 notebook 中运行代码时,我得到

<块引用>

NameError Traceback(最近一次调用最后)在1 from 算法导入 Argparser----> 2 从算法导入会话3 定义主():4 打印(主要开始")5 args = Argparser.parse()

D:gitstavstav-rlalgorithmsSession.py 中1213---> 14 来自算法.情节导入情节15 from algorithm.Agent 导入Agent16进口火炬

D:gitstavstav-rlalgorithmsEpisode.py 中1 作者 = 'Noam'2----> 3个进口手电筒4 导入 numpy 作为 np5 导入cv2

c:anaconda3envs hreadartrllibsite-packages orch__init__.py 中84 从火炬._C 导入 *85---> 86 all += [目录中名称的名称(C)87 如果 name[0] !='' 和第88话

NameError: name '_C' 未定义

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

如何让代码运行?

解决方案

你需要 Cython 才能让 pytorch 工作:

pip3 安装 Cython

请参阅有关 github 上问题的评论.

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

I have some code that uses pytorch, that runs fine from my IDE (pycharm).

For research, I tried to run it from a jupyter notebook.

The code in the notebook:

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

The package looks like:

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

some of those files do import torch

When running the code in the notebook, I get

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:gitstavstav-rlalgorithmsSession.py in 12 13 ---> 14 from algorithms.Episode import Episode 15 from algorithms.Agent import Agent 16 import torch

D:gitstavstav-rlalgorithmsEpisode.py in 1 author = 'Noam' 2 ----> 3 import torch 4 import numpy as np 5 import cv2

c:anaconda3envs hreadartrllibsite-packages orch__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

The error is on from algorithms import Session-->...-->import torch

How can i get the code to run?

解决方案

You need Cython for pytorch to work:

pip3 install Cython

See this comment on the issue on github.

My understanding is that there is a library called _C.cpython-37m-x86_64-linux-gnu.so in site-packages/torch which provides the shared object _C and requires Cython. PyCharm provides Cython support whereas the Jupyter environment doesn't.

这篇关于名称 '_C' 未定义 pytorch+jupyter notebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!