title: jupyter notebook: NameError: name 'main' is not defined
body: <p><a href="https://i.stack.imgur.com/GFMEf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GFMEf.png" alt="enter image description here"></a></p>
  
  <p>I'm working with a Jupyter notebook. I'm trying to import a file called main.py which contains 2 lines:</p>
  
  <pre><code>import os
  ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # This is your Project Root
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>import os
  %load main.py
  from main import ROOT_DIR 
  </code></pre>
  
  <p>I'm getting </p>
  
  <pre><code>---------------------------------------------------------------------------
  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
  -&gt; 3283             codeobj = eval(target, self.user_ns)
     3284         except Exception:
  
  &lt;string&gt; in &lt;module&gt;()
  
  NameError: name 'main' is not defined
  
  During handling of the above exception, another exception occurred:
  
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-3-1f13faed25b1&gt; in &lt;module&gt;()
        1 import os
  ----&gt; 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:
  -&gt; 2131                 result = fn(*args,**kwargs)
     2132             return result
     2133 
  
  &lt;decorator-gen-48&gt; in load(self, arg_s)
  
  ....\site-packages\IPython\core\magic.py in &lt;lambda&gt;(f, *a, **k)
      185     # but it's overkill for just that one bit of state.
      186     def magic_deco(arg):
  --&gt; 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 
  --&gt; 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, "
  -&gt; 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.
  </code></pre>
  
  <p>What am I doing wrong?</p>