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__'
Alex Leigh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.