You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Django网站到Heroku时出现python setup.py egg_info执行失败错误

Django部署Heroku遭遇依赖安装错误排查

问题背景

用Django开发的网站,本地虚拟环境依赖齐全,但部署到Heroku时触发python setup.py egg_info执行失败,尝试--use-pep517参数无效,错误核心为AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader',关联包为distribute和dotenv。

完整错误日志

× python setup.py egg_info did not run successfully.
         │ exit code: 1
         ╰─> [83 lines of output]
             /app/.heroku/python/lib/python3.11/site-packages/setuptools/__init__.py:84: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
             !!
             
                     ********************************************************************************
                     Requirements should be satisfied by a PEP 517 installer.
                     If you are using pip, you can try `pip install --use-pep517`.

!!
               dist.fetch_build_eggs(dist.setup_requires)
               error: subprocess-exited-with-error
             
               × python setup.py egg_info did not run successfully.
               │ exit code: 1
               ╰─> [17 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-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/setuptools/__init__.py", line 2, in <module>
                       from setuptools.extension import Extension, Library
                     File "/tmp/pip-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/setuptools/extension.py", line 5, in <module>
                       from setuptools.dist import _get_unpatched
                     File "/tmp/pip-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/setuptools/dist.py", line 7, in <module>
                       from setuptools.command.install import install
                     File "/tmp/pip-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/setuptools/command/__init__.py", line 8, in <module>
                       from setuptools.command import install_scripts
                     File "/tmp/pip-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/setuptools/command/install_scripts.py", line 3, in <module>
                       from pkg_resources import Distribution, PathMetadata, ensure_directory
                     File "/tmp/pip-wheel-2xd7j5kf/distribute_1efc1e9757504aed986ff8beb549bcab/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.
             Traceback (most recent call last):
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/installer.py", line 96, in _fetch_build_egg_no_warn
                 subprocess.check_call(cmd)
               File "/app/.heroku/python/lib/python3.11/subprocess.py", line 413, in check_call
                 raise CalledProcessError(retcode, cmd)
             subprocess.CalledProcessError: Command '['/app/.heroku/python/bin/python', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpaaqkndf4', '--quiet', 'distribute']' returned non-zero exit status 1.
             
             The above exception was the direct cause of the following exception:
             
             Traceback (most recent call last):
               File "<string>", line 2, in <module>
               File "<pip-setuptools-caller>", line 34, in <module>
               File "/tmp/pip-install-3p1f5saj/dotenv_646ba309b67d4732bcfce4160ecc8efb/setup.py", line 13, in <module>
                 setup(name='dotenv',
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/__init__.py", line 106, in setup
                 _install_setup_requires(attrs)
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/__init__.py", line 79, in _install_setup_requires
                 _fetch_build_eggs(dist)
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/__init__.py", line 84, in _fetch_build_eggs
                 dist.fetch_build_eggs(dist.setup_requires)
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/dist.py", line 907, in fetch_build_eggs
                 return _fetch_build_eggs(self, requires)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/installer.py", line 38, in _fetch_build_eggs
                 resolved_dists = pkg_resources.working_set.resolve(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/pkg_resources/__init__.py", line 829, in resolve
                 dist = self._resolve_dist(
                        ^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/pkg_resources/__init__.py", line 865, in _resolve_dist
                 dist = best[req.key] = env.best_match(
                                        ^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/pkg_resources/__init__.py", line 1135, in best_match
                 return self.obtain(req, installer)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/pkg_resources/__init__.py", line 1147, in obtain
                 return installer(requirement)
                        ^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.11/site-packages/setuptools/installer.py", line 98, in _fetch_build_egg_no_warn
                 raise DistutilsError(str(e)) from e
             distutils.errors.DistutilsError: Command '['/app/.heroku/python/bin/python', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpaaqkndf4', '--quiet', 'distribute']' returned non-zero exit status 1.
             [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.

问题根源与解决方案

根源解析

错误链条:

  1. Heroku使用Python 3.11,而旧版distribute包(已被合并到setuptools的废弃项目)与Python 3.11不兼容,触发importlib._bootstrap.SourceFileLoader属性缺失错误。
  2. 依赖中可能直接或间接依赖了distribute,或者使用了过时的dotenv包(非维护中的python-dotenv),后者的安装脚本依赖废弃的distribute。

修复步骤

  • 替换过时的dotenv包:如果依赖里写的是dotenv,替换为维护中的python-dotenv,执行本地命令更新:
    pip uninstall dotenv
    pip install python-dotenv
    pip freeze > requirements.txt
    
  • 移除distribute依赖:检查requirements.txt,若存在distribute条目直接删除,该包已被setuptools取代,无需单独安装。
  • 锁定兼容的setuptools版本:在requirements.txt中添加一行,确保使用适配Python 3.11的版本:
    setuptools>=65.5.0
    
  • 重新部署:提交修改后的依赖文件到Git,推送到Heroku:
    git add requirements.txt
    git commit -m "Fix dependency issues for Heroku deployment"
    git push heroku main
    

内容的提问来源于stack exchange,提问作者Youran Roh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:27:30