Step-by-Step Guide to Setting Up a Django Environment

Django is a powerful web framework for building robust web applications with Python. This guide walks you through setting up a Django environment from scratch, covering Python installation, virtual environments, and creating a Django project and app.

Step 1: Confirm Python and pip Installation

Django requires Python (3.6+ recommended) and pip (Python’s package manager). Before proceeding, confirm that both are installed.

Windows user:

Open Command Prompt cmd or PowerShell and run:

python --version
## or 
python3 --version
## use this to check for pip
pip --version

Mac/Linux user:

Open Terminal and run:

python3 --version
pip3 --version

Common Errors & Fixes:

  • If Python or pip is not recognized, install Python from python.org.

  • After installation, restart the terminal or add Python to the system PATH.

Step 2: Create a Virtual Environment

A virtual environment isolates project dependencies.

Step 2.1: Create a folder for your project and navigate to that folder in your command line or Bash

Step 2.2: While in the project folder directory, type in the following command to create a virtual environment for your project

Windows user:

python -m venv environmentname

Note: The environment name is totally up to you to pick

Mac/Linux user:

python3 -m venv myenv

Step 2.3: Next, you have to activate your virtual environment using the following command.

To activate the virtual environment:

Windows user:

 source environmentname\Scripts\activate

Mac/Linux user:

source myenv/bin/activate

After activation, you should see (environmentname) at the beginning of your terminal prompt.

Common Errors & Fixes:

  • On macOS, if you get a permission error, try: sudo chmod -R 777 myenv

  • If activation fails on Windows PowerShell, enable execution policies: Set-ExecutionPolicy Unrestricted -Scope Process

Step 3: Install Django

With the virtual environment activated, it’s time to install Django.

pip install django

To verify installation, use the following command:

django-admin --version

Common Errors & Fixes:

  • If django-admin is not found, ensure your virtual environment is activated.

  • If installation is slow, try:

      pip install --upgrade pip
    

Step 4: Creating a Django Project

To create your first Django project, run the following command while still in the project folder directory:

django-admin startproject projectname

This should create a new folder with your chosen project name.

Move into the project directory:

cd projectname

Run the server:

python manage.py runserver

A response like this should be gotten.

Visit http://127.0.0.1:8000/ in a browser. You should see the Django welcome page.

Common Errors & Fixes:

  • If python manage.py runserver fails with ModuleNotFoundError: No module named 'django', ensure your virtual environment is activated.

Conclusion

You've successfully set up a Django environment and created a project. The next stop for you is learning how to add an app and build a basic route. Yayy!!!

0
Subscribe to my newsletter

Read articles from Olayemi Mistura Suleiman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Olayemi Mistura Suleiman
Olayemi Mistura Suleiman