Fix No module named ‘django_bootstrap5’ ModuleNotFoundError in Python Django

Share your love

When working with Bootstrap in your Django project, you can easily style your web pages to achieve the most visually appealing web applications. In order to create aesthetically appealing webpages, you need to blend your Django application with the latest Bootstrap 5 CSS framework.

However, if you do not set up your environment correctly or have a misconfiguration in your settings, you may end up having a pesky bug that needs to be fixed; “ModuleNotFoundError: No module named ‘django_bootstrap5′”

The “No module named ‘django_bootstrap5′” exception happens for a couple of reasons:

  1. Not installing django-bootstrap5 package for loading bootstrap files in your active Python virtual environment
  2. Missing to activate the correct Python virtual environment
  3. Installing the wrong package that does not use ‘django_bootstrap5’ app name in the INSTALLED_APPS setting

Generally, the ModuleNotFoundError is a polite way of Django telling you that it cannot locate the package you wish to load Bootstrap 5 in your Django project.

Let’s see a couple of ways to fix “No module named ‘django_bootstrap5′” exception in Python.

Ways to fix ModuleNotFoundError: No module named ‘django_bootstrap5’

Install django-bootstrap5 package in your active virtual environment

If you are getting “No module named ‘django_bootstrap5′” whenever you run your Django development server, most likely, Django cannot find the package installed in your active virtual environment. To fix the exception, you need to install django-bootstrap5 package, which is a package that loads Bootstrap 5 CSS and JavaScript files in your project.

Here are the steps to take to install the “django-bootstrap5” PyPi package in your active virtual environment:

Step 1: Open the Terminal and navigate to your Django project’s root folder

The root directory of your Django project is the folder that has the manage.py file.

cd path-to-project-folder

Step 2: Use the command pip install django-bootstrap5 to install the package

By running, pip install django-bootstrap5 command, you will download the django-bootstrap5 package with its dependencies into your Python virtual environment. With that, the Django version running in the active environment will be able to locate the package and fix module not found error.

pip install django-bootstrap5

Step 3: Ensure you have bootstrap5 app in your list of INSTALLED_APPS

In your settings.py file, add the line django_bootstrap5 to the list, INSTALLED_APPS to allow Django to recognize django-bootstrap5 package as one of the apps used in your project.

How to fix no module named 'django_bootstrap5' in Python Django

Step 4: Add {% load django_bootstrap5 %} in your base.html and start using Bootstrap 5 classes

Now that you have fixed the ModuleNotFoundError, add the following lines of code inside the <head> tag in your base.html file:

    {% load django_bootstrap5 %}

    {% bootstrap_css %}

    {% bootstrap_javascript %}

    {% bootstrap_messages %}

With that, you can start using bootstrap 5 classes and components in your Django templates. Here’s an example container using the Bootstrap 5 class:

In base.html

    <div class="container-fluid">
        {% block content %}
    
        {% endblock content %}
    </div>

In child template, e.g contact.html:

{% extends 'base.html' %}
{% load django_bootstrap5 %}

{% block content %}
    <div class="row my-5">
        <div class="col-md-12">
            <form action="/contact/" method="post" class="form">
                {% csrf_token %}
                {% bootstrap_form form %}
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </div>
{% endblock %}

That’s it. Your Django development server and web pages should run and be styled respectively.

If the error still persists, probably check these other approaches to fix the exception.

Check that you have activated the correct Python virtual environment

If you have django-bootstrap5 package installed in your virtual environment, check that you have the correct Python virtual environment active. If you have activated a different virtual environment that does not have django-bootstrap5 package installed, then the ModuleNotFoundError: No module named ‘django_bootstrap5’ should persist.

To fix that, activate the correct Python virtual environment that has django-bootstrap5 PyPi package installed.

On Mac/Linux use the following command to activate the correct virtual environment:

source path-to-env/bin/activate

On Windows, use the command:

path-to-env\Scripts\activate.bat

After activating the virtual environment, you will notice the name of the virtual environment will appear at the beginning of the prompt.

Check that the active virtual environment has django-bootstap5 installed using the following command:

pip show django-bootstrap5

The command should display package information such as author and installation location if it exists.

If you get: “WARNING: Package(s) not found: django-bootstrap5” warning, then you should probably install the package if that is the correct package.

pip install django-bootstrap5

Install the correct package for loading Bootstrap 5 in Django

The correct package names for loading Bootstrap 5 into your Django project are django-bootstrap-v5 and django-bootstrap5. So, make sure you’re installing the correct package that uses ‘django-boostrap5’ app name in the list of INSTALLED_APPS.

The correct package that uses ‘django_bootstrap5’ app name is the django-boostrap5 Pypi package.

Therefore, ensure you have django-boostrap5 package installed in your Python virtual environment.

pip install django-bootstrap5

Find more information on django-bootstrap-v5 vs django-bootstrap5 by following the link to a page I have outlined their differences and how to avoid confusion between the two.

How to install Bootstrap 5 in Django using django-bootstrap5 package?

The best way to use Bootstrap 5 in your Django projects is to use the django-bootstrap5 package which saves you time, effort, and complexities of including Django in your project and leveraging its features.

For a step-by-step guide on how to blend your Django project with Bootstrap 5 the right way, check out the article I have written on how to use Bootstrap 5 with your Django project the right way.

Share your love
Badi
Badi

Badi here, creator of ngangasn.com— A website dedicated to providing helpful information and how-to's of web development and hosting. Inspired by a fascination to write the most efficient code to make a computer laugh, & humans, Steve has a passion for organizing characters to create code and informative content.
What makes me happy?
Well, nothing like the feeling of finally figuring out that one pesky bug that's been driving me crazy.

Leave a Reply

Your email address will not be published. Required fields are marked *