解决jupyter加载文件失败的问题

时间:2022-10-01 23:37:21

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

事情起因:

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

?
1
%load p2_test1.py

错误信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---------------------------------------------------------------------------
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后,发现问题所在:

?
1
2
%pwd
ll

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

参考

补充:jupyter notebook - sns.load_dataset加载文件错误解决方法

seaborn模块中sns.load_dataset加载文件错误解决方法:

?
1
2
import seaborn as sns
data = sns.load_dataset("iris")

出现错误:

urlerror: <urlopen error [errno 11004] getaddrinfo failed>

出现原因:

seaborn-data文件夹里面是空的,可以另外下载该文件夹的内容复制到该文件夹中。

下载地址:

https://github.com/mwaskom/seaborn-data

下载位置:

解决jupyter加载文件失败的问题

解决jupyter加载文件失败的问题

电脑上搜索seaborn-data文件夹

将下载下来的文件解压后全部放进seaborn-data文件夹

重新运行代码,出现结果。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/giselite/article/details/84973895