close

How to execute Python code from within Visual Studio Code

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to execute Python code from within Visual Studio Code in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to execute Python code from within Visual Studio Code?

  1. How to execute Python code from within Visual Studio Code?

  2. Install the Python extension (Python should be installed in your system). To install the Python Extension, press Ctrl + Shift + X and then type 'python' and enter. Install the extension.
  3. execute Python code from within Visual Studio Code

  4. Install the Python extension (Python should be installed in your system). To install the Python Extension, press Ctrl + Shift + X and then type 'python' and enter. Install the extension.

Method 1

All these answers are obsolete now.

Currently you have to:

  1. install the Python language extension (and Python, obviously)
  2. open folder (important!), open any Python file inside that folder
  3. switch to debug “tab”(?) and click on the gearbox (with hint ‘Configure of Fix ‘launch.json”)
  4. save the opened launch.json file (it’s placed in .vscode subdirectory in the folder opened in step #2)
  5. finally, click the green triangle or hit F5

No additional extensions or manual launch.json editing is required now.

Method 2

  1. Install the Python extension (Python should be installed in your system). To install the Python Extension, press Ctrl + Shift + X and then type ‘python’ and enter. Install the extension.
  2. Open the file containing Python code. Yes! A .py file.
  3. Now to run the .py code, simply right click on the editor screen and hit ‘Run Python File in the Terminal’. That’s it!

Now this is the additional step. Actually I got irritated by clicking again and again, so I set up the keyboard shortcut.

  1. Hit that Settings-type-looking-like icon on bottom-left side → Keyboard Shortcuts → type ‘Run Python File in the Terminal’. Now you will see that + sign, go choose your shortcut. You’re done!

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read