Django Getting started with Django Virtual Environment

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


Although not strictly required, it is highly recommended to start your project in a "virtual environment." A virtual environment is a container (a directory) that holds a specific version of Python and a set of modules (dependencies), and which does not interfere with the operating system's native Python or other projects on the same computer.

By setting up a different virtual environment for each project you work on, various Django projects can run on different versions of Python, and can maintain their own sets of dependencies, without risk of conflict.

Python 3.3+

Python 3.3+ already includes a standard venv module, which you can usually call as pyvenv. In environments where the pyvenv command is not available, you can access the same functionality by directly invoking the module as python3 -m venv.

To create the Virtual environment:

$ pyvenv <env-folder>
# Or, if pyvenv is not available
$ python3 -m venv <env-folder>

Python 2

If using Python 2, you can first install it as a separate module from pip:

$ pip install virtualenv

And then create the environment using the virtualenv command instead:

$ virtualenv <env-folder>

Activate (any version)

The virtual environment is now set up. In order to use it, it must be activated in the terminal you want to use it.

To 'activate' the virtual environment (any Python version)

Linux like:

$ source <env-folder>/bin/activate

Windows like:


This changes your prompt to indicate the virtual environment is active. (<env-folder>) $

From now on, everything installed using pip will be installed to your virtual env folder, not system-wide.

To leave the virtual environment use deactivate :

(<env-folder>) $ deactivate

Alternatively: use virtualenvwrapper

You may also consider using virtualenvwrapper which makes virtualenv creation and activation very handy as well as separating it from your code:

# Create a virtualenv
mkvirtualenv my_virtualenv

# Activate a virtualenv
workon my_virtualenv

# Deactivate the current virtualenv

Alternatively: use pyenv + pyenv-viritualenv

In environments where you need to handle multiple Python versions you can benefit from virtualenv together with pyenv-virtualenv:

# Create a virtualenv for specific Python version
pyenv virtualenv 2.7.10 my-virtual-env-2.7.10

# Create a vritualenv for active python verion
pyenv virtualenv venv34

# Activate, deactivate virtualenv
pyenv activate <name>
pyenv deactivate

When using virtualenvs, it is often useful to set your PYTHONPATH and DJANGO_SETTINGS_MODULE in the postactivate script.

# This hook is sourced after this virtualenv is activated

# Set PYTHONPATH to isolate the virtualenv so that only modules installed
# in the virtualenv are available
export PYTHONPATH="/home/me/path/to/your/project_root:$VIRTUAL_ENV/lib/python3.4"

# Set DJANGO_SETTINGS_MODULE if you don't use the default `myproject.settings`
# or if you use `django-admin` rather than ``

Set your Project Path

It is often also helpful to set your project path inside a special .project file located in your base <env-folder>. When doing this, everytime you activate your virtual environment, it will change the active directory to the specified path.

Create a new file called <env-folder>/.project. The contents of the file should ONLY be the path of the project directory.


Now, initiate your virtual environment (either using source <env-folder>/bin/activate or workon my_virtualenv) and your terminal will change directories to /path/to/project/directory.

Got any Django Question?