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

Flask应用本地正常,Heroku部署失败报pkgutil.ImpImporter错误

Flask应用本地正常但Heroku部署失败:AttributeError: module 'pkgutil' has no attribute 'ImpImporter'

我的Flask应用在本地运行正常,但部署到Heroku时编译失败,报错AttributeError: module 'pkgutil' has no attribute 'ImpImporter'。我试过参考相关问题,手动给requirements.txt添加python==3.12.1,但问题没解决。以下是相关文件和部署日志:

部署日志

-----> Building on the Heroku-22 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.12.1
       To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.12.1
-----> Installing pip 23.3.2, setuptools 68.2.2 and wheel 0.42.0
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting asgiref==3.7.2 (from -r requirements.txt (line 1))
         Downloading asgiref-3.7.2-py3-none-any.whl.metadata (9.2 kB)
       Collecting blinker==1.7.0 (from -r requirements.txt (line 2))
         Downloading blinker-1.7.0-py3-none-any.whl.metadata (1.9 kB)
       Collecting certifi==2022.12.7 (from -r requirements.txt (line 3))
         Downloading certifi-2022.12.7-py3-none-any.whl (155 kB)
       Collecting chardet==5.2.0 (from -r requirements.txt (line 4))
         Downloading chardet-5.2.0-py3-none-any.whl.metadata (3.4 kB)
       Collecting charset-normalizer==2.1.1 (from -r requirements.txt (line 5))
         Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
       Collecting click==8.1.7 (from -r requirements.txt (line 6))
         Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB)
       Collecting colorama==0.4.6 (from -r requirements.txt (line 7))
         Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
       Collecting Django==5.0.1 (from -r requirements.txt (line 8))
         Downloading Django-5.0.1-py3-none-any.whl.metadata (4.2 kB)
       Collecting filelock==3.9.0 (from -r requirements.txt (line 9))
         Downloading filelock-3.9.0-py3-none-any.whl (9.7 kB)
       Collecting Flask==3.0.1 (from -r requirements.txt (line 10))
         Downloading flask-3.0.1-py3-none-any.whl.metadata (3.6 kB)
       Collecting fsspec==2023.4.0 (from -r requirements.txt (line 11))
         Downloading fsspec-2023.4.0-py3-none-any.whl (153 kB)
       Collecting gunicorn==21.2.0 (from -r requirements.txt (line 12))
         Downloading gunicorn-21.2.0-py3-none-any.whl.metadata (4.1 kB)
       Collecting idna==3.4 (from -r requirements.txt (line 13))
         Downloading idna-3.4-py3-none-any.whl (61 kB)
       Collecting itsdangerous==2.1.2 (from -r requirements.txt (line 14))
         Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
       Collecting Jinja2==3.1.2 (from -r requirements.txt (line 15))
         Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
       Collecting MarkupSafe==2.1.3 (from -r requirements.txt (line 16))
         Downloading MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (2.9 kB)
       Collecting mpmath==1.3.0 (from -r requirements.txt (line 17))
         Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)
       Collecting networkx==3.0 (from -r requirements.txt (line 18))
         Downloading networkx-3.0-py3-none-any.whl (2.0 MB)
       Collecting numpy==1.24.1 (from -r requirements.txt (line 19))
         Downloading numpy-1.24.1.tar.gz (10.9 MB)
         Installing build dependencies: started
         Installing build dependencies: finished with status 'done'
         Getting requirements to build wheel: started
         Getting requirements to build wheel: finished with status 'error'
         error: subprocess-exited-with-error
         
         × Getting requirements to build wheel did not run successfully.
         │ exit code: 1
         ╰─> [33 lines of output]
             Traceback (most recent call last):
               File "/app/.heroku/python/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
                 main()
               File "/app/.heroku/python/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
                 json_out['return_val'] = hook(**hook_input['kwargs'])
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 112, in get_requires_for_build_wheel
                 backend = _build_backend()
                           ^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
                 obj = import_module(mod_path)
                       ^^^^^^^^^^^^^^^^^^^^^^^
               File "/app/.heroku/python/lib/python3.12/importlib/__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 1310, in _find_and_load_unlocked
               File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
               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 994, in exec_module
               File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
               File "/tmp/pip-build-env-e43lqbna/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 16, in <module>
                 import setuptools.version
               File "/tmp/pip-build-env-e43lqbna/overlay/lib/python3.12/site-packages/setuptools/version.py", line 1, in <module>
                 import pkg_resources
               File "/tmp/pip-build-env-e43lqbna/overlay/lib/python3.12/site-packages/pkg_resources/__init__.py", line 2172, in <module>
                 register_finder(pkgutil.ImpImporter, find_on_path)
                                 ^^^^^^^^^^^^^^^^^^^
             AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
             [end of output]
         
         note: This error originates from a subprocess, and is likely not a problem with pip.
       error: subprocess-exited-with-error
       
       × Getting requirements to build wheel did not run successfully.
       │ exit code: 1
       ╰─> See above for output.
       
       note: This error originates from a subprocess, and is likely not a problem with pip.
 !     Push rejected, failed to compile Python app.
 !     Push failed

requirements.txt内容

asgiref==3.7.2
blinker==1.7.0
certifi==2022.12.7
chardet==5.2.0
charset-normalizer==2.1.1
click==8.1.7
colorama==0.4.6
Django==5.0.1
filelock==3.9.0
Flask==3.0.1
fsspec==2023.4.0
gunicorn==21.2.0
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
mpmath==1.3.0
networkx==3.0
numpy==1.24.1
packaging==23.2
Pillow==9.3.0
python==3.12.1
requests==2.28.1
sqlparse==0.4.4
sympy==1.12
torch==2.1.2+cu118
torchaudio==2.1.2+cu118
torchvision==0.16.2+cu118
typing_extensions==4.4.0
tzdata==2023.4
urllib3==1.26.13
Werkzeug==3.0.1

Procfile内容

web: python app.py

app.py内容

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, it's me!"

if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development servers
    app.run()

另外,执行pip freeze >requirements.txt生成的文件原本不包含python==3.12.1,我手动添加了该条目,但无论是否添加,部署时都会出现相同错误。

内容的提问来源于stack exchange,提问作者Maryna K.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 03:59:51