How to Set Up Python Virtual Environments in Visual Studio Code?

Estimated read time 2 min read

To set up Python virtual environments in Visual Studio Code, you need to follow these steps:

  1. Open your Python project in Visual Studio Code.
  2. Open the terminal in Visual Studio Code by clicking on “Terminal” in the top menu and selecting “New Terminal”.
  3. In the terminal, navigate to your project directory using the cd command.
  4. Create a new virtual environment using the following command:
python -m venv env

This will create a new virtual environment called env in your project directory.

  1. Activate the virtual environment using the following command:

This will activate the virtual environment and you should see the name of the environment displayed in the terminal prompt.

  1. Install any necessary packages using pip. For example, to install numpy, you would use the following command:
pip install numpy
  1. To use the virtual environment in Visual Studio Code, you need to select it as your Python interpreter. To do this, open the Command Palette by pressing Ctrl+Shift+P (Windows, Linux) or Cmd+Shift+P (Mac), then type “Python: Select Interpreter”. Select the option “Python: Select Interpreter” from the drop-down menu and choose the virtual environment you just created.

That’s it! You can now work in your virtual environment using Visual Studio Code. When you’re done working in the virtual environment, you can deactivate it using the following command:


You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply