skip to Main Content

New to vs code here, but I am trying to figure out some python currently with some basic matplotlib figures. At first when i would run it line by line it would work fine, but then i was not able to run the whole .py file from the run button at the top. Searched and read that it was a "bug" or just not intuitive and the thread said if you run exit() it should work after that. Followed the instructions, and running the whole file now worked, however, now running line by line does not work and gives an error message.

now for my code its just some simple example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,50,75)
plt.plot(x, np.sin(x))
plt.show()

but after doing exit() and getting it to work to run the full file, i cannot run each individual line of code and get errors such as:

PS C:UsersnicolDesktopPython Workspace> import matplotlib.pyplot as plt
import : The term 'import' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that 
the path is correct and try again.
At line:1 char:1
+ import matplotlib.pyplot as plt
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (import:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

How would i get it to work well again with running each individual line? or is it just some vs thing thats not intuitive and just have to choose one or the other to work with?

2

Answers


  1. PS C:UsersnicolDesktopPython Workspace> import matplotlib.pyplot as plt
    

    You should not directly enter Python statements in the shell.

    If you want to run the codes line by line, you can use debug mode (F5 or Debug Python File).

    enter image description here

    Then you can use step-into(F11) in order to enter the next method to follow its execution line-by-line.

    enter image description here

    Login or Signup to reply.
  2. With VSCode you can run a Python code "line-by-line" either in the terminal or in an interactive window (similar to Jupyter Notebooks).

    Terminal:
    For the first option, you will need the Python extension ("ms-python.python"). Then you should be able to right-click on the line you want to run and click "Run Selection/Line in a Python Terminal".

    enter image description here

    Interactive window: You will have to install both the Python extension and the Jupyter extension ("ms-toolsai.jupyter"). Then you should be able to right-click on the line you want to run and click "Run in Interactive Window > Run Selection/Line in Interactive Window". You might need to install an IPython kernel for that, but the first time you try using the interactive window VSCode should allow you to install it.

    enter image description here

    For both commands, you can also set keyboard shortcuts. I find them very useful, especially the interactive window command.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search