Development Environment Setup
This chapter details how to set up a Django development environment, including Python installation, virtual environment configuration, Django installation, and the selection and configuration of development tools.
System Requirements
Operating System Support
Django supports the following operating systems:
- Windows 10/11
- macOS 10.15+
- Linux (Ubuntu, CentOS, Debian, etc.)
Python Version Requirements
Python Installation
Windows System
1. Download Python from the Official Website
Visit the Python website to download the latest version:
2. Install Python
3. Verify Installation
macOS System
1. Install Using Homebrew (Recommended)
2. Install from Official Website
Linux System
Ubuntu/Debian
CentOS/RHEL
Virtual Environment Configuration
Virtual environments are a best practice in Python development, allowing the creation of isolated Python environments for each project.
Why Use Virtual Environments
Creating Virtual Environments Using venv
1. Create a Virtual Environment
2. Activate the Virtual Environment
3. Deactivate the Virtual Environment
Creating Virtual Environments Using conda
If you use Anaconda or Miniconda:
Virtual Environment Best Practices
Django Installation
Install Django
Verify Django Installation
Install Additional Dependencies
Development Tool Selection
Code Editors/IDEs
1. Visual Studio Code (Recommended)
Recommended extensions:
2. PyCharm Professional
3. Sublime Text
Database Tools
1. SQLite Browser
2. pgAdmin (PostgreSQL)
3. MySQL Workbench
Version Control
Git Configuration
Project Structure Best Practices
Recommended Project Structure
Environment Variable Configuration
1. Install python-decouple
2. Create .env file
3. Use in settings.py
Development Tool Configuration
Django Debug Toolbar
Django Extensions
Code Quality Tools
1. Black (Code Formatter)
2. Flake8 (Code Linter)
3. isort (Import Sorter)
Pre-commit Hooks
Verify Environment Configuration
Create a Test Project
Environment Checklist
Common Issue Resolution
1. Python Command Not Recognized
2. pip Installation Fails
3. Virtual Environment Activation Fails
4. Django Import Error
Chapter Summary
This chapter detailed the setup of a Django development environment:
Key Points:
- Python Installation: Choose the appropriate Python version
- Virtual Environment: Create isolated environments for each project
- Django Installation: Install Django using pip
- Development Tools: Select appropriate IDEs and extensions
- Project Structure: Organize code following best practices
Best Practices:
- Use virtual environments to isolate project dependencies
- Configure environment variables to manage sensitive information
- Use code quality tools to ensure code standards
- Set up pre-commit hooks for automatic code checking
- Create detailed requirements.txt
Development Toolchain:
- VS Code + Python extension
- Django Debug Toolbar
- Black + Flake8 + isort
- Git version control
- Virtual environment management
In the next chapter, we will create our first Django project and learn about the basic project structure and configuration of Django.