Last modified: Mar 10, 2025 By Alexander Williams
Install Django Rest Framework with Virtualenv
Django Rest Framework (DRF) is a powerful tool for building APIs in Django. To ensure a clean and isolated setup, using virtual environments is essential. This guide will walk you through the process step by step.
Table Of Contents
- Why Use Virtual Environments?
- Step 1: Install Python and Pip
- Step 2: Create a Virtual Environment
- Step 3: Activate the Virtual Environment
- Step 4: Install Django and Django Rest Framework
- Step 5: Create a Django Project
- Step 6: Configure Django Rest Framework
- Step 7: Run Migrations and Start the Server
- Conclusion
Why Use Virtual Environments?
Virtual environments help manage dependencies for different projects. They prevent conflicts between package versions. This is especially useful when working with Django Rest Framework.
Step 1: Install Python and Pip
Ensure Python and Pip are installed on your system. You can check this by running the following commands in your terminal:
python --version
pip --version
If not installed, download Python from the official website. Pip usually comes bundled with Python.
Step 2: Create a Virtual Environment
Navigate to your project directory and create a virtual environment. Use the following command:
python -m venv myenv
This creates a folder named myenv containing the virtual environment.
Step 3: Activate the Virtual Environment
Activate the virtual environment using the appropriate command for your operating system:
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate
Once activated, your terminal prompt will change to indicate the active environment.
Step 4: Install Django and Django Rest Framework
With the virtual environment active, install Django and Django Rest Framework using Pip:
pip install django djangorestframework
This installs the latest versions of Django and DRF in your virtual environment.
Step 5: Create a Django Project
Create a new Django project using the django-admin
command:
django-admin startproject myproject
This creates a new directory named myproject with the necessary files.
Step 6: Configure Django Rest Framework
Add Django Rest Framework to your project's settings. Open settings.py and add 'rest_framework'
to the INSTALLED_APPS
list:
INSTALLED_APPS = [
...
'rest_framework',
]
This enables DRF in your Django project.
Step 7: Run Migrations and Start the Server
Apply the initial database migrations and start the development server:
python manage.py migrate
python manage.py runserver
Your Django project is now running with Django Rest Framework installed.
Conclusion
Setting up Django Rest Framework with a virtual environment ensures a clean and isolated development environment. This guide covered the essential steps from creating a virtual environment to running your Django project. For more advanced usage, check out our guide on Setting Up Your First API with Django Rest Framework.
If you're interested in exploring more about DRF, you can also read about Django Rest Framework's APIView: Examples and Usage or Django Rest Framework ListCreateAPIView Example.