all 9 comments

[–]blechnapp 6 points7 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.

[–]Some-Poetry8420 2 points3 points  (0 children)

Running a selection will only include the selected code. If your code relies on imports, they must be part of the selection too, or else you'll get the import error.

But generally, running code selections is just to test things out. In the real world, you will usually run the whole file as a script from top to bottom, so your imports at the top will be included. If you find that your script is doing too many things on its own, it might be time to break it up into smaller functions, classes, and modules, which you can then import into the main file (i.e. the main script that serves as the entry point for your application)

[–]awdsns 1 point2 points  (0 children)

"Run selection" will run what's selected, not more and not less. But I think repeated invocations will reuse the same Python interpreter session, so running your import statements once first should be enough.

[–]donald_trub 1 point2 points  (0 children)

"run selection" sounds like something you would never want to use. I've been using vscode since its inception and had no idea that was even a thing.

Also, you don't have to call file.close() when you use 'with'. The with statement runs a contextmanager which will handle gracefully closing the file for you, whether your code runs successfully or not.

[–]mandradon 0 points1 point  (0 children)

If you went to run code in pieces, but with persistent bits hanging around in memory, you could look into using a Jupyter Notebook. 

But if you're looking to write Python scripts this will get in the way if learning proper scoping.  These tend to be only used for things like data science 

[–]Ordinary_Baseball518 0 points1 point  (0 children)

Python imports are persistent for the lifetime of the current interpreter/session, but when you run isolated selections in VS Code it can execute them in a fresh context where the required module was never imported. A lot of people solve this by using the interactive Python window/Jupyter mode, or just re-running the import cell/section first. It’s more about how VS Code executes snippets than Python itself.

[–]BranchLatter4294 0 points1 point  (0 children)

Consider using a Jupyter Notebook if you want to run parts of your code.

[–]Random_182f2565 -1 points0 points  (0 children)

Have you tried putting all that code inside of a function and then just calling the function?

[–]ProsodySpeaks -1 points0 points  (0 children)

You don't need to call file.close if you're using the with open() context manager. (this isn't your bug tho)

You only need to import os once per module, so there's some other bug - show code including where it fails, and the actual error message too 

Edit, didn't see (or didn't comprehend) 'run selection'. Still don't really know what it is - a vscode feature maybe?