How to Create Python Project in Visual Studio Code

Visual Studio Code (VS Code) is a powerful, open-source code editor that supports multiple programming languages, including Python. In this guide, we will walk through the steps to create your first Python project in VS Code, covering everything from installation to running your code. Let’s dive in!

Setting Up Your Environment

The first step to creating a Python project in Visual Studio Code is to ensure you have the right environment set up. This involves installing Python and the necessary extensions in VS Code.

Installing Python

To run Python code, you need to have Python installed on your machine. Follow these steps:

  • Visit the official Python website.
  • Download the latest version of Python for your operating system.
  • Follow the installation instructions provided on the website.

Installing Python from python.org

Installing Visual Studio Code

If you haven’t installed Visual Studio Code yet, you can download it from the official VS Code website. Follow the installation prompts for your operating system.

Opening Visual Studio Code

Creating Your Project Folder

Once you have Python and VS Code installed, you can start creating your Python project.

  1. Open Visual Studio Code.
  2. Click on the Open Folder button or go to File > Open Folder.
  3. Create a new folder for your project, for example, my_first_py_project, and select it.

Creating a new project folder in VS Code

Creating Your First Python File

Now that your project folder is set up, let’s create a Python file.

  1. In the Explorer view, right-click on your project folder and select New File.
  2. Name your file main.py.

Creating a new Python file named main.py

Writing Your First Python Code

Open your main.py file and type the following code:

print("Hello, World!")

To save your file, press Ctrl + S.

Writing Hello World in main.py

Installing the Python Extension

To effectively run and debug Python code in VS Code, you need to install the Python extension provided by Microsoft.

  1. Click on the Extensions icon in the Activity Bar on the side of the window or press Ctrl + Shift + X.
  2. Search for “Python” in the Extensions Marketplace.
  3. Click on the Python extension by Microsoft and then click Install.

Installing Python extension in VS Code

Running Your Python Code

Once the extension is installed, you can run your Python code directly from VS Code.

    1. Open the terminal by going to Terminal > New Terminal.
    2. In the terminal, type the following command to run your Python file:
python main.py

This command will execute your script, and you should see “Hello, World!” printed in the terminal.

Running main.py in the terminal

Using the Run Button

To make running your code even easier, you can use the run button that appears in the top right corner of the editor once the Python extension is installed.

Simply click the play button (run button) to execute your code without opening the terminal manually.

Using the run button in VS Code

Creating Additional Python Files

As you expand your project, you may want to create additional Python files. You can repeat the steps to create a new file, for example, main2.py, and write a simple print statement:

print("Hello from main2!")

Run it the same way you did with main.py.

Creating an additional Python file named main2.py

Conclusion

Congratulations! You’ve successfully set up your first Python project in Visual Studio Code. You learned how to create a project folder, write Python code, install necessary extensions, and run your code efficiently. If you have any questions, feel free to leave them in the comments below!

For more tutorials and resources, you can check out supporting my work or visit my YouTube channel

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Sharing is caring!

Leave a Comment

Your email address will not be published.