python - Unicode解码错误: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when deploying to Heroku

标签 python django git heroku

我正在尝试遵循本教程:http://tutorial.djangogirls.org/en/index.html

我正在完成这一部分:http://tutorial.djangogirls.org/en/deploy/README.html

我要通过 git 将其推送到 heroku。我熟悉 git,但不熟悉 heroku,虽然我了解 python,但我是 django 初学者。

当我执行命令 git push heroku master 时,我得到此输出,该输出阻止了应用程序的部署。

这是我收到的错误:

(myvenv) $> git push heroku master
Counting objects: 19, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (19/19), 3.81 KiB | 0 bytes/s, done.
Total 19 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing runtime (python-3.4.1)
remote: -----> Installing dependencies with pip
remote:        Exception:
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-      6.0.6-py3.4.egg/pip/basecommand.py", lin
, in main
remote:            status = self.run(options, args)
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-    6.0.6-py3.4.egg/pip/commands/install.py"
e 321, in run
remote:            finder=finder, options=options, session=session):
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-6.0.6-py3.4.egg/pip/req/req_file.py", li
, in parse_requirements
remote:            session=session,
remote:          File "/app/.heroku/python/lib/python3.4/site-packages/pip-  6.0.6-py3.4.egg/pip/download.py", line 4
n get_file_content
remote:            content = f.read()
remote:          File "/app/.heroku/python/lib/python3.4/codecs.py", line   313, in decode
remote:            (result, consumed) = self._buffer_decode(data, self.errors, final)
remote:        UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in      position 0: invalid start byte
remote:
remote:
remote:  !     Push rejected, failed to compile Python app
remote:
remote: Verifying deploy...
remote:
remote: !       Push rejected to chsdjangoblog.
remote:
To https://git.heroku.com/chsdjangoblog.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/chsdjangoblog.git'

有人知道为什么会发生这种情况吗? heroku 似乎很好用,是否有更好的替代方案/heroku 的最佳用例是什么?我真的只是想解决这个问题,这样我就可以继续教程。一段时间以来,学习 django 一直是我的目标,因为我厌倦了 Word Press 和 PHP 开发,而且长期以来一直是 Python 爱好者。

出现错误后,当我尝试下一步时: heroku ps:scale web=1 我得到以下输出:

Scaling dynos... failed
! App mus tbe deployed before dynos can be scaled.

提前致谢。

编辑:

这是我的要求.txt:

Django==1.8
dj-database-url==0.3.0
gunicorn==19.3.0
heroku==0.1.4
python-dateutil==1.5
requests==2.6.0
whitenoise==1.0.6
psycopg2==2.5.4`

我尝试过另存为 UTF-8、ANSI、UTF-16。对他们所有人来说都是同样的信息。我什至重写了它,没有复制粘贴。为什么无论编码如何我的第一个字节总是 0xff? heroku 期望什么?有没有办法/工具来检查 txt 文件中的字节?

最佳答案

在这个问题的其他贡献者的肩膀上,看起来您的 requirements.txt 文件被编码为 UTF-16 小端。

0xFFByte Order Mark 的第一个字符对于UTF-16-LE,第二个字符是0xFE。回溯表明第一个字符是位置 0 处的 0xFF,并且在 Windows 中文件通常以带有 BOM 的 UTF-16 形式存储。

尝试将 requirements.txt 文件保存为 不带 BOM 的 UTF-8 或 ASCII。简单的旧 notepad.exe 可能就可以解决问题。

编辑

无法在记事本中工作,因此请使用 Python 3:

with open('requirements.txt', encoding='utf-16') as old, open('requirements_new.txt', 'w', encoding='utf-8') as new:
    new.write(old.read())

requirements_new.txt 现在将被编码为 UTF-8 并且应该可以工作(无论如何它最终可能会编码为 ASCII)。

请注意,这是基于其他人的评论和答案,这些评论和答案表明麻烦的文件是 requirements.txt

关于python - Unicode解码错误: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when deploying to Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29513875/

相关文章:

Python:正确分配测试描述符

python - 如何从 Django PolygonField 中提取点列表?

python - 如何停止 TastyPie 无缘无故地执行更新查询?

git - 从现有的存储库初始化

git - 如何使 Git 默认为 "add --all"?

python - 终止生成器表达式

python - kd-tree 可以用点积构建吗?

python - 解析 Latex : grammar, 递归下降的简单扩展,pyParsing?

python - Django unique_together 但仅用于过滤

git - 如何将我的应用程序上传到 github 但删除敏感的授权信息?