Open In App

How to Set Up a Python Virtual Environment in Visual Studio on Windows

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating a Python virtual environment is a fundamental practice for managing dependencies and ensuring project isolation. This guide will walk us through the steps to set up a Python virtual environment in Visual Studio on a Windows machine, providing a solid foundation for our Python project development.

Why is a Virtual Environment Important?

Suppose, we have multiple projects, one project uses Django 4.2 and the other uses Django 5.0. Each of the Django versions depends on specific versions of Python packages and we must separate their dependencies that's where virtual environments come into picture.

A virtual environment is important because it isolates a Python project's dependencies from the system-wide packages. This prevents conflicts between different projects that may require different versions of the same library. By creating a self-contained environment, we ensure consistent behavior across different systems, avoid version mismatches, and maintain a clean global Python installation. Virtual environments also help in managing dependencies efficiently, making our project more portable and easier to collaborate on.

Prerequisites

Before we start, we must have the following installed on our Windows system:

Steps to Set Up a Python Virtual Environment

Step 1: Open Visual Studio

Launch Visual Studio from the Start menu. Once the IDE is open, we can either create a new project or open an existing one.

Screenshot-2024-09-23-114119
Open Vs Code

Step 2: Create a New Project

  • Create a folder name test_project
  • Use open folder option to open the folder.

Step 3: Open the Integrated Terminal

  • Visual Studio includes an integrated terminal where you can run command-line commands.
  • Go to View > Terminal or press Ctrl + ` to open the terminal pane at the bottom of the IDE.
Screenshot-2024-09-23-114627
Open the Terminal in VsCode

Step 4: Create a Virtual Environment

To create a virtual environment, we can use the Python inbuilt venv package.

Run the following command to create a virtual environment:

python -m venv environment
Screenshot-2024-09-23-115235
Create a virtual Environment in Python

This command creates a new directory called environment in our project folder, containing the Python executable and necessary files for the virtual environment.

Also Read:

Step 5: Activate the Virtual Environment

To start using our virtual environment, we need to activate it. Run the following command in the terminal:

.\envrionment\Scripts\activate
Screenshot-2024-09-23-115340
Activate Virtual Environment in VsCode

We should see the terminal prompt change to indicate that the virtual environment is active, typically by prefixing the prompt with (environment).

Step 6: Install Required Packages

With our virtual environment activated, we can now install any required packages using pip. For example, to install Flask, run:

pip install flask

The installed packages won't be accessible outside the virtual environment.

Check Installation:

We can check the flask version installed.

Screenshot-2024-09-23-115656
Check Flask Version

Let's installed some more packages like: psycopg2, pydantic, sqlalchemy, and cryptography

pip install psycopg2 pydantic sqlalchemy cryptography

Check all installed Packages using Pip Freeze:

Screenshot-2024-09-23-120748
Check all installed Packages using Pip Freeze:

Step 7: Deactivate the Virtual Environment

When we're done working in the virtual environment, we can deactivate it by running:

deactivate
Screenshot-2024-09-23-120835
Deactivate Virtual Environment

This command returns our terminal to the global Python environment.

Conclusion

Setting up a Python virtual environment in Visual Studio on Windows is a straightforward process that greatly enhances the manageability and portability of our Python projects. Virtual environments ensure that our project's dependencies are isolated, preventing conflicts and improving reproducibility. With Visual Studio's rich integration for Python development, we can easily create, manage, and work with virtual environments, giving you the tools we need to write efficient, scalable, and organized Python code.


Next Article
Article Tags :
Practice Tags :

Similar Reads