Repository: ipython/ipython

Issue:
Windows %pip magic broken for 7.26 release due to shlex.quote pathing issue
<!-- This is the repository for IPython command line, if you can try to make sure this question/bug/feature belong here and not on one of the Jupyter repositories. 

If it's a generic Python/Jupyter question, try other forums or discourse.jupyter.org.

If you are unsure, it's ok to post here, though, there are few maintainer so you might not get a fast response. 

-->

Hi Team,

OS: Windows 10 Pro (build 19041.1110)
Python: 3.9
IPython: 7.26

I found working with PyCharm and demonstrating use of Notebooks on Windows that `%pip install` stopped working this week in new environments and complaining about pathing, the error:

```
The filename, directory name, or volume label syntax is incorrect.
```

The use of `%conda` seemed to work find for conda environments, it was just pip that was failing.

When a new project is created in PyCharm is pulls the latest version of `ipython` for use with Jupyter. Downgrading to 7.25 the functionality of `%pip install` is restored again.

Looking at the release and related pull requests I found this pathing issues for Mac.

https://github.com/ipython/ipython/pull/13052

The path to my virtual env when testing was: `C:\projects\Demos\pycharmNotebook\venv\`

I believe this is an issue with using single quotes for commands in Windows which causes problems and has differences to that of Unix/Mac/Linux systems.

For example, if I have the following notebook running 7.26
```
#%%
import shlex
import sys
import os
with_quote = shlex.quote(sys.executable)
without_quote = sys.executable
full_call_with_quote = " ".join([with_quote,"-m","pip","install servicestack"])
full_call_without_quote = ' '.join([without_quote,'-m','pip','install servicestack'])
print(full_call_without_quote)
print(full_call_with_quote)
os.system(full_call_without_quote)
os.system(full_call_with_quote)
#%%
```

The output of the print statements is:
```
C:\projects\Demos\pycharmNotebook\venv\Scripts\python.exe -m pip install servicestack
'C:\projects\Demos\pycharmNotebook\venv\Scripts\python.exe' -m pip install servicestack
```
And the output from the `os.system` calls is successful for the first, but fails with `The filename, directory name, or volume label syntax is incorrect.` for the second which is the same implementation changed in 7.26. 

Unfortunately it seems the fix for Mac paths with spaces has broken all paths on Windows.

I believe double quotes `"` works correctly on both but I don't have a Mac to easily accessible to test on. Windows WSL running Ubuntu 20.04 works with double quotes, but again I'm not 100% sure on the implications for out of the ordinary pathing.

To anyone experiencing this issue on Windows, downgrade to 7.25 resolves the issue for me.



Discussion:

Resolving patch:
diff --git a/IPython/core/magics/packaging.py b/IPython/core/magics/packaging.py
--- a/IPython/core/magics/packaging.py
+++ b/IPython/core/magics/packaging.py
@@ -66,7 +66,12 @@ def pip(self, line):
         Usage:
           %pip install [pkgs]
         """
-        python = shlex.quote(sys.executable)
+        python = sys.executable
+        if sys.platform == "win32":
+            python = '"' + python + '"'
+        else:
+            python = shlex.quote(python)
+
         self.shell.system(" ".join([python, "-m", "pip", line]))
 
         print("Note: you may need to restart the kernel to use updated packages.")


Test patch: