jupyter加载文件失败问题

1 篇文章 0 订阅
1 篇文章 0 订阅

遇到个小白常见的问题,发现度娘里面没有记录,翻墙谷歌了下,解决问题,在此写个说明。

事情起因:

在jupyter notebook中导入文件时发生了错误:

%load p2_test1.py

错误信息:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3586         try:                                              # User namespace
-> 3587             codeobj = eval(target, self.user_ns)
   3588         except Exception:

<string> in <module>

NameError: name 'p2_test1' is not defined

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-1-e4e531e2ca85> in <module>
----> 1 get_ipython().run_line_magic('load', 'p2_test1.py')

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2285                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2286             with self.builtin_trap:
-> 2287                 result = fn(*args,**kwargs)
   2288             return result
   2289 

<decorator-gen-47> in load(self, arg_s)

/usr/local/lib/python3.5/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/usr/local/lib/python3.5/dist-packages/IPython/core/magics/code.py in load(self, arg_s)
    333         search_ns = 'n' in opts
    334 
--> 335         contents = self.shell.find_user_code(args, search_ns=search_ns)
    336 
    337         if 's' in opts:

/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3588         except Exception:
   3589             raise ValueError(("'%s' was not found in history, as a file, url, "
-> 3590                                 "nor in the user namespace.") % target)
   3591 
   3592         if isinstance(codeobj, str):

ValueError: 'p2_test1.py' was not found in history, as a file, url, nor in the user namespace.

出现这个问题是因为解释器找不到对应的文件。念了“妈咪妈咪轰”pwd后,发现问题所在:
 

%pwd

ll

pwd显示当前目录不是该文件所在的目录。这下明白了,使用咒语要谨慎,记得在念“妈咪妈咪轰”load前,要cd到该文件的目录下去。

参考:

https://stackoverflow.com/questions/50658677/jupyter-notebook-nameerror-name-main-is-not-defined

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
载ipynb文件到Jupyter有两种方法。方法一是使用Anaconda集成环境安装TensorFlow,其中自带了Jupyter。首先,在Anaconda Prompt中进入存放ipynb文件所在的目录,然后输入命令"jupyter lab",会自动弹出JupyterLab的网页应用程序,左侧是ipynb文件所在的目录,点击任意一个ipynb文件即可在右侧显示内容。方法二是安装好Anaconda后,在开始菜单中找到Anaconda Navigator,打开后找到Jupyter并点击launch,自动打开网页后,点击右上角的upload按钮,可以上传电脑上的.ipynb文件,并在列表中显示出来,双击文件即可打开.ipynb文件。在执行这些操作时,有时可能会遇到Jupyter载ipynb文件时占用内存过多的问题,导致缓存内存不够。这可能是因为代码中导入了多个大文件,且没有进行内存清理。解决方法是清理一下output,即清除输出结果。此外,有时在运行代码时突然出现此问题,显示此页出现问题,但打开其他代码时没有问题。如果重启Jupyter或更换浏览器都无法解决问题,可以尝试其他解决方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [jupyter如何打开.ipynb文件?](https://blog.csdn.net/lj2048/article/details/100880199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Jupyter某个ipynb突然打不开](https://blog.csdn.net/weixin_52448178/article/details/125004153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深蓝静音

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值