Technical Articles

How to Execute Python Files in VS Code

How to Execute Python Files in VS Code

In this article we will show how we can run Python programmes in Visual Studio Code. The Python Extension will make Visual Studio Code a lightweight Python IDE, which will be an alternative to PyCharm. also cover how VS code will work for the Python environment and how to debug code through

Before begun here are the few prerequisites,

  • VS Code ( Source visualstudio.com)
  • VS Code Python Extension (Source VS code market place)
  • Python 3 (Source python.org)

Steps to Install VS Code and Python Extension:

  • VS Code will be the direct source from the website to install in local system.
  • Python extension will be at marketplace.visualstudio.com and search for Python for better search it will be seen as Microsoft published.

Steps To Install Python Interpreter:

Interpreter is impotent one to execute the python files and along with Python Extension need to install a Python interpreter too. Based on the platform installation procedures as follows

Linux

Default Python3 installed in Linux machines but for additional packages we need to install pip.

macOS

In mac installation preocedure should be using Homebrew , bew install python3 via terminal.

Windows

Directly download python3 from the python official website python.org and install

Steps to Validate Python Installation:
Windows:

py -3 --version


Linux/macOS:

python3 --version

Steps To Set Python Interpreter In VS Code:

To run Python code, you need to select an interpreter as which interpreter VS code needs to run the files. To do this, use Ctrl+Shift+p to search Python: You can also select Python Environment from the status bar. By selecting this Python Extension interpreter, the workspace will be set for,

Steps To Run Python Files:

  • Create a sample file with ” Hello world” and print statement.
  • After that save the file and make sure file saved in .py format and run that file using play Icon on top right corner of the file.
    -You can also right-click on the file to open a menu of options. If you wish to run each line of the file, choose Run selection/Line In Python Terminal.

Steps To Debug Python Files:

-To debug the python syntax and code related to the same open file, beside the play button, there is a drop down icon that will be there and select Debug Python File in Terminal.


-If you want to debug the file through the breakpoint, place the cursor on the line and press F9. That will set the breakpoint appearing in a red circle. Now press F5 to start the debugger. You will get the dropdown listing a couple of options.

Tags:python,Python3,VS code, Python Modules,Techarticles, mytecharticle, visualstudio, VB Code

About the author

admin

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.