Skip to content

Chapter 1: First Look at Django

1.2 Django Environment Setup

Python Environment Requirements

Django 5.x has specific requirements for Python version. Recommended version: Python 3.12+, better performance and supports latest features. Current highest version is Python 3.12.10 (released April 18, 2025).

Check Python Version

bash
# Check Python version
python --version
# or
python -V

# Output example
Python 3.12.10

If your system Python version is too old, use these methods to install new version:

Windows users:

macOS users:

  • Use Homebrew: brew install python@3.12
  • Or download installer from official website

Linux users:

bash
# Ubuntu/Debian
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-pip

# CentOS/RHEL
sudo dnf install python3.12 python3.12-venv python3.12-pip

Virtual Environment Creation

Virtual environment creates separate Python environment for each project, avoids dependency conflicts.

Create Virtual Environment with venv

bash
# Create virtual environment
python -m venv django_env

# Windows activation
django_env\Scripts\activate

# macOS/Linux activation
source django_env/bin/activate

# After activation, command prompt shows virtual environment name
(django_env) $

Virtual Environment Management

bash
# View installed packages
pip list

# Upgrade pip to latest version
python -m pip install --upgrade pip

# Exit virtual environment
deactivate

# Delete virtual environment (delete folder directly)
rm -rf django_env  # Linux/macOS
rmdir /s django_env  # Windows

Use conda (Optional)

If you use Anaconda or Miniconda:

bash
# Create conda environment
conda create -n django_env python=3.12

# Activate environment
conda activate django_env

# Exit environment
conda deactivate

Django 5.x Installation

Make sure virtual environment is activated, then install Django.

Install Latest Version

bash
# Install latest Django version
pip install django

# Install specific version
pip install django=<version number>

# Verify installation
python -m django --version
# Output example: 5.0.1

Install Additional Dependencies

Depending on project needs, may need to install extra packages. Or install when needed. Better to accumulate some commonly used packages for batch installation based on your usage habits.

bash
# Database drivers
pip install psycopg2-binary  # PostgreSQL
pip install mysqlclient      # MySQL
pip install cx_Oracle        # Oracle

# Development tools
pip install django-debug-toolbar  # Debug toolbar
pip install django-extensions     # Django extensions

# Production environment
pip install gunicorn         # WSGI server
pip install whitenoise       # Static file service

Development Tool Recommendations

Code Editors/IDEs

  1. Visual Studio Code

    • Lightweight and powerful
    • Rich plugin ecosystem
    • Recommended plugins:
      • Python
      • Django
      • Python Docstring Generator
      • GitLens
  2. AI IDE In China, main large models are Deepseek, Tongyi Qianwen, etc.

  3. PyCharm Professional

    • Most comprehensive Django IDE
    • Built-in Django support
    • Powerful debugging features
    • Database integration

VSCode Django Development Configuration

Create .vscode/launch.json (debug configuration):

json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "justMyCode": true
        }
    ]
}

Database Management Tools

Strongly recommend free client software, can manage MySQL, PostgreSQL, Sqlite databases. https://www.heidisql.com/

Project Dependency Management

requirements.txt

Create requirements.txt file to manage project dependencies:

bash
# Generate dependency file
pip freeze > requirements.txt

# Install dependencies
pip install -r requirements.txt

Example requirements.txt:

txt
Django==5.0.1
psycopg2-binary==2.9.7
Pillow==10.0.1
django-debug-toolbar==4.2.0

Development and Production Environment Separation

Create different dependency files:

requirements/base.txt:

txt
Django==5.0.1
Pillow==10.0.1

requirements/dev.txt:

txt
-r base.txt
django-debug-toolbar==4.2.0
django-extensions==3.2.3
ipython==8.15.0

requirements/prod.txt:

txt
-r base.txt
gunicorn==21.2.0
whitenoise==6.5.0

Installation methods:

bash
# Development environment
pip install -r requirements/dev.txt

# Production environment
pip install -r requirements/prod.txt

Environment Verification

After entering virtual environment, verify installation:

python -V
> Python 3.12.10
python -m django --version
> 5.2.6

Common Problem Solutions

1. pip installation slow

bash
# Use domestic mirror source
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django

# Or configure permanent mirror source
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

2. Permission errors

bash
# macOS/Linux use user installation
pip install --user django

# Or use sudo (not recommended)
sudo pip install django

Summary

Now you have successfully set up Django development environment:

  1. ✅ Installed Python 3.12+
  2. ✅ Created virtual environment
  3. ✅ Installed Django 5.x
  4. ✅ Configured development tools
  5. ✅ Set up dependency management

Next Article

We will create first Django project, start actual Django development journey!
1.3 First Django Project →

Directory

Return to Course Directory

Released under the [BY-NC-ND License](https://creativecommons.org/licenses/by-nc-nd/4.0/deed.en).