dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.88k stars 382 forks source link

Cannot connect to python kernel using provided command #3170

Open odkken opened 1 year ago

odkken commented 1 year ago

Describe the bug

Running the command from the documentation:

#!connect jupyter --kernel-name pythonkernel --kernel-spec python3

yields the following error: Error: System.ArgumentException: KernelSpec python3 not found at Microsoft.DotNet.Interactive.Jupyter.ZMQ.JupyterConnection.CreateKernelConnectionAsync(String kernelSpecName) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive.Jupyter\ZMQ\JupyterConnection.cs:line 46 at Microsoft.DotNet.Interactive.Jupyter.JupyterKernelConnector.CreateKernelAsync(String kernelName) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive.Jupyter\JupyterKernelConnector.cs:line 27 at Microsoft.DotNet.Interactive.Jupyter.ConnectJupyterKernelCommand.ConnectKernelsAsync(KernelInvocationContext context, InvocationContext commandLineContext) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive.Jupyter\ConnectJupyterKernelCommand.cs:line 72 at Microsoft.DotNet.Interactive.CompositeKernel.<>cDisplayClass21_0.<b__0>d.MoveNext() in D:\a_work\1\s\src\Microsoft.DotNet.Interactive\CompositeKernel.cs:line 271 --- End of stack trace from previous location --- at System.CommandLine.NamingConventionBinder.CommandHandler.GetExitCodeAsync(Object returnValue, InvocationContext context) at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.InvokeAsync(InvocationContext context) at System.CommandLine.Invocation.InvocationPipeline.<>cDisplayClass4_0.<b0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>cDisplayClass15_0.<b0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>cDisplayClass12_0.<b0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Builder.CommandLineBuilderExtensions.<>cDisplayClass19_0.<b0>d.MoveNext() --- End of stack trace from previous location --- at System.CommandLine.Invocation.InvocationPipeline.gFullInvocationChainAsync|2_0(InvocationContext context) at System.CommandLine.Parsing.ParseResultExtensions.InvokeAsync(ParseResult parseResult, IConsole console) at Microsoft.DotNet.Interactive.Commands.DirectiveCommand.InvokeAsync(KernelInvocationContext context) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive\Commands\DirectiveCommand.cs:line 33 at Microsoft.DotNet.Interactive.Kernel.HandleAsync(KernelCommand command, KernelInvocationContext context) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive\Kernel.cs:line 326 at Microsoft.DotNet.Interactive.KernelCommandPipeline.b__60(KernelCommand command, KernelInvocationContext context, KernelPipelineContinuation ) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 60 at Microsoft.DotNet.Interactive.KernelCommandPipeline.SendAsync(KernelCommand command, KernelInvocationContext context) in D:\a_work\1\s\src\Microsoft.DotNet.Interactive\KernelCommandPipeline.cs:line 41

I am launching via an anaconda terminal after having activated an environment using code-insiders .

Do I need to modify pythonkernel or python3 in the above command to match the name of my python environment?

Which version of .NET Interactive are you using? (In a notebook, run the #!about magic command. ): Version: 1.0.446101+82cadfa197106a3f4a0fb95783e8ce6bb93c07b6

Screenshots

If applicable, add screenshots to help explain your problem.

syedqutub commented 1 year ago

same issue. i am on Windows 11. python 3.11 installed using chocolatey. and using python.

syedqutub commented 1 year ago

After exploring the code base by the given stack trace it turns out that i did not have "jupyter" cli installed on my pc. so i did the "pip install jupyterlab" and then it worked for me!

pehkawn commented 10 months ago

Can confirm. I get the same bug trying to run Python kernel by this command. I already had JupyterLab installed. Attempting to reinstall did not solve the issue.

pehkawn commented 10 months ago

Ok, so checking Jupyter's kernelspec list, I found a workaround: I noticed that I could run R kernels, so I suspected it might have something to do with the path to the python3 kernel.

>jupyter kernelspec list
0.01s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Available kernels:
  ir           C:\Users\palbja\AppData\Roaming\jupyter\kernels\ir
  julia-1.9    C:\Users\palbja\AppData\Roaming\jupyter\kernels\julia-1.9
  python3      C:\Users\palbja\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\share\jupyter\kernels\python3

As you can see, the R kernel (ir) is located in a different folder. I simply copied the python3 folder to C:\Users\palbja\AppData\Roaming\jupyter\kernels\. After restarting the .NET kernel in the Polyglot notebook, running the command #!connect jupyter --kernel-name pythonkernel --kernel-spec python3 will now start the Python kernel.

[Edit:] I should point out that I could run Python just fine before copying the folder to a different location. It is also worth noting that I installed Python 3.11 from Microsoft Store without admin privileges, which may have influenced the Python kernelspec path. Hope this is helpful finding a solution.