mne-tools / mne-installers

Installers for MNE-Python.
BSD 3-Clause "New" or "Revised" License
8 stars 8 forks source link

Improved UX with Python in VS Code #170

Open hoechenberger opened 1 year ago

hoechenberger commented 1 year ago

With the latest Python extension, VS Code -- when started from the command line -- will honor the conda environment that was active in the shell VS Code was started from:

https://devblogs.microsoft.com/python/python-in-visual-studio-code-february-2023-release/#automatic-environment-selection-when-vs-code-is-launched-from-an-activated-terminal

We could make use of this new feature and essentially drop the VS Code setup instructions from the MNE installers docs: https://mne.tools/stable/install/installers.html#vs-code-setup

We could take it even further and add something of a menu entry / icon that would start a terminal, activate the MNE environment, and run VS Code from there. However it would still require the user to first install VS Code on their system. But I think it would make VS Code use easier! Users could just click on "VS Code (MNE)" and would have MNE in VS Code up and running. Aaaaand, we could then drop Spyder, eventually... 😈

What do you think?

@larsoner @agramfort @drammock

agramfort commented 1 year ago

no strong feeling on this

Message ID: @.***>

cbrnr commented 1 year ago

I know everyone is going to hate me, but I'm currently switching my teaching from VS Code to Spyder. VS Code has too many basic issues for interactive work (e.g. no IPython integration, no way to run code from a script line by line, etc.). Unless you use notebooks (which I don't), so this might not be relevant then.

hoechenberger commented 1 year ago

e.g. no IPython integration, no way to run code from a script line by line, etc.). Unless you use notebooks (which I don't),

Errm no, this is not true. I don't use notebooks and I do use these features with my scripts. You need to add "code cells" by adding comments of the form,

# %%

and you get access to all interactive features without having to use a notebook.

cbrnr commented 1 year ago

@hoechenberger this is using a notebook.

hoechenberger commented 1 year ago

No? It's a script...

cbrnr commented 1 year ago

It basically starts a notebook to run the code from your script. And the you can still only run cell by cell and not line by line.

cbrnr commented 1 year ago

Also, I was refering to your comment about eventually removing Spyder. This does not mean that I am against improving the UX of using VS Code with MNE.

hoechenberger commented 1 year ago

I don't know what you mean by saying, "basically starts a notebook"? It sends blocks or lines of code to a Jupyter (i.e., basically IPython) kernel.

And the you can still only run cell by cell and not line by line.

It's simply not true, you can send individual lines to the interactive window.

I mean I really don't care how you work or what you teach, but what you said above is simply just not true, I'm sorry...

cbrnr commented 1 year ago

Let me know how I can run a single line of code and advance to the next one.

hoechenberger commented 1 year ago

Let me know how I can run a single line of code and advance to the next one.

Place the cursor on the line (or select one or more lines), bring up the command palette, and choose, "Jupyter: Run Selection/Line in Interactive Window". Can probably be mapped to a more convenient keyboard shortcut too. Doesn't advance to the next line automatically though.

cbrnr commented 1 year ago

Doesn't advance to the next line automatically though.

That's what I meant by running a script line by line. Without advancing, it is not useful for stepping through a script. PyCharm and Spyder can do this by default, and so does RStudio for R and VS Code (yes, the same VS Code) for Julia!

So I think what I said is true.

Anyway, all I wanted to say is: please don't drop Spyder.

drammock commented 1 year ago

@cbrnr FWIW there is a keyboard shortcut, add this to keybindings.json:

[
    {
        "key": "ctrl+enter",
        "command": "workbench.action.terminal.runSelectedText",
        "when": "editorTextFocus"
    },
]

This will send the selected text (or if none selected, the line on which the cursor lies) to the active terminal. The active terminal can be running Bash, a vanilla python interpreter, an ipython interpreter, R, whatever. No notebooks involved. But as noted before, it does not auto-advance. It also kinda mangles indentation so it sometimes chokes on multiline selections (I think when there are both indents & later outdents is when it fails) and IIRC it sends physical lines not logical (python statement) lines

cbrnr commented 1 year ago

Yes, I tried that among many other things. Bottom line is that VS Code is not a good editor for interactive Python (non-notebook).

agramfort commented 1 year ago

@cbrnr I don't know why you say this. See:

https://user-images.githubusercontent.com/161052/218860655-145027d5-f2f5-44d8-9153-b7f2f82ffa49.mov

cbrnr commented 1 year ago

I'll take it back. It is just not for my workflow, where I really need to be able to run single lines or blocks of code and then the cursor auto-addvances to the next executable line. Not cell.

hoechenberger commented 1 year ago

Hey all, sorry this escalated in the way it did; this was unnecessary, and I recognize my (negative) contribution in the process. I just had a brief private chat with @cbrnr, and we're cool (again). Apparently, there are use cases for Spyder, so let's just keep including it in our installers as long as it doesn't cause too much of a burden. My proposal to remove it was really just more of a thought experiment, no immediate call for action.

Have a great day, everybody!

cbrnr commented 1 year ago

I also apologize for taking this thread in the wrong direction by statements that could be easily misinterpreted. I know my communication style is very direct (we've had this conversation before), but my intention is not to attack or insult anyone. If anyone feels attacked or insulted, I'd be happy to chat.

larsoner commented 1 year ago

We could make use of this new feature and essentially drop the VS Code setup instructions from the MNE installers docs

I'm not sure we can -- if you install on Linux, macOS, or Windows, you get a nice start menu / app folder shortcut. I bet 95%+ of people launch code this way rather than through the command prompt. So it's likely that their vscode launched the standard way will usually not find the right python.

We could add a little note that, as an alternative to setting the config value, you could always launch vscode from a terminal already configured to find the correct python, though.

We could take it even further and add something of a menu entry / icon that would start a terminal, activate the MNE environment, and run VS Code from there. However it would still require the user to first install VS Code on their system. But I think it would make VS Code use easier! Users could just click on "VS Code (MNE)" and would have MNE in VS Code up and running.

Agreed we should do this in MNE-installers. Without too much effort we could probably even add a little error prompt when code is not found (because qtpy is in the MNE env?). We might have to do some magic/detection of the executable location -- on Linux code is available, on macOS I had to add a little softlink:

$ ls -al /Users/larsoner/.local/bin/code 
lrwxr-xr-x  1 larsoner  staff  68 May 20  2022 /Users/larsoner/.local/bin/code -> /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code

No idea on Windows...

hoechenberger commented 1 year ago

on Linux code is available, on macOS I had to add a little softlink:

Wait wait wait what? This should not be necessary. If you need to do this, could it be you missed this setup step?

https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line

If we cannot find code, we should just refer to this website.

larsoner commented 1 year ago

This should not be necessary. If you need to do this, could it be you missed this setup step?

Yes because all I did was download the .dmg and run the installer like usual. I imagine the vast majority of end users are in this boat, since what you linked to requires some extra thought/reading/work compared to standard install procedures 🤷

hoechenberger commented 1 year ago

I imagine the vast majority of end users are in this boat, since what you linked to requires some extra thought/reading/work compared to standard install procedures 🤷

I believe I actually got a prompt when first starting VS Code on macOS. But I don't remember. Anyway, like I said, we could just tell users what to do in case we cannot find code :)

hoechenberger commented 8 months ago

Let me know how I can run a single line of code and advance to the next one.

This has now been implemented in the latest release of the Python for VS Code extension (albeit it's still an experiment):

https://devblogs.microsoft.com/python/python-in-visual-studio-code-november-2023-release/

cbrnr commented 8 months ago

Yes, and I'm already using and loving it! I would still wait until this is not experimental. And there is one final big one: IPython support is still not implemented. They have been discussing this (in various issues), but unless it is possible to use IPython instead of the standard interactive Python interpreter, I still would not use VS Code instead of Spyder in our installers.

hoechenberger commented 2 weeks ago

@cbrnr

I know everyone is going to hate me, but I'm currently switching my teaching from VS Code to Spyder. VS Code has too many basic issues for interactive work (e.g. no IPython integration, no way to run code from a script line by line, etc.). Unless you use notebooks (which I don't), so this might not be relevant then.

Is that still the case for you?

cbrnr commented 2 weeks ago

I never actually switched my teaching to Spyder, because it is too much effort and probably not worth it 😄. Things have improved a little meanwhile, because Smart Send is now enabled by default. There are still a couple of bugs and quirks that make the experience less than ideal. However, I'll probably stick with it and just hope that VS Code keeps improving (for example, the new interactive console experience might be worth looking into once it gets out of beta). IPython will likely never be integrated, but that's OK as long as there are decent alternatives, which I think are almost there.

hoechenberger commented 2 weeks ago

Then maybe my above proposal could actually work: we launch vs code from an activated MNE-Installer environment. It will then automatically pick up and activate that environment (at least by default)

we can easily add a shortcut for this (and display an informative error message if the "code" command cannot be found)