Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved UX with Python in VS Code #170

Open
hoechenberger opened this issue Feb 14, 2023 · 26 comments
Open

Improved UX with Python in VS Code #170

hoechenberger opened this issue Feb 14, 2023 · 26 comments
Labels

Comments

@hoechenberger
Copy link
Member

hoechenberger commented Feb 14, 2023

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
Copy link
Member

agramfort commented Feb 14, 2023 via email

@cbrnr
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Member Author

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
Copy link
Collaborator

cbrnr commented Feb 14, 2023

@hoechenberger this is using a notebook.

@hoechenberger
Copy link
Member Author

No? It's a script...

@cbrnr
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Member Author

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
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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

@hoechenberger
Copy link
Member Author

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
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Member

@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
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Member

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

Screen.Recording.2023-02-14.at.21.49.45.mov

@cbrnr
Copy link
Collaborator

cbrnr commented Feb 14, 2023

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
Copy link
Member Author

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
Copy link
Collaborator

cbrnr commented Feb 15, 2023

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
Copy link
Member

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
Copy link
Member Author

hoechenberger commented Feb 15, 2023

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
Copy link
Member

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
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Collaborator

cbrnr commented Nov 3, 2023

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
Copy link
Member Author

@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
Copy link
Collaborator

cbrnr commented Jul 10, 2024

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
Copy link
Member Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants