Skip to main content
Django Basics Tutorial
CHAPTER 02 Beginner

Setting Up Python and Django Environment

Updated: May 14, 2026
20 min read

# CHAPTER 2

Setting Up Python and Django Environment

1. Introduction

Before we can build a Django application, we must set up our digital workshop. Because Django is a Python framework, you must have Python installed on your computer. Furthermore, Python relies heavily on Virtual Environments to keep projects organized and prevent conflicting software versions. In this chapter, we will install Python, configure a virtual environment, and use pip to install the Django framework.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Install Python and verify it using the command line.
  • Understand the purpose of a Virtual Environment.
  • Create and activate a Python Virtual Environment (venv).
  • Install Django using the pip package manager.

3. Beginner-Friendly Explanation

Imagine you are a professional chef who cooks for two different restaurants. Restaurant A requires a strictly vegan kitchen. Restaurant B requires a kitchen that only cooks barbecue. If you try to cook both menus in the same kitchen at the same time, the ingredients will mix, cross-contaminate, and ruin the food. Virtual Environments are separate, isolated kitchens. When you work on Project A, you walk into Kitchen A. It has its own tools and its own ingredients (Django version 3). When you work on Project B, you walk into Kitchen B (Django version 5). Virtual environments ensure your Python projects never contaminate each other.

4. Step 1: Installing Python

  1. 1. Visit the official Python website: python.org/downloads.
  1. 2. Download the latest stable version for your Operating System.
  1. 3. CRITICAL FOR WINDOWS USERS: During the installation wizard, you MUST check the box that says "Add Python to PATH" before clicking Install. If you miss this, your terminal will not recognize Python commands.

Verify Installation: Open your terminal (Command Prompt/PowerShell for Windows, Terminal for Mac/Linux) and type:

bash
1
python --version

*(If you are on a Mac, you may need to type python3 --version).*

5. Step 2: Creating a Virtual Environment

Navigate to the location on your computer where you want to store your code.
bash
123456
# Create a new directory for your project
mkdir my_django_learning
cd my_django_learning

# Create the virtual environment (We will name the environment 'env')
python -m venv env

*(Note for Mac/Linux users: Use python3 -m venv env)*

This command creates a new folder named env inside your project directory. This folder contains a fresh, isolated copy of Python.

6. Step 3: Activating the Virtual Environment

Before you install Django, you must "walk into the kitchen" (activate the environment).

For Windows:

bash
1
env\Scripts\activate

For Mac/Linux:

bash
1
source env/bin/activate

*Success Indicator:* You will know it worked because your terminal prompt will change. It will now have (env) written at the very beginning of the line!

7. Step 4: Installing Django

Now that we are safely inside our isolated environment, we will use pip (Python's official package installer) to download Django from the internet.
bash
12
# Ensure your virtual environment is active first!
pip install django

To verify that Django installed correctly, run:

bash
1
python -m django --version

8. Backend Workflow: Leaving the Environment

When you are done working for the day, you should "leave the kitchen." To deactivate the virtual environment and return to your computer's global Python settings, simply type:
bash
1
deactivate

*(The (env) prefix will disappear from your terminal).*

9. Best Practices

  • Never Upload the Environment: If you are using Git and GitHub, you MUST create a .gitignore file and add env/ to it. The virtual environment folder contains thousands of system files. You only upload your actual code, not the environment. Other developers will create their own environments on their own computers.

10. Common Mistakes

  • Installing Django Globally: Beginners often forget to activate their virtual environment before running pip install django. This installs Django "globally" on your main computer. Later, if you try to build an older project that requires an older version of Django, the global installation will cause massive conflicts. Always check for the (env) tag in your terminal before running pip.

11. Exercises

  1. 1. Explain the purpose of pip. What is it, and what is its equivalent in the Node.js ecosystem?

12. Coding Challenges

  • Challenge: Open your terminal. Deactivate your current environment if it is active. Create a brand new folder called test_project. Inside it, create a new virtual environment called venv. Activate it.

13. MCQs with Answers

Question 1

What is the primary purpose of a Python Virtual Environment (venv)?

Question 2

Which tool is the standard package manager used to install third-party libraries (like Django) in Python?

14. Interview Questions

  • Q: Explain what happens if you forget to activate your virtual environment before running pip install django. Why is this considered bad practice in professional Python development?
  • Q: What does the "Add Python to PATH" checkbox actually do during the Windows installation process?

15. FAQs

Q: I get an error saying python is not recognized as an internal or external command. What do I do? A: This means you forgot to check "Add Python to PATH" during installation. You must uninstall Python, re-run the installer, and ensure that box at the very bottom of the first screen is checked.

16. Summary

In Chapter 2, we transformed our computer into a professional Python development machine. We installed the Python runtime, learned the critical importance of isolating our dependencies using Virtual Environments (venv), and successfully activated our environment. Finally, we utilized Python's package manager (pip) to download and install the Django framework securely within our isolated workspace.

17. Next Chapter Recommendation

Before we generate our project files, we need to understand how Django thinks. Proceed to Chapter 3: Understanding Django Architecture.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·