Home > database >  How to execute the whole current file in the VSCode Python REPL?
How to execute the whole current file in the VSCode Python REPL?

Time:08-30

I'm working on a project that depends on a big library that takes a long time to load. I use VSCode and I'm looking for a workflow that doesn't import this library each time I need to execute my code to check the results.

I think that executing the current file in the VSCode Python REPL is probably the best way to do this. For now, the best I can do is to select all my code (ctrl a), then execute it in on the repl (shift enter), which is a bit tedious.

Is there a way to get this behavior by just pressing f5 or another unique key?

I looked at the Choosing debug configuration.

If you choose "Python / Dynamic Python: File" that will execute the file that you have open in the editor.

It is also possible to edit the .vscode/launch.json file for more advanced scenarios.

CodePudding user response:

Add the magic command #%% above your code, Then you will get a jupyter-like interface.

enter image description here

select Run Cell or other run mode, and an interactive window will open.

enter image description here

Or right-click directly in the code file and select Run Current File in Interactive Window

enter image description here

You can also add a shortcut key for this action:

  • Click on the gear icon at the bottom left of vscode and select Keyboard Shortcuts

    enter image description here

  • Type Run Current File in Interactive Window in the search box

  • Double click or right click and select Add Keybinding...

    enter image description here

  • Press the shortcut key you want to bind (Try to avoid conflicts with other shortcut keys)

    enter image description here

  • Related