KeyError: ‘__reduce_cython__’ when trying to import sklearn package into Django app

When attempting to run my Django app with python manage.py runserver, I am getting the following error relating to a scikit-learn module import:

File "sklearn\metrics\_pairwise_distances_reduction\_argkmin.pyx", line 1, in init sklearn.metrics._pairwise_distances_reduction._argkmin
KeyError: '__reduce_cython__'

This is the last line in a larger error message, listed below. I have tried everything I’ve found online, including:

  • upgrading all packages
  • uninstalling and reinstalling related packages
  • checking package dependencies are satisfied
  • using pipdeptree to ensure package versions aren’t conflicting
  • restarting vscode, laptop etc.

Despite this, every time I run python manage.py runserver I get the same error appearing.

I’m using pip to install packages. Here are my current package versions in this venv:

asgiref 3.7.2
Cython 3.0.10
Django 5.0.5
django-cors-headers 4.3.1
django-environ 0.11.2
djangorestframework 3.14.0
joblib 1.4.2
numpy 1.26.4
packaging 24.0
pandas 2.2.1
pip 24.0
pipdeptree 2.19.1
psycopg 3.1.18
psycopg2-binary 2.9.9
python-dateutil 2.9.0.post0
pytz 2024.1
scikit-learn 1.4.2
scipy 1.13.0
six 1.16.0
sqlparse 0.4.4
stringcase 1.2.0
threadpoolctl 3.5.0
typing_extensions 4.9.0
tzdata 2024.1

Interestingly from sklearn.feature_extraction.text import TfidfVectorizer causes no issues, but from sklearn.metrics.pairwise import cosine_distances is the line causing the issue.

Please help! Have been stuck on this for a few days now.

Full error message:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:userpathAppDataLocalProgramsPythonPython312Libthreading.py", line 1073, in _bootstrap_inner
    self.run()
  File "C:userpathAppDataLocalProgramsPythonPython312Libthreading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "C:userpathvenvLibsite-packagesdjangoutilsautoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:userpathvenvLibsite-packagesdjangocoremanagementcommandsrunserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "C:userpathvenvLibsite-packagesdjangocoremanagementbase.py", line 486, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangocorechecksregistry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangocorechecksurls.py", line 14, in check_url_config
    return check_resolver(resolver)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangocorechecksurls.py", line 24, in check_resolver
    return check_method()
           ^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangourlsresolvers.py", line 519, in check
    for pattern in self.url_patterns:
                   ^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangoutilsfunctional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangourlsresolvers.py", line 738, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
                       ^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangoutilsfunctional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangourlsresolvers.py", line 731, in urlconf_module
    return import_module(self.urlconf_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:userpathAppDataLocalProgramsPythonPython312Libimportlib__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:userpathurls.py", line 21, in <module>
    path("page/", include("shopping_list.urls")),
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:userpathvenvLibsite-packagesdjangourlsconf.py", line 39, in include
    urlconf_module = import_module(urlconf_module)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:userpathAppDataLocalProgramsPythonPython312Libimportlib__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:userpathurls.py", line 3, in <module>
    from .views import my_api
  File "C:userpathmy_api.py", line 8, in <module>
    from sklearn.metrics.pairwise import cosine_distances
  File "C:userpathvenvLibsite-packagessklearnmetrics__init__.py", line 7, in <module>
    from . import cluster
  File "C:userpathvenvLibsite-packagessklearnmetricscluster__init__.py", line 25, in <module>
    from ._unsupervised import (
  File "C:userpathvenvLibsite-packagessklearnmetricscluster_unsupervised.py", line 22, in <module>
    from ..pairwise import _VALID_METRICS, pairwise_distances, pairwise_distances_chunked
  File "C:userpathvenvLibsite-packagessklearnmetricspairwise.py", line 43, in <module>
    from ._pairwise_distances_reduction import ArgKmin
  File "C:userpathvenvLibsite-packagessklearnmetrics_pairwise_distances_reduction__init__.py", line 94, in <module>
    from ._dispatcher import (
  File "C:userpathvenvLibsite-packagessklearnmetrics_pairwise_distances_reduction_dispatcher.py", line 13, in <module>
    from ._argkmin import (
  File "sklearn\metrics\_pairwise_distances_reduction\_argkmin.pyx", line 1, in init sklearn.metrics._pairwise_distances_reduction._argkmin
KeyError: '__reduce_cython__'

New contributor

Alex Leigh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật