Repository: ipython/ipython

Issue:
IPython does not start on WSL
I installed IPython on WSL, but when I try to launch it, I get the following error:

```
/mnt/c/Users/User/Desktop$ ipython
Traceback (most recent call last):
  File "/home/user/.local/bin/ipython", line 8, in <module>
    sys.exit(start_ipython())
  File "/home/user/.local/lib/python3.10/site-packages/IPython/__init__.py", line 129, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/traitlets/config/application.py", line 1042, in launch_instance
    app.initialize(argv)
  File "/home/user/.local/lib/python3.10/site-packages/traitlets/config/application.py", line 113, in inner
    return method(app, *args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/IPython/terminal/ipapp.py", line 279, in initialize
    self.init_shell()
  File "/home/user/.local/lib/python3.10/site-packages/IPython/terminal/ipapp.py", line 293, in init_shell
    self.shell = self.interactive_shell_class.instance(parent=self,
  File "/home/user/.local/lib/python3.10/site-packages/traitlets/config/configurable.py", line 551, in instance
    inst = cls(*args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/IPython/terminal/interactiveshell.py", line 854, in __init__
    super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 575, in __init__
    self.init_virtualenv()
  File "/home/user/.local/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 864, in init_virtualenv
    if p_venv.parts[1] == "cygdrive":
IndexError: tuple index out of range

If you suspect this is an IPython 8.14.0 bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True
```

System info:
```
/mnt/c/Users/User/Desktop$ python3 -c "import IPython; print(IPython.sys_info())"
{'commit_hash': 'f11276427',
 'commit_source': 'installation',
 'default_encoding': 'utf-8',
 'ipython_path': '/home/user/.local/lib/python3.10/site-packages/IPython',
 'ipython_version': '8.14.0',
 'os_name': 'posix',
 'platform': 'Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.35',
 'sys_executable': '/usr/bin/python3',
 'sys_platform': 'linux',
 'sys_version': '3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]'}
```



Discussion:
I received this same error while installing iPython on a Dockerfile. If you look into the source code, `p_env` is a Pathlib object for your `VIRTUAL_ENV` environment variable. For me, the error was resolved by adding a "/" to the forefront of that environment variable.
> I received this same error while installing iPython on a Dockerfile. If you look into the source code, `p_env` is a Pathlib object for your `VIRTUAL_ENV` environment variable. For me, the error was resolved by adding a "/" to the forefront of that environment variable.

@joegenius98 Can you give me more details? I'm also having the same problem, with a Dockerfile. Can you paste what that part of your Dockerfile was before and after? This has been driving me nuts!
> > I received this same error while installing iPython on a Dockerfile. If you look into the source code, `p_env` is a Pathlib object for your `VIRTUAL_ENV` environment variable. For me, the error was resolved by adding a "/" to the forefront of that environment variable.
> 
> @joegenius98 Can you give me more details? I'm also having the same problem, with a Dockerfile. Can you paste what that part of your Dockerfile was before and after? This has been driving me nuts!

I'm not using it inside docker, but am embedding into an async python set of scripts and was running into the same issue.

Basically when your VIRTUAL_ENV env var is just the relative path ".venv" then this line in the ipython shell startup
`if p_venv.parts[1] == "cygdrive":`
throws an error as there is only 1 part (ie. p_venv.parts[0]) to the path

From a shell you can see the current value of VIRTUAL_ENV like:
```
$ echo $VIRTUAL_ENV
.venv
```

Or from in python
```
>>> import os
>>> print(os.environ["VIRTUAL_ENV"])
.venv
```

You can work around this by setting the var to the absolute path rather than the relative one.
In a shell like this
`$ VIRTUAL_ENV="$(pwd)/$VIRTUAL_ENV"`
or in python
`os.environ["VIRTUAL_ENV"] = str(Path().absolute()) + "/" + os.environ["VIRTUAL_ENV"]`

The above work around got things working fine for me pending this being fixed.

Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -909,7 +909,7 @@ def init_virtualenv(self):
         paths = self.get_path_links(p)
 
         # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
-        if p_venv.parts[1] == "cygdrive":
+        if len(p_venv.parts) > 2 and p_venv.parts[1] == "cygdrive":
             drive_name = p_venv.parts[2]
             p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:])
 


Test patch: