-1

I am new to using VS Code. When I run a line of code in python and create a new object, such as a class, it does not store it in local memory. So if I run a second line of code calling the class right after the first line where I created the class, it says that class does not exist. So I have to run the entire block of code in its entirety every time to execute properly. This does not happen in Jupyter Notebooks. Is there a setting to adjust this? Thank you.

I googled this issue, I looked on stackoverflow, and I checked settings without success.

Peter Macej
  • 4,831
  • 22
  • 49
JTA1618
  • 25
  • 4
  • 1
    Please add what code you have and what you've tried. – imad.nyc May 11 '23 at 14:30
  • 1
    What do you mean by "run a line of code" - VS Code by itself does not execute code, you are most likely using some extension that does that instead – UnholySheep May 11 '23 at 14:32
  • This sounds a lot like what happens when you are using VSCode and not using Jupyter notebook ;) I think there is some option to use VSCode in a way similar to a python repl (I never use it - so I just have a vague recollection of seeing this). – topsail May 11 '23 at 14:41
  • @JTA1618 , you can get a persistent namespace other ways than Jupyter. Jupyter is definitely the most modern way though. And you can run Jupyter in VSCode. What you describe in your post though is the traditional way to run Python code not in a console. You are executing code and when it completes it is done and the scope/namespace it ran is cleared away. This is similar to writing Python code in a file and then pointing Python at that script on the command line and execuiting. Again, the scope and namespace will go away. What you seem to want to do is interactive coding in a console or ... – Wayne May 11 '23 at 15:14
  • Jupyter. You can do that with some convenient settings inside even VSCode by using IPython as the console/interpreter, see this thread, especially the comment I point directly at, [here](https://stackoverflow.com/questions/52310689/use-ipython-repl-in-vs-code#comment115745795_60272350). IPython was Python where they added convenient tools, abilities, and magic commands. It evolved into IPython and IPython notebook and then IPython Notebook evolved into Jupyter when it became able to run other kernels besides IPython. But IPython is still very actively used. ... – Wayne May 11 '23 at 15:24
  • Plus, you can use Python interactively from within VSCode, see [here](https://www.reddit.com/r/vscode/comments/gtvk9q/how_to_start_python_console_in_vs_code/) and [here](https://stackoverflow.com/a/49269921/8508004). But you won't have a lot of abilities as far as I know because basically it is separate from VSCode. I think if you use the debugger right, you can get the interactivity where you can query the running namespace and run additional code. I think. ... – Wayne May 11 '23 at 15:25
  • See [here](https://code.visualstudio.com/docs/python/debugging) and [here](https://code.visualstudio.com/docs/python/python-tutorial#_configure-and-run-the-debugger). – Wayne May 11 '23 at 15:25
  • I agree, it's unclear how you're running your code. What button/command/keybinding are you using, and/or what extension? Please [edit] to clarify. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea May 12 '23 at 01:55

1 Answers1

0

If you want to run the codes line by line, you could use debug mode.

You can add breakpoint and use step into button.

enter image description here

Another way is to use jupyter. Install Jupyter extension and run cell one by one.

enter image description here

You can also try to use interactive window which is the same as run jupyter cell in .py file.

enter image description here

Right click and select Run Selection/Line in Interactive Window, it'll also achieve your goal.

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13