Python

How to create a virtual environment in python

In this article, introduce how to create a virtual environment in Python.

Let’s get straight to the point.

My environment

Python version: Python 3.12.2
CLI: git bash

Why create a virtual environment

If you don’t use a virtual environment and you have multiple project, you may occur package version conflict.

For example, we can think the case that project A need to use package version 3.10, but project B need to use version more than 3.11.

When occur this case, your project have an impact.

So you need to create a virtual environment per your project.

How to use a virtual environment

Create a virtual environment

There is a standard feature “venv” of to manage virtual environment in python.

First of all, you need to install python in your computer.

After the installation complete, you need to execute the following command in your computer.

python -m venv env

You can create env folder.

It manage package of your project in this folder.

Activate of a virtual environment

You can’t use a virtual environment only creating env folder.

You need to execute the following command in your computer to activate a virtual environment.

source env/Scripts/activate

This command need to execute every opening CLI.

Deactivate of a virtual environment

If you want to deactivate a virtual environment, you can execute the following commnad.

deactivate