{"id":1147351,"date":"2025-01-13T16:23:50","date_gmt":"2025-01-13T08:23:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1147351.html"},"modified":"2025-01-13T16:23:50","modified_gmt":"2025-01-13T08:23:50","slug":"%e5%a6%82%e4%bd%95%e6%94%b9python%e9%a1%b9%e7%9b%ae%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1147351.html","title":{"rendered":"\u5982\u4f55\u6539python\u9879\u76ee\u914d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25165508\/c192bce1-f2f4-4d98-ae9c-956468f17862.webp\" alt=\"\u5982\u4f55\u6539python\u9879\u76ee\u914d\u7f6e\" \/><\/p>\n<p><p> \u5728Python\u9879\u76ee\u4e2d\uff0c\u914d\u7f6e\u7ba1\u7406\u5bf9\u4e8e\u9879\u76ee\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u81f3\u5173\u91cd\u8981\u3002<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570\u548c\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u6765\u6539Python\u9879\u76ee\u914d\u7f6e<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u80fd\u591f\u5c06\u914d\u7f6e\u4e0e\u4ee3\u7801\u5206\u79bb\u3001\u4fbf\u4e8e\u7248\u672c\u63a7\u5236\u3001\u589e\u5f3a\u7075\u6d3b\u6027<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u6539Python\u9879\u76ee\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u914d\u7f6e\u6587\u4ef6\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u683c\u5f0f\uff0c\u6bd4\u5982INI\u3001YAML\u3001JSON\u3001TOML\u7b49\u3002\u6bcf\u79cd\u683c\u5f0f\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002INI\u6587\u4ef6\u7b80\u5355\u6613\u8bfb\uff0c\u9002\u7528\u4e8e\u5c0f\u578b\u9879\u76ee\uff1bYAML\u548cJSON\u9002\u5408\u590d\u6742\u914d\u7f6e\uff0c\u7ed3\u6784\u5316\u4e14\u652f\u6301\u5d4c\u5957\uff1bTOML\u7528\u4e8e\u9ad8\u6027\u80fd\u9700\u6c42\u4e0b\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001INI\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>a. \u521b\u5efaINI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>INI\u6587\u4ef6\u4f7f\u7528\u201c\u952e=\u503c\u201d\u5bf9\u6765\u5b58\u50a8\u914d\u7f6e\uff0c\u6587\u4ef6\u540d\u901a\u5e38\u4ee5\u201c.ini\u201d\u7ed3\u5c3e\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>config.ini<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[database]<\/p>\n<p>host = localhost<\/p>\n<p>port = 3306<\/p>\n<p>user = root<\/p>\n<p>password = root<\/p>\n<p>[application]<\/p>\n<p>debug = True<\/p>\n<p>secret_key = your_secret_key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u8bfb\u53d6INI\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86<code>configparser<\/code>\u6a21\u5757\u6765\u8bfb\u53d6INI\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>db_port = config[&#39;database&#39;].getint(&#39;port&#39;)<\/p>\n<p>db_user = config[&#39;database&#39;][&#39;user&#39;]<\/p>\n<p>db_password = config[&#39;database&#39;][&#39;password&#39;]<\/p>\n<p>app_debug = config[&#39;application&#39;].getboolean(&#39;debug&#39;)<\/p>\n<p>app_secret_key = config[&#39;application&#39;][&#39;secret_key&#39;]<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001YAML\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>a. \u521b\u5efaYAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>YAML\u6587\u4ef6\u7ed3\u6784\u5316\u4e14\u652f\u6301\u5d4c\u5957\uff0c\u6587\u4ef6\u540d\u901a\u5e38\u4ee5\u201c.yaml\u201d\u6216\u201c.yml\u201d\u7ed3\u5c3e\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>config.yaml<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\">database:<\/p>\n<p>  host: localhost<\/p>\n<p>  port: 3306<\/p>\n<p>  user: root<\/p>\n<p>  password: root<\/p>\n<p>application:<\/p>\n<p>  debug: true<\/p>\n<p>  secret_key: your_secret_key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u8bfb\u53d6YAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u7684<code>PyYAML<\/code>\u5e93\u53ef\u4ee5\u8bfb\u53d6YAML\u6587\u4ef6\u3002\u9996\u5148\u5b89\u88c5<code>PyYAML<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyyaml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8bfb\u53d6YAML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>with open(&#39;config.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>db_port = config[&#39;database&#39;][&#39;port&#39;]<\/p>\n<p>db_user = config[&#39;database&#39;][&#39;user&#39;]<\/p>\n<p>db_password = config[&#39;database&#39;][&#39;password&#39;]<\/p>\n<p>app_debug = config[&#39;application&#39;][&#39;debug&#39;]<\/p>\n<p>app_secret_key = config[&#39;application&#39;][&#39;secret_key&#39;]<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001JSON\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>a. \u521b\u5efaJSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>JSON\u6587\u4ef6\u5e7f\u6cdb\u5e94\u7528\u4e8e\u914d\u7f6e\u7ba1\u7406\uff0c\u6587\u4ef6\u540d\u901a\u5e38\u4ee5\u201c.json\u201d\u7ed3\u5c3e\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>config.json<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-json\">{<\/p>\n<p>  &quot;database&quot;: {<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;port&quot;: 3306,<\/p>\n<p>    &quot;user&quot;: &quot;root&quot;,<\/p>\n<p>    &quot;password&quot;: &quot;root&quot;<\/p>\n<p>  },<\/p>\n<p>  &quot;application&quot;: {<\/p>\n<p>    &quot;debug&quot;: true,<\/p>\n<p>    &quot;secret_key&quot;: &quot;your_secret_key&quot;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86<code>json<\/code>\u6a21\u5757\u6765\u8bfb\u53d6JSON\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;config.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    config = json.load(file)<\/p>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>db_port = config[&#39;database&#39;][&#39;port&#39;]<\/p>\n<p>db_user = config[&#39;database&#39;][&#39;user&#39;]<\/p>\n<p>db_password = config[&#39;database&#39;][&#39;password&#39;]<\/p>\n<p>app_debug = config[&#39;application&#39;][&#39;debug&#39;]<\/p>\n<p>app_secret_key = config[&#39;application&#39;][&#39;secret_key&#39;]<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001TOML\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>a. \u521b\u5efaTOML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>TOML\u6587\u4ef6\u7b80\u5355\u6613\u8bfb\u4e14\u5177\u6709\u8f83\u9ad8\u7684\u89e3\u6790\u6027\u80fd\uff0c\u6587\u4ef6\u540d\u901a\u5e38\u4ee5\u201c.toml\u201d\u7ed3\u5c3e\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>config.toml<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-toml\">[database]<\/p>\n<p>host = &quot;localhost&quot;<\/p>\n<p>port = 3306<\/p>\n<p>user = &quot;root&quot;<\/p>\n<p>password = &quot;root&quot;<\/p>\n<p>[application]<\/p>\n<p>debug = true<\/p>\n<p>secret_key = &quot;your_secret_key&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u8bfb\u53d6TOML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u7684<code>toml<\/code>\u5e93\u53ef\u4ee5\u8bfb\u53d6TOML\u6587\u4ef6\u3002\u9996\u5148\u5b89\u88c5<code>toml<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install toml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u8bfb\u53d6TOML\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import toml<\/p>\n<p>config = toml.load(&#39;config.toml&#39;)<\/p>\n<p>db_host = config[&#39;database&#39;][&#39;host&#39;]<\/p>\n<p>db_port = config[&#39;database&#39;][&#39;port&#39;]<\/p>\n<p>db_user = config[&#39;database&#39;][&#39;user&#39;]<\/p>\n<p>db_password = config[&#39;database&#39;][&#39;password&#39;]<\/p>\n<p>app_debug = config[&#39;application&#39;][&#39;debug&#39;]<\/p>\n<p>app_secret_key = config[&#39;application&#39;][&#39;secret_key&#39;]<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u73af\u5883\u53d8\u91cf\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u73af\u5883\u53d8\u91cf\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u914d\u7f6e\u7ba1\u7406\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u654f\u611f\u6570\u636e\u548c\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u914d\u7f6e\u3002\u901a\u8fc7\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u66f4\u6539\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u4ee5Linux\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export DATABASE_HOST=localhost<\/p>\n<p>export DATABASE_PORT=3306<\/p>\n<p>export DATABASE_USER=root<\/p>\n<p>export DATABASE_PASSWORD=root<\/p>\n<p>export APPLICATION_DEBUG=true<\/p>\n<p>export APPLICATION_SECRET_KEY=your_secret_key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86<code>os<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>db_host = os.getenv(&#39;DATABASE_HOST&#39;)<\/p>\n<p>db_port = int(os.getenv(&#39;DATABASE_PORT&#39;))<\/p>\n<p>db_user = os.getenv(&#39;DATABASE_USER&#39;)<\/p>\n<p>db_password = os.getenv(&#39;DATABASE_PASSWORD&#39;)<\/p>\n<p>app_debug = os.getenv(&#39;APPLICATION_DEBUG&#39;).lower() == &#39;true&#39;<\/p>\n<p>app_secret_key = os.getenv(&#39;APPLICATION_SECRET_KEY&#39;)<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u53c2\u6570\u9002\u7528\u4e8e\u9700\u8981\u7075\u6d3b\u914d\u7f6e\u7684\u573a\u666f\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u4f20\u9012\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86<code>argparse<\/code>\u6a21\u5757\u6765\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&#39;Project Configuration&#39;)<\/p>\n<p>parser.add_argument(&#39;--db-host&#39;, type=str, required=True, help=&#39;Database Host&#39;)<\/p>\n<p>parser.add_argument(&#39;--db-port&#39;, type=int, required=True, help=&#39;Database Port&#39;)<\/p>\n<p>parser.add_argument(&#39;--db-user&#39;, type=str, required=True, help=&#39;Database User&#39;)<\/p>\n<p>parser.add_argument(&#39;--db-password&#39;, type=str, required=True, help=&#39;Database Password&#39;)<\/p>\n<p>parser.add_argument(&#39;--app-debug&#39;, type=bool, required=True, help=&#39;Application Debug Mode&#39;)<\/p>\n<p>parser.add_argument(&#39;--app-secret-key&#39;, type=str, required=True, help=&#39;Application Secret Key&#39;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>db_host = args.db_host<\/p>\n<p>db_port = args.db_port<\/p>\n<p>db_user = args.db_user<\/p>\n<p>db_password = args.db_password<\/p>\n<p>app_debug = args.app_debug<\/p>\n<p>app_secret_key = args.app_secret_key<\/p>\n<p>print(f&quot;Database Host: {db_host}&quot;)<\/p>\n<p>print(f&quot;Database Port: {db_port}&quot;)<\/p>\n<p>print(f&quot;Application Debug Mode: {app_debug}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py --db-host localhost --db-port 3306 --db-user root --db-password root --app-debug true --app-secret-key your_secret_key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u80fd\u591f\u66f4\u597d\u5730\u7ba1\u7406\u548c\u7ef4\u62a4\u914d\u7f6e\uff0c\u5e38\u7528\u7684\u5de5\u5177\u6709<code>ConfigParser<\/code>\u3001<code>Cerberus<\/code>\u3001<code>Pydantic<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528ConfigParser<\/h3>\n<\/p>\n<p><p>ConfigParser\u662fPython\u6807\u51c6\u5e93\u63d0\u4f9b\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u914d\u7f6e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>a. \u5b89\u88c5ConfigParser<\/h4>\n<\/p>\n<p><p>ConfigParser\u662fPython\u6807\u51c6\u5e93\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>b. \u4f7f\u7528ConfigParser<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config[&#39;database&#39;] = {<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 3306,<\/p>\n<p>    &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>    &#39;password&#39;: &#39;root&#39;<\/p>\n<p>}<\/p>\n<p>config[&#39;application&#39;] = {<\/p>\n<p>    &#39;debug&#39;: True,<\/p>\n<p>    &#39;secret_key&#39;: &#39;your_secret_key&#39;<\/p>\n<p>}<\/p>\n<p>with open(&#39;config.ini&#39;, &#39;w&#39;) as configfile:<\/p>\n<p>    config.write(configfile)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Cerberus<\/h3>\n<\/p>\n<p><p>Cerberus\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u9a8c\u8bc1\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u914d\u7f6e\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><h4>a. \u5b89\u88c5Cerberus<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cerberus<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u4f7f\u7528Cerberus<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from cerberus import Validator<\/p>\n<p>schema = {<\/p>\n<p>    &#39;database&#39;: {<\/p>\n<p>        &#39;type&#39;: &#39;dict&#39;,<\/p>\n<p>        &#39;schema&#39;: {<\/p>\n<p>            &#39;host&#39;: {&#39;type&#39;: &#39;string&#39;, &#39;required&#39;: True},<\/p>\n<p>            &#39;port&#39;: {&#39;type&#39;: &#39;integer&#39;, &#39;required&#39;: True},<\/p>\n<p>            &#39;user&#39;: {&#39;type&#39;: &#39;string&#39;, &#39;required&#39;: True},<\/p>\n<p>            &#39;password&#39;: {&#39;type&#39;: &#39;string&#39;, &#39;required&#39;: True}<\/p>\n<p>        }<\/p>\n<p>    },<\/p>\n<p>    &#39;application&#39;: {<\/p>\n<p>        &#39;type&#39;: &#39;dict&#39;,<\/p>\n<p>        &#39;schema&#39;: {<\/p>\n<p>            &#39;debug&#39;: {&#39;type&#39;: &#39;boolean&#39;, &#39;required&#39;: True},<\/p>\n<p>            &#39;secret_key&#39;: {&#39;type&#39;: &#39;string&#39;, &#39;required&#39;: True}<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>config = {<\/p>\n<p>    &#39;database&#39;: {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;port&#39;: 3306,<\/p>\n<p>        &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;root&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;application&#39;: {<\/p>\n<p>        &#39;debug&#39;: True,<\/p>\n<p>        &#39;secret_key&#39;: &#39;your_secret_key&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>v = Validator(schema)<\/p>\n<p>if v.validate(config):<\/p>\n<p>    print(&quot;Configuration is valid&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Configuration is invalid&quot;)<\/p>\n<p>    print(v.errors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528Pydantic<\/h3>\n<\/p>\n<p><p>Pydantic\u662f\u4e00\u4e2a\u57fa\u4e8e\u6570\u636e\u6a21\u578b\u7684\u9a8c\u8bc1\u5e93\uff0c\u9002\u7528\u4e8e\u590d\u6742\u914d\u7f6e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>a. \u5b89\u88c5Pydantic<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pydantic<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>b. \u4f7f\u7528Pydantic<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pydantic import BaseModel, Field, ValidationError<\/p>\n<p>class DatabaseConfig(BaseModel):<\/p>\n<p>    host: str<\/p>\n<p>    port: int<\/p>\n<p>    user: str<\/p>\n<p>    password: str<\/p>\n<p>class ApplicationConfig(BaseModel):<\/p>\n<p>    debug: bool<\/p>\n<p>    secret_key: str<\/p>\n<p>class Config(BaseModel):<\/p>\n<p>    database: DatabaseConfig<\/p>\n<p>    application: ApplicationConfig<\/p>\n<p>config_data = {<\/p>\n<p>    &#39;database&#39;: {<\/p>\n<p>        &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>        &#39;port&#39;: 3306,<\/p>\n<p>        &#39;user&#39;: &#39;root&#39;,<\/p>\n<p>        &#39;password&#39;: &#39;root&#39;<\/p>\n<p>    },<\/p>\n<p>    &#39;application&#39;: {<\/p>\n<p>        &#39;debug&#39;: True,<\/p>\n<p>        &#39;secret_key&#39;: &#39;your_secret_key&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>try:<\/p>\n<p>    config = Config(config_data)<\/p>\n<p>    print(&quot;Configuration is valid&quot;)<\/p>\n<p>except ValidationError as e:<\/p>\n<p>    print(&quot;Configuration is invalid&quot;)<\/p>\n<p>    print(e.json())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u3001\u547d\u4ee4\u884c\u53c2\u6570\u548c\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u6765\u7ba1\u7406Python\u9879\u76ee\u914d\u7f6e\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u80fd\u591f\u5c06\u914d\u7f6e\u4e0e\u4ee3\u7801\u5206\u79bb\u3001\u4fbf\u4e8e\u7248\u672c\u63a7\u5236\u3001\u589e\u5f3a\u7075\u6d3b\u6027<\/strong>\uff0c\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u548c\u590d\u6742\u5ea6\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u914d\u7f6e\u7ba1\u7406\u7684\u7075\u6d3b\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u66f4\u6539\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u9879\u76ee\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f7f\u7528JSON\u3001YAML\u6216INI\u683c\u5f0f\u3002\u8981\u66f4\u6539\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5e76\u5c06\u73b0\u6709\u914d\u7f6e\u8f6c\u6362\u4e3a\u6240\u9700\u683c\u5f0f\u3002\u4f7f\u7528Python\u7684\u5185\u7f6e\u5e93\uff0c\u5982<code>json<\/code>\u3001<code>yaml<\/code>\u6216<code>configparser<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5199\u5165\u4e0d\u540c\u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u76f8\u5e94\u5730\u4fee\u6539\u8bfb\u53d6\u914d\u7f6e\u7684\u90e8\u5206\uff0c\u4ee5\u9002\u5e94\u65b0\u7684\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u52a8\u6001\u52a0\u8f7dPython\u9879\u76ee\u4e2d\u7684\u914d\u7f6e\uff1f<\/strong><br \/>\u52a8\u6001\u52a0\u8f7d\u914d\u7f6e\u610f\u5473\u7740\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6839\u636e\u9700\u8981\u66f4\u6539\u914d\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u547d\u4ee4\u884c\u53c2\u6570\u6216\u5916\u90e8\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\uff08\u5982dotenv\u6216configparser\uff09\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u786c\u7f16\u7801\u914d\u7f6e\u5728\u9879\u76ee\u4e2d\uff0c\u4f7f\u5176\u66f4\u52a0\u7075\u6d3b\u548c\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fddPython\u9879\u76ee\u7684\u914d\u7f6e\u5b89\u5168\uff1f<\/strong><br \/>\u4fdd\u62a4\u914d\u7f6e\u6587\u4ef6\u5c24\u5176\u91cd\u8981\uff0c\u7279\u522b\u662f\u5f53\u5b83\u4eec\u5305\u542b\u654f\u611f\u4fe1\u606f\uff08\u5982API\u5bc6\u94a5\u6216\u6570\u636e\u5e93\u51ed\u8bc1\uff09\u65f6\u3002\u53ef\u4ee5\u8003\u8651\u5c06\u654f\u611f\u4fe1\u606f\u5b58\u50a8\u5728\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002\u4f7f\u7528Python\u7684<code>os<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u914d\u7f6e\u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u6b63\u786e\uff0c\u907f\u514d\u672a\u6388\u6743\u7684\u8bbf\u95ee\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u9879\u76ee\u4e2d\uff0c\u914d\u7f6e\u7ba1\u7406\u5bf9\u4e8e\u9879\u76ee\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u547d\u4ee4\u884c\u53c2\u6570\u548c [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147351"}],"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=1147351"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1147351\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1147351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1147351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1147351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}