Skip to content

Django - Environment

Django - Setting Up Your Development Environment

Section titled “Django - Setting Up Your Development Environment”

Setting up a proper development environment is the crucial first step to building Django applications. This involves installing Python, setting up a project environment using venv, installing Django itself, and potentially a database system.

Django is a Python framework, so you need Python installed. Modern versions of Django require Python 3. Check the official Django documentation for the specific Python 3.x versions supported by the Django version you intend to use (e.g., Django 4.x typically requires Python 3.8+).

Most Linux distributions and macOS versions come with Python 3 pre-installed. Windows users will likely need to install it.

Open your terminal or command prompt and run:

python --version
# or sometimes python3 --version

If Python 3 is installed, you’ll see its version number. If not, or if you need a specific version, download it from the official Python website:

Important: During installation on Windows, ensure you check the box that says “Add Python to PATH”.

Step 2 – Setting Up a Virtual Environment (venv)

Section titled “Step 2 – Setting Up a Virtual Environment (venv)”

It is highly recommended to use virtual environments for your Python projects. A virtual environment isolates the project’s dependencies (like Django and other libraries) from your global Python installation and other projects.

Navigate to the directory where you want to create your project, then run:

# Linux/macOS
python3 -m venv venv
# Windows
python -m venv venv

This creates a directory named venv containing a copy of the Python interpreter and a place to install project-specific libraries.

Before installing packages or running your project, you must activate the environment:

# Linux/macOS
source venv/bin/activate
# Windows (Command Prompt)
.\venv\Scripts\activate.bat
# Windows (PowerShell)
.\venv\Scripts\Activate.ps1
# Windows (Git Bash)
source venv/Scripts/activate

Your command prompt should change to indicate the active environment (e.g., (venv) your-user@machine:...$). To deactivate, simply type deactivate.

With your virtual environment activated, install Django using pip, Python’s package installer (which is included in venv):

pip install django

This command downloads and installs the latest stable version of Django and its dependencies within your active virtual environment.

To install a specific version:

pip install django==4.2.* # Installs the latest 4.2.x version

Run the following command:

django-admin --version

If Django is installed correctly within your active environment, this will print its version number.

Django requires a database. By default, it’s configured to use SQLite 3, which is stored as a single file in your project directory. SQLite is excellent for development and small applications because it requires no separate server setup.

For larger or production applications, Django officially supports several major database engines:

  • PostgreSQL: Often recommended for robust Django deployments.
  • MySQL / MariaDB: Very popular open-source databases.
  • Oracle: A powerful commercial database.
  • SQLite: Built-in, file-based, great for development/testing.

To use databases other than SQLite, you’ll need to:

  1. Install the database server itself.
  2. Install the appropriate Python database adapter library within your virtual environment (e.g., pip install psycopg2-binary for PostgreSQL, pip install mysqlclient for MySQL).
  3. Configure the DATABASES setting in your Django project’s settings.py file with the connection details (engine, name, user, password, host, port).

Support for NoSQL databases (like MongoDB) exists through third-party libraries (e.g., Djongo), but they are not officially part of the core Django framework.

Django includes a lightweight web server specifically for development and testing. It automatically reloads your Python code when changes are detected, making development faster.

You run it from your project’s base directory (where manage.py is located) with:

python manage.py runserver

This starts the server, typically on http://127.0.0.1:8000/.

Remember: This server is only for development. For production deployment, you’ll need a production-grade setup like Nginx or Apache with a WSGI server (Gunicorn, uWSGI, mod_wsgi), as discussed in the deployment chapter.