How to Run a Python Program in Visual Studio Code (VS Code)

Python Program in Visual Studio Code

Introduction :

Visual Studio Code (VS Code) is a popular and powerful code editor where you can write, edit and run various programming languages like C, C++, Javascript, Python etc. It is a versatile code editor developed by Microsoft that offers a range of features for coding, debugging and version control. In this article, I shall show you how run Python programs in Visual Studio Code (VS Code). If you want to enter the world of Python programming, you can use VS Code editor because it is straightforward, user-friendly and easy to use.

Essential steps to run Python programs in the Visual Studio Code :

In this article, I shall cover the following steps of setting up and running Python programs in the Visual Studio Code (VS Code).

Step 1 :

Before you begin, make sure you have Visual Studio Code and python interpreter installed on your system. If you haven’t already, download and install Visual Studio Code from the official website. You can also see how install VS Code on your pc from here. If you haven’t installed Python yet, you can download it from the official Python website.

Step 2 :

Now you have to set up Visual Studio Code for Python program. After installing Visual Studio Code, open it and click on the Extension icon in the sidebar or press Ctrl+Shift+X. Then, search for “Python” and install the official Python extension by developed Microsoft.

python extension for VS Code

Now, it is the time to select the Python interpreter (Optional but Recommended). Selecting a Python interpreter is important for running your Python code within the VS Code. At the bottom-left corner of the VS Code, you’ll see the current interpreter version (“Select Python Interpreter”). Click on it and choose the appropriate Python interpreter you want to use.

Python Interpreter for VS Code

Step 3 :

After setting up Visual Studio Code for Python programming, you have to create a workspace and also a Python file. A workspace is a directory that contains your project files. After open VS Code, click on “File” in the top-left corner and select “Open Folder”. Now you can create a new folder by clicking on “New folder” and change the name of the folder. Here, you can keep your Python project. Then, open that folder using VS Code.

python workspace for VS Code

After creating workspace, you have to create a new Python file with the .py extension. You can do this by clicking on the “File” then “New File” and saving it with a .py extension such as “pac.py”.

python file for VS Code

Step 4 :

Now, it is the time to write Python programs. In the newly created Python file (“pac.py”), you can write your own Python code. Now, you can create functions, define variables and include any necessary libraries in your newly created Python file. For example, you can create a simple “Hello World” program.

print("Hello World")

Step 5 :

Now, you have to run your Python file. There are multiple ways to run your Python file in VS Code.

Run Python program using the Integrated Terminal :

You can open integrated terminal in two ways. First click on “View” in the top menu and then “Terminal”. You can also press Ctrl+` to open the integrated terminal. Navigate to the directory where your Python file is located using the cd command. In terminal write “python pac.py” and press “enter” key. Replace “pac.py” with the actual name of your Python file.

Run Python program using the Integrated Terminal

Run Python program using the Run Button :

Above your main code, you’ll see a green arrow icon (play button) on the right side. Click on it to run your Python program. The output of your program will appear in the integrated terminal at the bottom.

Run Python program using the Run Button

Run Python program using the Command Palette :

Press Ctrl+Shift+P to open the command palette. Type “Run Python File in Terminal” and select it to run your Python file.

Run Python program using the Command Palette

Run Python program using the Context Menu :

Right-click anywhere inside your Python file in VS Code and select “Run Python File in Terminal” from the context menu. VS Code will automatically open the integrated terminal at the bottom of the window and run your Python program. You will see the output in the terminal.

Run Python program using the Context Menu

Step 6 :

At last, the output of your Python program will be displayed in the terminal at the bottom of the VS Code. Here, You can view any print statements or other type of output of your program.

Conclusion :

Finally, You’ve successfully run a Python program in Visual Studio Code (VS Code). By the end of this article, you’ll have a solid foundation in running Python programs within the VS Code. Thank you for visiting my site.

Scroll to Top