How do I install virtual venv in VS code?

In Visual Studio Code (VS Code), you don't actually "install" a virtual environment; rather, you create and activate a virtual environment using the terminal integrated into VS Code. Here are the steps to create and activate a virtual environment in VS Code:

  1. Install Python: Make sure you have Python installed on your machine. You can download it from the official Python website. During the installation process, make sure to check the option to add Python to your system PATH.

  2. Open a New Terminal: Open VS Code and open a new terminal. You can do this by selecting "View" in the top menu and then choosing "Terminal" or by using the keyboard shortcut Ctrl + `.

  3. Navigate to Your Project Directory: Use the cd command to navigate to the directory where you want to create your virtual environment.

     cd path/to/your/project
    
  4. Create a Virtual Environment: Run the following command to create a virtual environment. This example uses the venv module, which is included with Python 3.3 and later versions.

     python -m venv venv
    

    This command creates a folder named venv in your project directory, containing the virtual environment.

  5. Activate the Virtual Environment: Depending on your operating system, the activation command will differ:

    • On Windows:

        .\venv\Scripts\activate
      
    • On macOS/Linux:

        source venv/bin/activate
      

After activation, you should see the name of your virtual environment in the terminal prompt.

  1. Verify Activation: You can verify that your virtual environment is active by checking the terminal prompt for the virtual environment name or by running the following command:

     which python
    

    It should point to the venv directory.

Conclusion:

Now, you have successfully created and activated a virtual environment in VS Code. You can install and manage Python packages within this isolated environment for your project.

0
Subscribe to my newsletter

Read articles from LingarajTechhub All About Programming directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

LingarajTechhub All About Programming
LingarajTechhub All About Programming

Corporate Software Development Trainer with a demonstrated track record of success in the IT and Ed-tech industries for product development. I started my career as a software developer and have since taught C, C++, Java, and Python to hundreds of IT enthusiasts, including corporate professionals, throughout the years. I have been developing software for over 12 years.