![]() |
VOOZH | about |
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.
A 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.
We 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):
$ 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:
To 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).
$ 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.
👁 activate virtual environment in Python
This will let you know that the virtual environment is currently active.
Once 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.
Once you are done with the work, you can deactivate the virtual environment by the following command:
(virtualenv_name)$ deactivate
👁 deactivate virtual environment in Python
This returns you to the system’s default Python environment.