Last Updated : 29 Jul, 2025
A virtual environment is an isolated Python environment that allows you to manage dependencies for each project separately. It prevents conflicts between projects and avoids affecting the system-wide Python installation. Tools like venv or virtualenv are commonly used to create them.
Why do we need a Virtual EnvironmentA virtual environment should be used for every Python project. By default, all Python projects share the same location to store packages. This can cause problems if two projects need different versions of the same package, like Django. A virtual environment solves this by creating a separate space for each project’s packages, so they don’t interfere with each other. It’s basically a folder with its own Python setup. Using a virtual environment helps avoid conflicts and keeps your projects clean and organized.
How to Create a Virtual Environment in PythonWe use the virtualenv module to create isolated environments. It creates a folder with all necessary executables for the project.
Step 1: Installing virtualenv
$ pip install virtualenv
Step 2: Check Installation
$ virtualenv --version
Step 3: Create a Virtual Environment
$ virtualenv my_env
This creates a directory named my_env containing the isolated environment. To use a specific Python interpreter (e.g., Python 3):
Activating a Virtual Environment in Python$ virtualenv -p /usr/bin/python3 my_env
Now after creating a virtual environment, you need to activate it. Remember to activate the relevant virtual environment every time you work on the project. This can be done using the following command:
1. On WindowsTo activate virtual environment using windows command prompt change directory to your virtual env, Then use the below command
$ cd <envname>$ Scripts\activate
Note: source is a shell command designed for users running on Linux (or any Posix, but whatever, not Windows).
2. On Linux/macOS$ source virtualenv_name/bin/activate
Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal.
This will let you know that the virtual environment is currently active.
Installing Dependencies in Virtual EnvironmentOnce your virtual environment (e.g., venv) is active, you can install project-specific dependencies inside it.
For example, to install Django 1.9:
(virtualenv_name)$ pip install Django==1.9
This installs Django 1.9 inside the virtualenv_name folder, keeping it isolated from the system Python.
Deactivate Python Virtual EnvironmentOnce you are done with the work, you can deactivate the virtual environment by the following command:
(virtualenv_name)$ deactivate
This returns you to the system’s default Python environment.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4