我创建了一个 Python 应用程序,您可能会发现 on GitHub 。我想测试一下我是否能够部署它并选择了 Heroku,这是我第一次部署一些东西。即使它在本地运行良好,它也无法在 Heroku 上部署,并出现以下错误 TypeError: the JSON object must be str, not 'bytes'
。看来错误来自构建包中的 lockfile = json.load(f)
。
问题
在阅读与 json.load()
相关的错误时,出现了一个关于为什么此部署失败的问题:Heroku 的 Python 构建包仅保留给 Python 2 应用程序吗?
Heroku 不成功的构建日志
完整日志是:
Activity Feed Build Log
ID 4d444270-f24f-461c-9079-bcd9134cec62
-----> Python app detected
! The latest version of Python 3 is python-3.6.5 (you are using python-3.5.2, which is unsupported).
! We recommend upgrading by specifying the latest version (python-3.6.5).
Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Installing pip
Traceback (most recent call last):
File "/app/tmp/buildpacks/779a8bbfbbe7e1b715476c0b23fc63a2103b3e4131eda558669aba8fb5e6e05682419376144189b29beb5dee6d7626b4d3385edb0954bffea6c67d8cf622fd51/vendor/pipenv-to-pip", line 23, in <module>
main()
File "/app/tmp/buildpacks/779a8bbfbbe7e1b715476c0b23fc63a2103b3e4131eda558669aba8fb5e6e05682419376144189b29beb5dee6d7626b4d3385edb0954bffea6c67d8cf622fd51/vendor/pipenv-to-pip", line 11, in main
lockfile = json.load(f)
File "/app/.heroku/python/lib/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/app/.heroku/python/lib/python3.5/json/__init__.py", line 312, in loads
s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
! Push rejected, failed to compile Python app
! Push failed
Pip 文件
我指定我想使用 Python 3.6.5
[[source]]
url = "https://pypi.python.org/simple"
[packages]
requests = { extras = ['socks'] }
[requires]
python_version = "3.6.5"
构建包
我在设置中选择了 Python 构建包:
其他信息
- 使用的版本:
我使用Python 3.5.2。或者至少我是这么认为! 这是 full requirements .
- 之前的部署尝试
我不知道,但我尝试在 OpenShift 上部署该应用程序。该应用程序日志显示它在依赖项下载期间失败,我认为可以肯定地说它与内存相关。我还没有尝试增加构建内存限制以允许构建一直完成。
- 检查答案
我已经查过了
- TypeError: the JSON object must be str, not 'bytes'.
但我不确定我可以要求他们使用请求的内置
.json()
函数:lockfile.json(f)
? - Giving error “the JSON object must be str, not 'bytes' ” 答案完全相同,但这让我想知道构建包是否适用于 Python 2
最佳答案
I use Python 3.5.2.
Heroku 仅支持Python 2.7 和 3.6; Python 3.5 不支持将二进制文件传递给 json.load()。您发布的日志明确地告诉您:
(you are using python-3.5.2, which is unsupported).
另请参阅heroku-buildpack-python issue #678 :
pull requests accepted! Note, we officially only endorse the use of latest 2.7.x and 3.6.x.
拉取请求已提交并接受,升级到 v135 应该可以解决此问题。但是,我对 Heroku 还不够熟悉,无法告诉您是否可以实现这一点。我只是将您的 Python 版本升级到 3.6。
关于python - 部署应用程序时,构建包中的 json.load() 出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932087/