-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.

8
  • 1
    Please add what code you have and what you've tried. Commented May 11, 2023 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 Commented May 11, 2023 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). Commented May 11, 2023 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 ... Commented May 11, 2023 at 15:14
  • <continued> 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. 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. ... Commented May 11, 2023 at 15:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.