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 configuration options and there is no option for this.
2
Answers
I can’t tell exactly how your editor is configured at the moment.
Generally, you can configure what happens when you run and debug the application in the "Run and Debug" tab and with the
.vscode/launch.json
configuration file.Typically, this configuration file is generated automatically. If you start debugging for the first time, it should offer you several options and choosing one will generate the
.vscode/launch.json
file.Alternatively, if you already have an existing configuration or need several different configurations, you can open the "Run and Debug" tab and either select an existing configuration or generate a new one:
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.Add the magic command
#%%
above your code, Then you will get a jupyter-like interface.select Run Cell or other run mode, and an interactive window will open.
Or right-click directly in the code file and select
Run Current File in Interactive Window
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
Type Run Current File in Interactive Window in the search box
Double click or right click and select
Add Keybinding...
Press the shortcut key you want to bind (Try to avoid conflicts with other shortcut keys)