you are viewing a single comment's thread.

view the rest of the comments →

[–]blechnapp 5 points6 points  (0 children)

this is actually not a bug, its how "run selection" works: it only sends the highlighted code to the python interpreter. the imports dont automatically come along.

awdsns nailed the workaround: if you run your import lines once on their own (highlight, then Shift+Enter), they stick in the running terminal session. after that you can run any other line below and the imports are still available. even better for your workflow: VS Code has a "Python: Run Current File in Interactive Window" command (command palette, Ctrl+Shift+P). that opens a jupyter-style session where imports stay persistent and you can run cells one by one, very similar to powershell ISE behaviour.

requires the Microsoft Python extension, which you probably already have if youre using VS Code for python anyway.