Python 应用部署指南 #################### 入门指南 =========== 准备工作 ----------- 首先,我们从 github 上 clone 一个 Python 应用示例程序。 .. code-block:: console $ git clone https://github.com/sinacloud/python-getting-started.git $ cd python-getting-started $ ls Procfile README.md main.py requirements.txt runtime.txt 这个示例程序是一个使用 `Tornado `_ 框架写的 HTTP Web 服务器,我们依次来看下每个文件的作用,首先,main.py 文件,这个是应用主要的代码。 .. code-block:: python # ... class MainHandler(tornado.web.RequestHandler): @gen.coroutine def get(self): http_client = AsyncHTTPClient() response = yield http_client.fetch("http://www.sinacloud.com") self.set_header('content-type', 'text/plain') self.write('Hello, World! ' + response.body[:100]) application = tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(application) http_server.listen(5050 or os.environ['PORT']) tornado.ioloop.IOLoop.current().start() 这段程序就是设置了一些 HTTP Handler,最后启动一个 Web 服务器,监听在环境变量 ``PORT`` 指定的端口上。 上面程序依赖于 ``tornado`` 这个包, ``requirements.txt`` 这个文件里列出了这些依赖。 :: tornado==4.2.1 最后, ``runtime.txt`` 文件里指定了使用的 Python 版本, ``Procfile`` 里指定了怎么启动这个 server。 :: web: python main.py .. include:: ./deploy-to-sinacloud.snippet.rst 使用 Gunicorn 作 Web 服务器 ---------------------------- 您也可以使用 `Gunicorn `_ 做 Web 服务器来运行应用。 首先,将 ``gunicorn`` 加入依赖描述文件 ``requirements.txt`` 中,然后修改 ``Procfile`` 文件命令,使用 gunicorn 来作为启动命令。重新部署即可。 :: web: gunicorn --worker-class tornado main:application 构建和运行说明 ================ 如何识别应用是 Python 应用? ---------------------------- 容器云通过判断你的应用根目录下是否存在 ``requirements.txt`` 文件来判断你的应用是否是 Python 应用,所以,即使你的应用没有任何第三方依赖,你仍然需要在根目录下创建一个空的 ``requirements.txt`` 文件。 指定 Python 版本 ---------------- 如果你想要指定你想要使用的 Python 版本,你可以通过在你的应用的根目录下创建一个 ``runtime.txt`` 文件,在里面写上你要使用的 Python 版本。例如: .. code-block:: console $ cat runtime.txt python-3.6.6 我们建议你始终明确指定你要使用的 Python 版本为你本地测试时使用的版本。 以下是当前支持的 Python 版本列表: - pypy3-2.4.0 - pypy3-5.5.0 - pypy3-5.7.1 - pypy3-5.8.0 - pypy-5.3.1 - pypy-5.6.0 - pypy-5.7.0 - pypy-5.7.1 - pypy-5.8.0 - python-2.4.6 - python-2.5.5 - python-2.6.9 - python-2.7.9 ~ python-2.7.15 - python-3.4.9 - python-3.5.2 python-3.5.3 python-3.5.6 - python-3.6.0 ~ python-3.6.8 - python-3.7.0 ~ python-3.7.2 第三方依赖 ----------- 容器云在构建应用的时候会执行下面的命令来安装所有的第三方依赖包,所以你不用将这些第三方依赖包提交入你的代码仓库中,你只需要在 ``requirements.txt`` 文件中指定你有哪些依赖即可。 .. code-block:: console $ pip install -r requirements.txt --allow-all-external .. note:: 部分第三方依赖需要安装额外的底层库,这些依赖需在 ``requirements.txt`` 文件中 **显式添加** 才能使用,否则会报编译错误,以下是其列表: - `lxml `_ - `PIL `_ - `Pillow `_ - `pylibmc `_ - `GDAL `_ 比如 `pyquery `_ 依赖 lxml,需要在 ``requirements.txt`` 文件中显式添加 lxml 这个依赖才可以使用。 系统依赖 ----------- 容器云在构建应用的时候会执行下面的命令来安装系统包,所以您只需要在 ``sinacloud-packages.txt`` 文件中指定您有哪些依赖包即可。 .. code-block:: console # cat sinacloud-packages.txt curl wget vim # apt-get install -y curl wget vim .. note:: 第三方依赖包来源于 `http://archive.ubuntu.com/ubuntu/ `_ ,请确保依赖包正确性。 指定如何运行应用程序 --------------------- 你需要通过 :ref:`Procfile` 来指定如何运行你的应用程序。 .. Django 应用支持 .. ----------------- .. .. 静态文件 .. ````````` .. .. http://devcenter.heroku.com/articles/django-assets