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:
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.
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 +
`.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
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.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.
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.