jupyter notebook:NameError:name' main'没有定义

时间:2018-06-02 15:59:08

标签: python jupyter-notebook jupyter jupyter-lab

enter image description here

我正在使用Jupyter笔记本电脑。我试图导入名为main.py的文件,其中包含2行:

import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # This is your Project Root

我的代码:

import os
%load main.py
from main import ROOT_DIR 

我正在

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
....\site-packages\IPython\core\interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3282         try:                                              # User namespace
-> 3283             codeobj = eval(target, self.user_ns)
   3284         except Exception:

<string> in <module>()

NameError: name 'main' is not defined

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-3-1f13faed25b1> in <module>()
      1 import os
----> 2 get_ipython().run_line_magic('load', 'main.py')
      3 from main import ROOT_DIR

....\site-packages\IPython\core\interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2129                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2130             with self.builtin_trap:
-> 2131                 result = fn(*args,**kwargs)
   2132             return result
   2133 

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

....\site-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):

....\site-packages\IPython\core\magics\code.py in load(self, arg_s)
    342         search_ns = 'n' in opts
    343 
--> 344         contents = self.shell.find_user_code(args, search_ns=search_ns)
    345 
    346         if 's' in opts:

....\site-packages\IPython\core\interactiveshell.py in find_user_code(self, target, raw, py_only, skip_encoding_cookie, search_ns)
   3284         except Exception:
   3285             raise ValueError(("'%s' was not found in history, as a file, url, "
-> 3286                                 "nor in the user namespace.") % target)
   3287 
   3288         if isinstance(codeobj, str):

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

我做错了什么?

0 个答案:

没有答案