Heroku ---> Installing pip remote: AttributeError: module 'pip._vendor.requests' has no attribute 'Session'

A Python 3.6 Django ==11 app was being deployed and code being pushed regularly till yesterday. Now I have the error:

remote: AttributeError: module 'pip._vendor.requests' has no attribute 'Session'

Entire trace:

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing pip
remote: Traceback (most recent call last):
remote:   File "/app/tmp/buildpacks/779a8bbfbbe7e1b715476c0b23fc63a2103b3e4131eda558669aba8fb5e6e05682419376144189b29beb5dee6d7626b4d3385edb0954bffea6c67d8cf622fd51/vendor/pip-pop/pip-diff", line 15, in <module>
remote:     from pip.req import parse_requirements
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/req/__init__.py", line 3, in <module>
remote:     from .req_install import InstallRequirement
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/req/req_install.py", line 25, in <module>
remote:     import pip.wheel
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/wheel.py", line 28, in <module>
remote:     from pip.download import path_to_url, unpack_url
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/download.py", line 40, in <module>
remote:     from pip._vendor import requests, six
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/_vendor/requests/__init__.py", line 83, in <module>
remote:     from pip._internal.compat import WINDOWS
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/_internal/__init__.py", line 42, in <module>
remote:     from pip._internal import cmdoptions
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/_internal/cmdoptions.py", line 16, in <module>
remote:     from pip._internal.index import (
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/_internal/index.py", line 25, in <module>
remote:     from pip._internal.download import HAS_TLS, is_url, path_to_url, url_to_path
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/pip/_internal/download.py", line 326, in <module>
remote:     class PipSession(requests.Session):
remote: AttributeError: module 'pip._vendor.requests' has no attribute 'Session'
remote: mount: failure.bad-requirements: No such file or directory
remote: -----> Installing requirements with pip
remote: 
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 22, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
remote:            app_config.import_models()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
remote:            self.models_module = import_module(models_module_name)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/tmp/build_989d7449dd0cd18694a87448cb09f19f/indexapp/models.py", line 9, in <module>
remote:            from .image_processing import *
remote:          File "/tmp/build_989d7449dd0cd18694a87448cb09f19f/indexapp/image_processing.py", line 7, in <module>
remote:            import cv2
remote:        ModuleNotFoundError: No module named 'cv2'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to django-rest-api-comp-vision.
remote: 
To https://git.heroku.com/django-rest-api-comp-vision.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/django-rest-api-comp-vision.git'

Another issue - which i'm aware of as seen above is with OpenCv [cv2] not being present within the default build DIR . Am in the process of deploying a Python OpenCV buildpack for the same - but need this issue resolved first:

File "/tmp/build_989d7449dd0cd18694a87448cb09f19f/indexapp/image_processing.py", line 7, in <module>
remote:            import cv2
remote:        ModuleNotFoundError: No module named 'cv2'

1 answer

  • answered 2018-04-14 20:33 dbader

    I just ran into the same error:

    AttributeError: module 'pip._vendor.requests' has no attribute 'Session'
    

    Clearing the Heroku build cache resolved the issue for me. Here's what you need to do:

    1. Install the official heroku-repo plugin for the Heroku CLI:

      $ heroku plugins:install heroku-repo
      
    2. Clear your app's build cache:

      $ heroku repo:purge_cache -a APPNAME
      
    3. Trigger a deploy to rebuild your app

    Clearing the build cache causes pip to re-install your Python dependencies from scratch on the next build, instead of attempting to upgrade your current set of dependencies.

    This resolved the AttributeError for me, hope this helps!