将Django/Python 3.4部署到Heroku
我正在尝试使用Django和Heroku来部署我的第一个示例应用,参考的是Django/Heroku的入门教程。
我使用的工具是:Python 3.4和Windows 7的PowerShell。
我遇到的挑战是:在部署到Heroku时失败了,我不太确定原因是什么。在第一次执行“git push”时,我发现默认使用的是python-2.7.0。于是我在应用的根目录下添加了一个runtime.txt
文件,里面写的是python-3.4.0。
当我运行git push heroku master
时,发生了以下情况:
-----> Python app detected
-----> Preparing Python runtime (python-3.4.0)
-----> Installing Setuptools (2.1)
-----> Installing Pip (1.5.4)
-----> Installing dependencies using Pip (1.5.4)
Exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/commands/install.py", line 262, in run
for req in parse_requirements(filename, finder=finder, options=options, session=session):
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/req.py", line 1546, in parse_requirements
session=session,
File "/app/.heroku/python/lib/python3.4/site-packages/pip-1.5.4-py3.4.egg/pip/download.py", line 275, in get_file_content
content = f.read()
File "/app/.heroku/python/lib/python3.4/codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Storing debug log for failure in /app/.pip/pip.log
! Push rejected, failed to compile Python app
这是我的requirements.txt
文件的内容(是用pip freeze > requirements.txt
命令创建的)
Django==1.6.2
dj-database-url==0.3.0
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.2
pystache==0.5.3
static==1.0.2
这是我的Procfile
(顺便说一下:gunicorn似乎是一个只适用于Unix的命令,在Windows上不工作;可以在这里查看):
web: gunicorn mytodo.wsgi
Heroku的教程没有提到setup.py
文件,但似乎这个文件是必要的,所以我简单地复制了一个模板……这不是我最喜欢的解决方案,但我不知道该怎么办。
setup(
name='mysite',
version='0.1.0',
install_requires=[], # Don't put anything here, just use requirements.txt
packages=['mysite'],
package_dir={'mysite': 'src/mysite'},
)
可能发生的情况:
- Unicode错误信息可能来自Procfile
。我在网上看到过说它必须是ASCII文件,但我不确定怎么声明,因为Procfile没有文件后缀。
- setup.py
文件可能有问题。
任何帮助都非常感谢。谢谢!
3 个回答
我不是专家,但你可以看看这篇关于如何从Windows电脑部署到Heroku的博客文章。希望对你有帮助。http://www.swarley.me.uk/blog/2014/02/24/create-a-django-development-environment-on-64-bit-windows-for-heroku-deployment/
更新:好吧,我觉得我有了更好的答案。首先,Heroku明确表示,Windows用户会遇到一些Linux和iOS用户不会遇到的问题:https://devcenter.heroku.com/articles/bundler-windows-gemfile 这篇文章是关于Ruby的,但同样的问题也适用于其他语言,因为它们讨论的是你使用的操作系统。
不过,这个解决方案对我有效:使用Bitbucket作为你的远程仓库,然后从Windows电脑上传到Bitbucket。接着再从Bitbucket上传到Heroku。这里有一个非常相似的问题和答案在SO上:使用Bitbucket上的git部署到Heroku
试着把requirements.txt文件里的static==1.0.2删掉。这个版本和python 3.4不太兼容。不过,通过dj-static安装的话就没问题了。这对我有效:
Django==1.5.1
dj-database-url==0.2.2
dj-static==0.0.5
gunicorn==18.0
psycopg2==2.5.1
我在尝试把一个Django应用部署到Heroku时,遇到了完全一样的问题,系统是Windows 7。问题的原因是这样的:命令pip freeze >requirements.txt
会把文件编码成UTF-16格式,而Heroku希望这个requirements.txt文件是用ansi编码的。
为了解决这个问题,我打开了requirements.txt文件,用记事本编辑,然后选择文件菜单中的“另存为”,在编码选项里选择ANSI,保存后再提交这个新的requirements.txt。之后我就可以运行git push heroku master
,一切都正常了。