'AttributeError: 'PosixPath' object has no attribute 'read_text' while building heroku app

I am getting AttributeError: 'PosixPath' object has no attribute 'read_text' when trying to deploy a django app to heroku. The problem occurs when pip is installing pathlib python library after running the git push heroku main command on the command line. Is there something I am doing wrong?

The following is the log generated on heroku, which is similar to what I saw on the command line:

-----> Building on the Heroku-20 stack
-----> Using buildpacks:
       1. https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
       2. heroku/python
-----> ffmpeg app detected
-----> Installing ffmpeg
       Variable FFMPEG_DOWNLOAD_URL isn't set, using default value
       Downloading https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz
       Unpacking the archive
       Installation successful
-----> Python app detected
-----> Using Python version specified in runtime.txt
 !     Python has released a security update! Please consider upgrading to python-3.7.13
       Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Python version has changed from python-3.9.10 to python-3.7.10, clearing cache
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.7.10
-----> Installing pip 22.0.4, setuptools 60.10.0 and wheel 0.37.1
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting asgiref==3.5.0
         Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
       Collecting attrs==21.4.0
         Downloading attrs-21.4.0-py2.py3-none-any.whl (60 kB)
       Collecting autobahn==22.2.2
         Downloading autobahn-22.2.2.tar.gz (375 kB)
         Preparing metadata (setup.py): started
         Preparing metadata (setup.py): finished with status 'done'
       Collecting Automat==20.2.0
         Downloading Automat-20.2.0-py2.py3-none-any.whl (31 kB)
       Collecting blinker==1.4
         Downloading blinker-1.4.tar.gz (111 kB)
         Preparing metadata (setup.py): started
         Preparing metadata (setup.py): finished with status 'done'
       Collecting cachetools==5.0.0
         Downloading cachetools-5.0.0-py3-none-any.whl (9.1 kB)
       Collecting certifi==2021.10.8
         Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
       Collecting cffi==1.15.0
         Downloading cffi-1.15.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (427 kB)
       Collecting channels==3.0.4
         Downloading channels-3.0.4-py3-none-any.whl (38 kB)
       Collecting charset-normalizer==2.0.12
         Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
       Collecting constantly==15.1.0
         Downloading constantly-15.1.0-py2.py3-none-any.whl (7.9 kB)
       Collecting cryptography==36.0.1
         Downloading cryptography-36.0.1-cp36-abi3-manylinux_2_24_x86_64.whl (3.6 MB)
       Collecting daphne==3.0.2
         Downloading daphne-3.0.2-py3-none-any.whl (26 kB)
       Collecting Django==3.2.12
         Downloading Django-3.2.12-py3-none-any.whl (7.9 MB)
       Collecting djangorestframework==3.13.1
         Downloading djangorestframework-3.13.1-py3-none-any.whl (958 kB)
       Collecting ffmpeg-python==0.2.0
         Downloading ffmpeg_python-0.2.0-py3-none-any.whl (25 kB)
       Collecting future==0.18.2
         Downloading future-0.18.2.tar.gz (829 kB)
         Preparing metadata (setup.py): started
         Preparing metadata (setup.py): finished with status 'done'
       Collecting google-api-core==2.7.2
         Downloading google_api_core-2.7.2-py3-none-any.whl (114 kB)
       Collecting google-api-python-client==2.44.0
         Downloading google_api_python_client-2.44.0-py2.py3-none-any.whl (8.3 MB)
       Collecting google-auth==2.6.4
         Downloading google_auth-2.6.4-py2.py3-none-any.whl (156 kB)
       Collecting google-auth-httplib2==0.1.0
         Downloading google_auth_httplib2-0.1.0-py2.py3-none-any.whl (9.3 kB)
       Collecting google-auth-oauthlib==0.5.1
         Downloading google_auth_oauthlib-0.5.1-py2.py3-none-any.whl (19 kB)
       Collecting googleapis-common-protos==1.56.0
         Downloading googleapis_common_protos-1.56.0-py2.py3-none-any.whl (241 kB)
       Collecting httplib2==0.20.4
         Downloading httplib2-0.20.4-py3-none-any.whl (96 kB)
       Collecting hyperlink==21.0.0
         Downloading hyperlink-21.0.0-py2.py3-none-any.whl (74 kB)
       Collecting idna==3.3
         Downloading idna-3.3-py3-none-any.whl (61 kB)
       Collecting incremental==21.3.0
         Downloading incremental-21.3.0-py2.py3-none-any.whl (15 kB)
       Collecting mega.py==1.0.8
         Downloading mega.py-1.0.8-py2.py3-none-any.whl (19 kB)
       Collecting oauthlib==3.2.0
         Downloading oauthlib-3.2.0-py3-none-any.whl (151 kB)
       Collecting protobuf==3.20.0
         Downloading protobuf-3.20.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)
       Collecting pyasn1==0.4.8
         Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
       Collecting pyasn1-modules==0.2.8
         Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
       Collecting pycparser==2.21
         Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
       Collecting pycryptodome==3.14.1
         Downloading pycryptodome-3.14.1-cp35-abi3-manylinux2010_x86_64.whl (2.0 MB)
       Collecting pyOpenSSL==22.0.0
         Downloading pyOpenSSL-22.0.0-py2.py3-none-any.whl (55 kB)
       Collecting pyparsing==3.0.8
         Downloading pyparsing-3.0.8-py3-none-any.whl (98 kB)
       Collecting pytz==2022.1
         Downloading pytz-2022.1-py2.py3-none-any.whl (503 kB)
       Collecting requests==2.27.1
         Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
       Collecting requests-oauthlib==1.3.1
         Downloading requests_oauthlib-1.3.1-py2.py3-none-any.whl (23 kB)
       Collecting rsa==4.8
         Downloading rsa-4.8-py3-none-any.whl (39 kB)
       Collecting service-identity==21.1.0
         Downloading service_identity-21.1.0-py2.py3-none-any.whl (12 kB)
       Collecting six==1.16.0
         Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
       Collecting sqlparse==0.4.2
         Downloading sqlparse-0.4.2-py3-none-any.whl (42 kB)
       Collecting tenacity==5.1.5
         Downloading tenacity-5.1.5-py2.py3-none-any.whl (34 kB)
       Collecting Twisted==22.2.0
         Downloading Twisted-22.2.0-py3-none-any.whl (3.1 MB)
       Collecting txaio==22.2.1
         Downloading txaio-22.2.1-py2.py3-none-any.whl (30 kB)
       Collecting typing_extensions==4.1.1
         Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
       Collecting uritemplate==4.1.1
         Downloading uritemplate-4.1.1-py2.py3-none-any.whl (10 kB)
       Collecting urllib3==1.26.9
         Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
       Collecting zope.interface==5.4.0
         Downloading zope.interface-5.4.0-cp37-cp37m-manylinux2010_x86_64.whl (251 kB)
       Collecting twisted[tls]>=18.7
         Downloading Twisted-22.4.0-py3-none-any.whl (3.1 MB)
       Collecting pathlib==1.0.1
         Downloading pathlib-1.0.1.tar.gz (49 kB)
         Preparing metadata (setup.py): started
         Preparing metadata (setup.py): finished with status 'error'
         error: subprocess-exited-with-error
         
         × python setup.py egg_info did not run successfully.
         │ exit code: 1
         ╰─> [20 lines of output]
             Traceback (most recent call last):
               File "<string>", line 36, in <module>
               File "<pip-setuptools-caller>", line 34, in <module>
               File "/tmp/pip-install-fwxp6f5v/pathlib_5dc29fdd1ec44473b4d7032f8a49d94d/setup.py", line 30, in <module>
                 url='https://pathlib.readthedocs.org/',
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 109, in setup
                 _setup_distribution = dist = klass(attrs)
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/dist.py", line 457, in __init__
                 for ep in metadata.entry_points(group='distutils.setup_keywords'):
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 999, in entry_points
                 return SelectableGroups.load(eps).select(**params)
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 449, in load
                 ordered = sorted(eps, key=by_group)
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 997, in <genexpr>
                 dist.entry_points for dist in unique(distributions())
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 609, in entry_points
                 return EntryPoints._from_text_for(self.read_text('entry_points.txt'), self)
               File "/app/.heroku/python/lib/python3.7/site-packages/setuptools/_vendor/importlib_metadata/__init__.py", line 917, in read_text
                 return self._path.joinpath(filename).read_text(encoding='utf-8')
             AttributeError: 'PosixPath' object has no attribute 'read_text'
             [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.
 !     Push rejected, failed to compile Python app.
 !     Push failed


Solution 1:[1]

I found a solution that worked for me; I removed the packages mega and pathlib form the requirements.txt file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Walter Maina