Problem Formulation and Solution Overview
Virtual Environments are best used when a coder or several coders work together to develop medium to large-scale applications. The best approach is to keep this code and associated libraries and dependencies separate. This is where a Virtual Environment comes in!
Setting up the Environment
Open the Visual Studio Code Editor and create a new folder that will house the Python script to be worked on.

Create a blank script file called sales.py
and place this file into this directory.

From the View menu, select Command Pallette or press CTRL+SHIFT+P
on the keyboard for the same result.

The above action displays a dropdown box. Start typing the word Python: Create Environment, or if visible, click to select this option.

The above action displays another dropdown box. For this article, the first option was selected (Venv
).

The above action displays another dropdown box. For this article, the second option was selected (Python 3.11.0 64-bit
).

The above action starts the installation of the Virtual Environment. Wait for this process to complete.

Once the Virtual Environment is installed, the following occurs:
- A message in the status bar indicates the name of the Virtual Environment (
.venv:env
) - A new folder called
.venv
is created.

Next, locate and open the activate.bat
from the .venv\Scripts
folder. From the Main Menu, Click Run β Start Debugging
or press F5
on the keyboard.
If successful, the command prompt changes similar to below.

You are now officially working in a Virtual Environment.
π‘Note: If an error is generated when running this .bat
file, you may need to install an extension that reads Batch files. For this article, Batch Runner was installed.
Install Required Library
For this article, our sales.py
file reads in the sales.csv
file. This means that the pandas
library will need to be installed. To perform this task, navigate to the terminal window and run the following code.
pip install pandas
If successful, the pandas
library is now installed in the Virtual Environment and if we run sales.py
there should be no errors.

π‘ Note: Install any other required libraries the same way.
Generate a Requirements File
A Requirements File can be created at any time during the development cycle. This file is a checklist for the Python application in question. It lists all libraries and associated versions used in the app.
To generate a requirements.txt
file, navigate to the terminal and run the following code:
pip freeze > requirements.txt
This action generates the above-noted file and places it into, for this example, the SALES_APP
folder. Open this file and scroll down to see the pandas
library listed, including the version number. This is handy when deploying to another Virtual Environment to ensure the correct library versions are installed.
Deactivating the Virtual Environment
To deactivate the Virtual Environment, click the (.venv:env
) link in the bottom-right of the IDE. This action displays the Interpreter dropdown box. Select Python 3.11.0 64-bit
or any other option that does not contain the (.venv:env
) words.

If successful, the status bar no longer has the (.venv:env) link, and the prompt no longer contains that as well.

While still in the Virtual Environment, another option is to navigate to the Scripts folder, open and run deactivate.bat
file
Summary
This article showed you how to setup, activate, and de-activate a Virtual Environment in the VS Code IDE.