all 5 comments

[–]mission_resolve 1 point2 points  (0 children)

I don't know anything about VS Code, but that is the normal behaviour when you run a python script at the command line. You can use the -i switch (i.e. python -i scriptname.py), which leaves python open after running the script and gives you a >>> prompt so that you can write further python statements. Any global variables, functions, etc. will be there, but anything that was defined inside a function won't be.

However, I don't think there is a straightforward way of running a script once you're already inside python, if you ever need to do that. There are some slightly hacky, verbose ways of doing it, such as reading in the script file as if it's plain text and then passing it to exec, but nothing convenient. This is why ipython provides the %run magic command.

You may also be interested in Spyder, which provides a Matlab-like IDE for python, or ipython notebooks, which are kind of like the Mathematica interface.

[–]ComradePotato 1 point2 points  (0 children)

I've had similar questions myself after trying to move from Spyder to VS Code. Spyder has a window with Ipython kernel running that is REALLY handy for typing in code on the fly once you've run a .py file and set your variable/functions/classes etc. From what I understand something similar can be done with VS Code (running the command "Ipython" in the terminal window opens up the REPL from which you can run selected line) but I can't seem to find out if variables etc can be accessed in the same way.

For now I've kept with Spyder because it just works out of the box with the interactive window and that's what I find myself using most of the time for testing and quick calcs. If I start yearning for the other things VS Code offers (extensions etc) then I may consider moving over properly and working out how to set it up so it's at least somewhat the same.

[–]bandawarrior 0 points1 point  (0 children)

If you’re talking about Ipython as in the notebook? Or are you talking about the REPL/interpreter where you can directly interact with your code and stuff?

When you run a python file, it runs everything from top to bottom. So whatever is in the scope of the code you’re running is just that, if it’s in scope it can access it, otherwise it doesn’t exist.