python-3.xpipdistribute

Cannot install python3 distribute package: no attribute SourceFileLoader


I was following the django tutorial to write reusable apps. I tried installing distribute with pip install distribute and got this error:

(djangoenv) user@user:~/learndjango$ pip install distribute
Collecting distribute
  Using cached distribute-0.7.3.zip (145 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 14, in <module>
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/setuptools/__init__.py", line 2, in <module>
          from setuptools.extension import Extension, Library
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/setuptools/extension.py", line 5, in <module>
          from setuptools.dist import _get_unpatched
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/setuptools/dist.py", line 7, in <module>
          from setuptools.command.install import install
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/setuptools/command/__init__.py", line 8, in <module>
          from setuptools.command import install_scripts
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/setuptools/command/install_scripts.py", line 3, in <module>
          from pkg_resources import Distribution, PathMetadata, ensure_directory
        File "/tmp/pip-install-axthdilv/distribute_6a5e6b0f39f74664a39e141cbad8edce/pkg_resources.py", line 1518, in <module>
          register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
      AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

Here are the versions of pip and other possibly related packages:

(djangoenv) user@user:~/learndjango$ python3 --version
Python 3.10.12

(djangoenv) user@user:~/learndjango$ pip --version
pip 24.2 from /home/user/learndjango/djangoenv/lib/python3.10/site-packages/pip (python 3.10)

(djangoenv) user@user:~/learndjango$ pip show setuptools
Name: setuptools
Version: 72.1.0
...
(djangoenv) user@user:~/learndjango$ pip show wheel
Name: wheel
Version: 0.44.0
...

Here's a pip freeze:

(djangoenv) user@user:~/learndjango$ pip freeze
asgiref==3.8.1
coverage==7.6.1
Django==5.1
django-debug-toolbar==4.4.6
python-dotenv==1.0.1
sqlparse==0.5.1
typing_extensions==4.12.2

I found a few similar questions here on stackoverflow:

  1. using pip3: module "importlib._bootstrap" has no attribute "SourceFileLoader"
  2. 'importlib._bootstrap' has no attribute 'SourceLoader'
  3. Unable to install distribute with python 3.2

Here are my attempts based on those questions:

pip install works for other packages except distribute.


Solution

  • You are using a modern version of Python to install a package that is no longer maintained. setuptools is now the recommended packaging tool and was merged with the distribute fork. The latest version of distribute was released in 2013 supporting Python 2.4 - 2.7 and Python 3.1 - 3.3. You are using Python 3.10.12.

    A little bit of history:

    The Distribute is a package that was used to build and distribute Python packages. It was developed as a fork of the setuptools package and provided many of the same features, with the goal of improving and extending the functionality of setuptools. However, Distribute was eventually merged back into the setuptools project, and is no longer maintained as a separate package. If you come across a reference to Distribute, it is most likely an outdated reference that should be replaced with setuptools. setuptools is now the recommended way to build and distribute Python packages.

    See https://www.geeksforgeeks.org/differences-between-distribute-distutils-setuptools-in-python/