How to Install Python Django Framework: Step-by-Step Guide
Django is a powerful and popular Python web framework that enables developers to create robust, scalable web applications quickly and efficiently. This tutorial will guide you through the process of installing Django on your system and setting up a basic development environment so you can start building your web projects.
Prerequisites
- Python Installed: Ensure you have Python installed on your computer. Django requires Python 3.6 or higher. You can download Python from the official Python website.
- Basic Command Line Knowledge: Familiarity with command line or terminal usage will be helpful.
- Internet Connection: Required to download Django and its dependencies using pip.
Step 1: Verify Python Installation
Open your terminal or command prompt and run:
python3 --version
or
python --version
If Python is installed, you will see the Python version number. If not, install it first.
Step 2: Install pip (if not installed)
pip is the package installer for Python and usually comes bundled with recent Python versions. Verify by running:
pip --version
If pip is missing, install it from the official pip installation guide.
Step 3: Set Up a Virtual Environment (Recommended)
Creating a virtual environment isolates your Django project dependencies. Run these commands:
python3 -m venv myenv
source myenv/bin/activate # On Windows: myenv\Scripts\activate
Your terminal should now indicate the virtual environment is active.
Step 4: Install Django
With your virtual environment activated (or globally if you prefer), install Django by running:
pip install django
This installs the latest stable Django version.
Step 5: Verify Django Installation
Check the installed Django version:
python -m django --version
If this prints the version number, Django is installed successfully.
Step 6: Create a New Django Project
Start creating your first project with:
django-admin startproject myproject
This creates a directory named myproject containing the initial project files.
Step 7: Run the Development Server
Navigate into your project folder and run the development server:
cd myproject
python manage.py runserver
Open http://127.0.0.1:8000/ in your web browser. You should see the Django welcome page.
Troubleshooting
- Command not found errors: Make sure you activated your virtual environment or that your PATH variables are correctly set.
- Permission issues: Avoid installing Django with sudo or administrator rights globally; use a virtual environment.
- Port in use error: If port 8000 is busy, run
python manage.py runserver 8080or another port number.
Summary Checklist
- Installed Python 3.6+
- Set up virtual environment
- Installed Django via pip
- Created Django project
- Ran local development server
For advanced setup and features, explore more tutorials on TalkEcho such as <a href="/
