Setting up Python Virtual Environments on Visual Studio Code in Windows 10

Soorya Narayan Satheesh
3 min readFeb 22, 2024

--

Creating a virtual environment for running Python Flask applications on Visual Studio Code (VSCode) is crucial and all software engineers must know how this is carried out due to several reasons.

Generally, Virtual Environments are crucial for the following reasons :-

  1. Isolation/Dependency Management: Virtual environments provide a contained space for projects, ensuring that dependencies for one project do not interfere with another. This prevents conflicts between different versions of libraries or packages required by different projects
  2. Reproducibility: Virtual environments make it easier to reproduce the development environment on different machines or for different developers. By encapsulating dependencies, developers can share the environment configuration, making it simpler to recreate the exact setup needed to run the project.
  3. Sandboxing: Virtual environments provide a sandboxed environment where developers can experiment with different libraries, packages, or configurations without affecting the system-wide installation. This allows for safe testing and exploration without risking the stability of the entire system.

If you are trying to setup a virtual environment on VSCode in a system with Windows 10 operating system you can follow the following steps

Step 1 — In VSCode open the folder where you want to save your project

Step 2- Click Cntrl +J or select option from the drop down menu below to open the Terminal

Step 3- Check if your Terminal is open in the correct project folder

Step 4- Run command for creating the Virtual Environment . “.venv” creates a hidden folder of the same name which stores all the required files for the virtual environment.

python3.12 -m venv .venv

Step 5- Check under the explorer tab if a .venv folder is created as shown below.

Step 6- Press Cntrl+Shift+P to choose the Python Interpreter for the project

Step 7- Choose the interpreter with the .venv text attached to it.

Step 8- Kill the terminal and reopen it. If you can see a (.venv) symbol before your shell prompt then the Virtual environment has been correctly activated . Else you can follow the following steps

Step 9- Run the following shell command to activate the virtual environment

.venv\Scripts\activate

Step 10- Some users may come across an error mentioning that this system does not allow scripts to be run. In that case you need to open the Power shell as an administrator. Then run the following command and press “Y” key to accept. After that rerun the previous command.

Congratulations, you have setup your virtual environment in Python on VSCode. Enjoy making amazing projects !!

--

--

Soorya Narayan Satheesh
Soorya Narayan Satheesh

Written by Soorya Narayan Satheesh

UC Berkeley Graduate, Software Engineer, Machine Learning specialist and Geospatial evangelist