Reza Abedini
Reza Abedini

Reputation: 1

django.template.library.InvalidTemplateLibrary

im tried to install django-jalali-date module on Django5.0 . But I encountered the following error:

django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'jalali_date.templatetags.jalali_tags': No module named 'distutils'

i am tried install this module for convert Gregorian calendar to solar calendar. I installed it with pip install django-jalali-date and put it in the installed apps

Upvotes: 0

Views: 367

Answers (1)

sindo mim
sindo mim

Reputation: 11

The error you're encountering is due to the absence of the distutils module, which has been deprecated in Python 3.10 and removed in Python 3.12. The django-jalali-date package you're using relies on this module, leading to the error in Django 5.0.0.

To resolve this issue, you have a few options:

  1. Use a Python version that includes distutils (<= Python 3.9): If you can, use Python 3.9, which still includes distutils. This can be a quick fix if you do not have a strict requirement to use Python 3.12.

  2. Modify the django-jalali-date package to remove the dependency on distutils: You can manually modify the jalali_tags.py file in the django-jalali-date package to remove or replace the usage of distutils.version.StrictVersion. Here’s how you can do it:

    • Locate the jalali_tags.py file within your environment. It should be at a path similar to E:\Projects\Django\someproject\env\Lib\site-packages\jalali_date\templatetags\jalali_tags.py.

    • Open the file and find the line that imports StrictVersion:

      from distutils.version import StrictVersion
      
    • Replace the import and usage of StrictVersion with an alternative, such as packaging.version:

      from packaging.version import Version
      
      # Replace any usage of StrictVersion with Version
      # For example:
      version = Version("1.0.0")
      

    To use packaging, you might need to install it if it’s not already installed:

    pip install packaging
    
  3. Use a different package or wait for an update: If the above solutions are not feasible, consider using a different package for handling Jalali dates or check if an updated version of django-jalali-date becomes available that supports Python 3.12.

Here is an example of how you might update the jalali_tags.py file:

# Original code in jalali_tags.py
# from distutils.version import StrictVersion

# Updated code
from packaging.version import Version as StrictVersion

# The rest of your code remains unchanged

After making these changes, try running your Django project again. This should resolve the ModuleNotFoundError for distutils.

Upvotes: 0

Related Questions