解決jupyter加載文件失敗的問題
遇到個(gè)小白常見的問題,發(fā)現(xiàn)度娘里面沒有記錄,翻墻谷歌了下,解決問題,在此寫個(gè)說明。
事情起因:
在jupyter notebook中導(dǎo)入文件時(shí)發(fā)生了錯(cuò)誤:
%load p2_test1.py
錯(cuò)誤信息:
---------------------------------------------------------------------------
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.
出現(xiàn)這個(gè)問題是因?yàn)榻忉屍髡也坏綄?duì)應(yīng)的文件。念了“媽咪媽咪轟”pwd后,發(fā)現(xiàn)問題所在:
%pwd ll
pwd顯示當(dāng)前目錄不是該文件所在的目錄。這下明白了,使用咒語(yǔ)要謹(jǐn)慎,記得在念“媽咪媽咪轟”load前,要cd到該文件的目錄下去。
補(bǔ)充:Jupyter Notebook - sns.load_dataset加載文件錯(cuò)誤解決方法
seaborn模塊中sns.load_dataset加載文件錯(cuò)誤解決方法:
import seaborn as sns
data = sns.load_dataset("iris")
出現(xiàn)錯(cuò)誤:
URLError: <urlopen error [Errno 11004] getaddrinfo failed>
出現(xiàn)原因:
seaborn-data文件夾里面是空的,可以另外下載該文件夾的內(nèi)容復(fù)制到該文件夾中。
下載地址:
https://github.com/mwaskom/seaborn-data
下載位置:


電腦上搜索seaborn-data文件夾
將下載下來的文件解壓后全部放進(jìn)seaborn-data文件夾
重新運(yùn)行代碼,出現(xiàn)結(jié)果。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Python 實(shí)現(xiàn)刪除某路徑下文件及文件夾的實(shí)例講解
下面小編就為大家分享一篇Python 實(shí)現(xiàn)刪除某路徑下文件及文件夾的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
C語(yǔ)言中printf()函數(shù)的全面介紹及用法(簡(jiǎn)單易懂)
在C語(yǔ)言中,printf()是常用的輸出函數(shù),包含在頭文件中,它使用格式控制字符串,其中包括格式字符、轉(zhuǎn)義字符和普通字符,格式字符以"%"開頭,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
詳解如何用TensorFlow訓(xùn)練和識(shí)別/分類自定義圖片
這篇文章主要介紹了詳解如何用TensorFlow訓(xùn)練和識(shí)別/分類自定義圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python 使用pandas實(shí)現(xiàn)查詢和統(tǒng)計(jì)示例詳解
這篇文章主要為大家介紹了Python 使用pandas實(shí)現(xiàn)查詢和統(tǒng)計(jì)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Python實(shí)現(xiàn)爬取并分析電商評(píng)論
這篇文章主要介紹了Python實(shí)現(xiàn)爬取并分析電商評(píng)論,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
django js實(shí)現(xiàn)部分頁(yè)面刷新的示例代碼
今天小編就為大家分享一篇django js實(shí)現(xiàn)部分頁(yè)面刷新的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

