Repository: ipython/ipython

Issue:
Relative imports broken in the notebook (Windows)
It seems like relative imports do not work anymore in my notebooks since 6590fa5cce38f9b5020f219433b6821ba318a533.
I'm on Windows 8 64 bits, Python 2.7.5.



Discussion:
Odd, I can't see how that would have affected import machinery. Can you give an example of a failing case?

Weird. It only happens on my default profile, not in a fresh profile (I have a few startup scripts). Let me investigate further...

Very weird. No startup script = no problem. Add an empty .py file in `.ipython/profile_default/startup`, and relative imports do not work anymore!

Test notebook:

```
# <codecell>
%%writefile mymodule.py
a = 1

# <codecell>
import mymodule
```

Hmm, I see that as well. Weird.

It's not actually a relative import, though - it's an absolute import, it's just that the CWD isn't getting added to sys.path, for some reason.


Resolving patch:
diff --git a/IPython/core/application.py b/IPython/core/application.py
--- a/IPython/core/application.py
+++ b/IPython/core/application.py
@@ -137,7 +137,7 @@ def _profile_changed(self, name, old, new):
     )
     def _ipython_dir_default(self):
         d = get_ipython_dir()
-        self._ipython_dir_changed('ipython_dir', '', d)
+        self._ipython_dir_changed('ipython_dir', d, d)
         return d
     
     _in_init_profile_dir = False


Test patch:
diff --git a/IPython/kernel/tests/test_kernel.py b/IPython/kernel/tests/test_kernel.py
--- a/IPython/kernel/tests/test_kernel.py
+++ b/IPython/kernel/tests/test_kernel.py
@@ -17,6 +17,7 @@
 
 from IPython.testing import decorators as dec, tools as tt
 from IPython.utils import py3compat
+from IPython.utils.path import locate_profile
 
 from .utils import new_kernel, kernel, TIMEOUT, assemble_output, execute, flush_channels
 
@@ -46,6 +47,21 @@ def test_simple_print():
         _check_mp_mode(kc, expected=False)
 
 
+def test_sys_path():
+    """test that sys.path doesn't get messed up by default"""
+    with kernel() as kc:
+        msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))")
+        stdout, stderr = assemble_output(kc.iopub_channel)
+        nt.assert_equal(stdout, "''\n")
+
+def test_sys_path_profile_dir():
+    """test that sys.path doesn't get messed up when `--profile-dir` is specified"""
+    
+    with new_kernel(['--profile-dir', locate_profile('default')]) as kc:
+        msg_id, content = execute(kc=kc, code="import sys; print (repr(sys.path[0]))")
+        stdout, stderr = assemble_output(kc.iopub_channel)
+        nt.assert_equal(stdout, "''\n")
+
 @dec.knownfailureif(sys.platform == 'win32', "subprocess prints fail on Windows")
 def test_subprocess_print():
     """printing from forked mp.Process"""
diff --git a/IPython/kernel/tests/utils.py b/IPython/kernel/tests/utils.py
--- a/IPython/kernel/tests/utils.py
+++ b/IPython/kernel/tests/utils.py
@@ -36,10 +36,13 @@
 #-------------------------------------------------------------------------------
 
 
-def start_new_kernel():
+def start_new_kernel(argv=None):
     """start a new kernel, and return its Manager and Client"""
     km = KernelManager()
-    km.start_kernel(stdout=PIPE, stderr=PIPE)
+    kwargs = dict(stdout=PIPE, stderr=PIPE)
+    if argv:
+        kwargs['extra_arguments'] = argv
+    km.start_kernel(**kwargs)
     kc = km.client()
     kc.start_channels()
     
@@ -123,7 +126,7 @@ def stop_global_kernel():
     KM = None
 
 @contextmanager
-def new_kernel():
+def new_kernel(argv=None):
     """Context manager for a new kernel in a subprocess
     
     Should only be used for tests where the kernel must not be re-used.
@@ -132,7 +135,7 @@ def new_kernel():
     -------
     kernel_client: connected KernelClient instance
     """
-    km, kc = start_new_kernel()
+    km, kc = start_new_kernel(argv)
     try:
         yield kc
     finally: