Activate Virtual Environment in Django

  1. Python Virtual Environment
  2. Set Up a Virtual Environment
  3. Create a Virtual Environment
  4. Activate a Virtual Environment
Activate Virtual Environment in Django

When working on Python projects, we sometimes need a bunch of libraries and packages. By default, all the libraries and packages that we install are installed for the Python installed globally on our system. It can cause a few problems at times.

Suppose that you have Python 3.9 globally installed on your system, and a bunch of your projects is using it. Now, you come up with an excellent idea and start surfing the internet for all the resources you will need to execute your idea.

You find that you’d need Python library XYZ for your project, but the condition is that this library requires Python 3.6 to work. But you have Python 3.9 on your machine, and removing it will break all the other projects. Also, you can’t dump the idea and start executing ideas based on the Python version installed on your machine.

So, to avoid such conflicts, virtual environments are used.

Python Virtual Environment

A virtual environment is an isolated Python environment created for a project. This environment has its own Python interpreter, libraries, and packages, which means that the Python interpreter and the installed dependencies belong to this project only. We can have different Python versions and diverse libraries for various projects with virtual environments without piling up the dependencies installed globally.

When starting a new project, it is always recommended to create a virtual environment and use it specifically for that project.

Set Up a Virtual Environment

It’s effortless to set up a virtual environment for your Python projects. To create a virtual environment, we need a Python package, namely, virtualenv. This package can be installed using the following pip command.

pip install virtualenv

Create a Virtual Environment

To create a virtual environment, run the following command in the console.

virtualenv environment

environment is the name of the virtual environment. You can give it any name. By default, the Python version of this environment will be the same as the Python version installed globally on your machine or at the top in your environment variables if you have multiple Python versions.

To create an environment with a custom Python version, use the following command. Ensure that you have that Python version installed on your machine as well. Otherwise, it will throw an error.

virtualenv --python=\path\to\the\python\version\python.exe environment

So, if you have Python 3.9 and 3.7 on your machine and wish to create an environment with Python 3.7 on Windows, your command would look like this. (Considering the default location of Python)

virtualenv --python=C:\Users\User-Name\AppData\Local\Programs\Python\Python37\python.exe environment

Activate a Virtual Environment

Before activating the environment, make sure that the working directory of the console has a virtual environment.

To activate a virtual environment in Windows, use the following command.


For Mac and Linux, use the following command.

source environment/bin/activate

To deactivate a virtual environment, run the following command.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub