{"id":1143358,"date":"2025-01-08T22:49:24","date_gmt":"2025-01-08T14:49:24","guid":{"rendered":""},"modified":"2025-01-08T22:49:28","modified_gmt":"2025-01-08T14:49:28","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%86%99%e4%b8%80%e4%b8%aaapp%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143358.html","title":{"rendered":"\u7528python\u5982\u4f55\u5199\u4e00\u4e2aapp\u5e94\u7528\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181021\/cd46a1f1-c7b6-4750-8099-7e5ad2b10934.webp\" alt=\"\u7528python\u5982\u4f55\u5199\u4e00\u4e2aapp\u5e94\u7528\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u7528Python\u7f16\u5199\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u53ef\u4ee5\u5206\u4e3a\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a<strong>\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u3001\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784\u3001\u7f16\u5199\u6838\u5fc3\u903b\u8f91\u3001\u5b9e\u73b0\u7528\u6237\u754c\u9762\u3001\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3001\u90e8\u7f72\u548c\u7ef4\u62a4<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e00\u4e2a\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5b9e\u7528\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6<\/h2>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9996\u5148\u8981\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u6846\u67b6\u3002\u4e0d\u540c\u7c7b\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u6846\u67b6\u3002\u4f8b\u5982\uff0c<strong>Django\u548cFlask<\/strong>\u662f\u5e38\u7528\u7684Web\u6846\u67b6\uff0c<strong>Kivy\u548cPyQt<\/strong>\u5219\u9002\u7528\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>Django\u548cFlask<\/h3>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684Web\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5927\u578b\u9879\u76ee\u548c\u5feb\u901f\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f00\u7bb1\u5373\u7528\u7684\u529f\u80fd\uff0c\u5982\u7528\u6237\u8ba4\u8bc1\u3001\u8868\u5355\u5904\u7406\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002<\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5c0f\u578b\u9879\u76ee\u548c\u5bf9\u7075\u6d3b\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u9879\u76ee\u3002\u5b83\u7684\u6838\u5fc3\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u63d2\u4ef6\u8fdb\u884c\u529f\u80fd\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><h3>Kivy\u548cPyQt<\/h3>\n<\/p>\n<p><p>Kivy\u662f\u4e00\u4e2a\u5f00\u6e90\u7684Python\u5e93\uff0c\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u8de8\u5e73\u53f0\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u652f\u6301\u591a\u70b9\u89e6\u63a7\u548c\u5404\u79cd\u8f93\u5165\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u624b\u673a\u548c\u5e73\u677f\u7535\u8111\u7b49\u8bbe\u5907\u3002<\/p>\n<\/p>\n<p><p>PyQt\u662f\u4e00\u4e2a\u7ed1\u5b9aQt\u5e93\u7684Python\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u8de8\u5e73\u53f0\u7684\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684UI\u7ec4\u4ef6\u548c\u5f3a\u5927\u7684\u56fe\u5f62\u754c\u9762\u8bbe\u8ba1\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784<\/h2>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u4e86\u5408\u9002\u7684\u6846\u67b6\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784\u3002\u8fd9\u5305\u62ec\u786e\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u8981\u529f\u80fd\u6a21\u5757\u3001\u6570\u636e\u6d41\u548c\u4ea4\u4e92\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u786e\u5b9a\u529f\u80fd\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u9700\u6c42\uff0c\u5c06\u5176\u5212\u5206\u4e3a\u591a\u4e2a\u6a21\u5757\u3002\u6bcf\u4e2a\u6a21\u5757\u8d1f\u8d23\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u7528\u6237\u7ba1\u7406\u3001\u6570\u636e\u5904\u7406\u3001\u754c\u9762\u663e\u793a\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u6d41\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u8bbe\u8ba1\u6570\u636e\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6d41\u52a8\u65b9\u5f0f\uff0c\u5305\u62ec\u6570\u636e\u7684\u8f93\u5165\u3001\u5904\u7406\u548c\u8f93\u51fa\u3002\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5b58\u50a8\u65b9\u5f0f\uff0c\u5982\u6570\u636e\u5e93\u3001\u6587\u4ef6\u7cfb\u7edf\u7b49\uff0c\u4ee5\u53ca\u6570\u636e\u7684\u4f20\u8f93\u65b9\u5f0f\uff0c\u5982API\u3001\u6d88\u606f\u961f\u5217\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4ea4\u4e92\u65b9\u5f0f\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u786e\u5b9a\u7528\u6237\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684\u4ea4\u4e92\u65b9\u5f0f\uff0c\u5305\u62ec\u7528\u6237\u754c\u9762\u7684\u8bbe\u8ba1\u3001\u7528\u6237\u8f93\u5165\u7684\u5904\u7406\u7b49\u3002\u5bf9\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528HTML\u3001CSS\u548cJavaScript\u8fdb\u884c\u524d\u7aef\u5f00\u53d1\uff1b\u5bf9\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Kivy\u6216PyQt\u8fdb\u884c\u754c\u9762\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7f16\u5199\u6838\u5fc3\u903b\u8f91<\/h2>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u597d\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u7f16\u5199\u6838\u5fc3\u903b\u8f91\u3002\u8fd9\u90e8\u5206\u4e3b\u8981\u6d89\u53ca\u5177\u4f53\u529f\u80fd\u7684\u5b9e\u73b0\uff0c\u5305\u62ec\u6570\u636e\u5904\u7406\u3001\u4e1a\u52a1\u903b\u8f91\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u9700\u6c42\uff0c\u7f16\u5199\u6570\u636e\u5904\u7406\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u5e93\u3001\u6587\u4ef6\u5904\u7406\u3001\u6570\u636e\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Django\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class User(models.Model):<\/p>\n<p>    username = models.CharField(max_length=100)<\/p>\n<p>    em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l = models.EmailField()<\/p>\n<p>    password = models.CharField(max_length=100)<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return self.username<\/p>\n<h2><strong>\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528\u8be5\u6a21\u578b<\/strong><\/h2>\n<p>from django.shortcuts import render<\/p>\n<p>from .models import User<\/p>\n<p>def user_list(request):<\/p>\n<p>    users = User.objects.all()<\/p>\n<p>    return render(request, &#39;user_list.html&#39;, {&#39;users&#39;: users})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e1a\u52a1\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u7f16\u5199\u4e1a\u52a1\u903b\u8f91\u7684\u4ee3\u7801\u3002\u4f8b\u5982\uff0c\u7528\u6237\u8ba4\u8bc1\u3001\u6743\u9650\u7ba1\u7406\u3001\u6570\u636e\u6821\u9a8c\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Flask\u8fdb\u884c\u7528\u6237\u8ba4\u8bc1\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>from werkzeug.security import generate_password_hash, check_password_hash<\/p>\n<p>app = Flask(__name__)<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e\u5e93<\/strong><\/h2>\n<p>users = {}<\/p>\n<p>@app.route(&#39;\/register&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def register():<\/p>\n<p>    data = request.get_json()<\/p>\n<p>    username = data[&#39;username&#39;]<\/p>\n<p>    password = generate_password_hash(data[&#39;password&#39;])<\/p>\n<p>    users[username] = password<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;User registered successfully&#39;}), 201<\/p>\n<p>@app.route(&#39;\/login&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    data = request.get_json()<\/p>\n<p>    username = data[&#39;username&#39;]<\/p>\n<p>    password = data[&#39;password&#39;]<\/p>\n<p>    if username in users and check_password_hash(users[username], password):<\/p>\n<p>        return jsonify({&#39;message&#39;: &#39;Login successful&#39;}), 200<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;Invalid credentials&#39;}), 401<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5b9e\u73b0\u7528\u6237\u754c\u9762<\/h2>\n<\/p>\n<p><p>\u7528\u6237\u754c\u9762\u662f\u7528\u6237\u4e0e\u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\u7684\u6865\u6881\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u7c7b\u578b\uff0c\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u548c\u5de5\u5177\u6765\u8bbe\u8ba1\u548c\u5b9e\u73b0\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<p><h3>Web\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528HTML\u3001CSS\u548cJavaScript\u8fdb\u884c\u524d\u7aef\u5f00\u53d1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684HTML\u9875\u9762\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;User List&lt;\/title&gt;<\/p>\n<p>    &lt;style&gt;<\/p>\n<p>        table {<\/p>\n<p>            width: 100%;<\/p>\n<p>            border-collapse: collapse;<\/p>\n<p>        }<\/p>\n<p>        th, td {<\/p>\n<p>            border: 1px solid black;<\/p>\n<p>            padding: 8px;<\/p>\n<p>            text-align: left;<\/p>\n<p>        }<\/p>\n<p>    &lt;\/style&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;User List&lt;\/h1&gt;<\/p>\n<p>    &lt;table&gt;<\/p>\n<p>        &lt;tr&gt;<\/p>\n<p>            &lt;th&gt;Username&lt;\/th&gt;<\/p>\n<p>            &lt;th&gt;Email&lt;\/th&gt;<\/p>\n<p>        &lt;\/tr&gt;<\/p>\n<p>        {% for user in users %}<\/p>\n<p>        &lt;tr&gt;<\/p>\n<p>            &lt;td&gt;{{ user.username }}&lt;\/td&gt;<\/p>\n<p>            &lt;td&gt;{{ user.email }}&lt;\/td&gt;<\/p>\n<p>        &lt;\/tr&gt;<\/p>\n<p>        {% endfor %}<\/p>\n<p>    &lt;\/table&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u684c\u9762\u5e94\u7528\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528Kivy\u6216PyQt\u8fdb\u884c\u754c\u9762\u8bbe\u8ba1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Kivy\u521b\u5efa\u7b80\u5355\u7a97\u53e3\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from kivy.app import App<\/p>\n<p>from kivy.uix.label import Label<\/p>\n<p>class MyApp(App):<\/p>\n<p>    def build(self):<\/p>\n<p>        return Label(text=&#39;Hello, World!&#39;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    MyApp().run()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u662f\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002\u6d4b\u8bd5\u53ef\u4ee5\u5206\u4e3a\u5355\u5143\u6d4b\u8bd5\u3001\u96c6\u6210\u6d4b\u8bd5\u548c\u7cfb\u7edf\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h3>\u5355\u5143\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u662f\u5bf9\u5e94\u7528\u7a0b\u5e8f\u7684\u5355\u4e2a\u529f\u80fd\u6a21\u5757\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5176\u529f\u80fd\u7684\u6b63\u786e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528unittest\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from myapp import add<\/p>\n<p>class TestMyApp(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u96c6\u6210\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u96c6\u6210\u6d4b\u8bd5\u662f\u5bf9\u591a\u4e2a\u529f\u80fd\u6a21\u5757\u8fdb\u884c\u7ec4\u5408\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5176\u534f\u540c\u5de5\u4f5c\u6b63\u5e38\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528pytest\u8fdb\u884c\u96c6\u6210\u6d4b\u8bd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytest<\/p>\n<p>from myapp import app<\/p>\n<p>@pytest.fixture<\/p>\n<p>def client():<\/p>\n<p>    with app.test_client() as client:<\/p>\n<p>        yield client<\/p>\n<p>def test_register_login(client):<\/p>\n<p>    response = client.post(&#39;\/register&#39;, json={&#39;username&#39;: &#39;test&#39;, &#39;password&#39;: &#39;test&#39;})<\/p>\n<p>    assert response.status_code == 201<\/p>\n<p>    response = client.post(&#39;\/login&#39;, json={&#39;username&#39;: &#39;test&#39;, &#39;password&#39;: &#39;test&#39;})<\/p>\n<p>    assert response.status_code == 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7cfb\u7edf\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u7cfb\u7edf\u6d4b\u8bd5\u662f\u5bf9\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u5176\u5728\u5b9e\u9645\u4f7f\u7528\u73af\u5883\u4e2d\u7684\u529f\u80fd\u6b63\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u5982Selenium\u8fdb\u884c\u7cfb\u7edf\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u90e8\u7f72\u548c\u7ef4\u62a4<\/h2>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6d4b\u8bd5\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u662f\u5c06\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u3002\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u7c7b\u578b\uff0c\u9009\u62e9\u5408\u9002\u7684\u90e8\u7f72\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>Web\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eWeb\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u9009\u62e9\u4e91\u670d\u52a1\u63d0\u4f9b\u5546\uff0c\u5982AWS\u3001Azure\u3001Google Cloud\u8fdb\u884c\u90e8\u7f72\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Heroku\u90e8\u7f72Flask\u5e94\u7528\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa<code>Procfile<\/code>\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-plaintext\">web: python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa<code>requirements.txt<\/code>\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-plaintext\">Flask==2.0.1<\/p>\n<p>Werkzeug==2.0.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u90e8\u7f72\u5230Heroku\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">heroku create myapp<\/p>\n<p>git add .<\/p>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<p>git push heroku master<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u6253\u5305<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528PyInstaller\u8fdb\u884c\u6253\u5305\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528PyInstaller\u6253\u5305Kivy\u5e94\u7528\u7a0b\u5e8f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p>pyinstaller --onefile myapp.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u3001\u8bbe\u8ba1\u5e94\u7528\u7a0b\u5e8f\u7684\u67b6\u6784\u3001\u7f16\u5199\u6838\u5fc3\u903b\u8f91\u3001\u5b9e\u73b0\u7528\u6237\u754c\u9762\u3001\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3001\u90e8\u7f72\u548c\u7ef4\u62a4\u3002\u901a\u8fc7\u5408\u7406\u7684\u89c4\u5212\u548c\u8bbe\u8ba1\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u65e0\u8bba\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u8fd8\u662f\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\uff0cPython\u90fd\u6709\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\u652f\u6301\uff0c\u80fd\u591f\u6ee1\u8db3\u4e0d\u540c\u7c7b\u578b\u7684\u5f00\u53d1\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u6765\u5f00\u53d1Python\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728\u5f00\u53d1Python\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u81f3\u5173\u91cd\u8981\u3002\u5e38\u7528\u7684\u6846\u67b6\u5305\u62ecFlask\u548cDjango\u3002Flask\u9002\u5408\u5c0f\u578b\u9879\u76ee\uff0c\u7075\u6d3b\u6027\u9ad8\uff0c\u6613\u4e8e\u4e0a\u624b\uff1b\u800cDjango\u5219\u9002\u5408\u5927\u578b\u9879\u76ee\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a\u5185\u7f6e\u529f\u80fd\uff0c\u5982\u7528\u6237\u8ba4\u8bc1\u548c\u6570\u636e\u5e93\u7ba1\u7406\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u6765\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u6846\u67b6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u6570\u636e\u5b58\u50a8\uff1f<\/strong><br \/>\u5904\u7406\u7528\u6237\u8f93\u5165\u901a\u5e38\u9700\u8981\u4f7f\u7528\u8868\u5355\u548c\u6570\u636e\u9a8c\u8bc1\u3002\u53ef\u4ee5\u4f7f\u7528Flask-WTF\u6216Django\u8868\u5355\u6a21\u5757\u6765\u7b80\u5316\u8fd9\u4e2a\u8fc7\u7a0b\u3002\u6570\u636e\u5b58\u50a8\u65b9\u9762\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528SQLite\u3001PostgreSQL\u6216MySQL\u7b49\u6570\u636e\u5e93\u3002ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\uff0c\u5982SQLAlchemy\u548cDjango ORM\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u5982\u4f55\u6d4b\u8bd5\u548c\u8c03\u8bd5Python\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u6d4b\u8bd5\u548c\u8c03\u8bd5\u662f\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u8d28\u91cf\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528unittest\u6216pytest\u6846\u67b6\u6765\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u3002\u5bf9\u4e8e\u8c03\u8bd5\uff0cPython\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684pdb\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u884c\u6267\u884c\u4ee5\u67e5\u627e\u95ee\u9898\u3002\u6b64\u5916\uff0c\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5e2e\u52a9\u60a8\u5feb\u901f\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7f16\u5199\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f \u4f7f\u7528Python\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u53ef\u4ee5\u5206\u4e3a\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u3001\u8bbe\u8ba1\u5e94 [&hellip;]","protected":false},"author":3,"featured_media":1143368,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143358"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1143358"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143358\/revisions"}],"predecessor-version":[{"id":1143370,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143358\/revisions\/1143370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143368"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}