{"id":1175079,"date":"2025-01-15T17:26:54","date_gmt":"2025-01-15T09:26:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175079.html"},"modified":"2025-01-15T17:26:57","modified_gmt":"2025-01-15T09:26:57","slug":"python%e5%a6%82%e4%bd%95%e8%a3%85%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1175079.html","title":{"rendered":"python\u5982\u4f55\u88c5\u73af\u5883\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110628\/d7039fda-dbff-42df-8ad5-b16e0023bc7f.webp\" alt=\"python\u5982\u4f55\u88c5\u73af\u5883\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>Python\u88c5\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001dotenv\u6a21\u5757\u3001\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u7cfb\u7edf\u8bbe\u7f6e\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528os\u6a21\u5757<\/strong>\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7<code>os.environ<\/code>\u6765\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002<strong>\u4f7f\u7528dotenv\u6a21\u5757<\/strong>\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u548c\u7ba1\u7406<code>.env<\/code>\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u3002<strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/strong>\u5219\u53ef\u4ee5\u5c06\u73af\u5883\u53d8\u91cf\u5199\u5165\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u8bfb\u53d6\u65f6\u8fdb\u884c\u89e3\u6790\u3002<strong>\u901a\u8fc7\u7cfb\u7edf\u8bbe\u7f6e<\/strong>\u5219\u662f\u76f4\u63a5\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u6240\u6709\u8fd0\u884c\u7684\u7a0b\u5e8f\u90fd\u6709\u6548\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528os\u6a21\u5757\u6765\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7os\u6a21\u5757\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5bfc\u5165os\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>os.environ<\/code>\u5b57\u5178\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>os.environ[&#39;MY_VARIABLE&#39;] = &#39;some_value&#39;<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = os.getenv(&#39;MY_VARIABLE&#39;)<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528os\u6a21\u5757\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>os\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u5e93\u6a21\u5757\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7os\u6a21\u5757\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u8981\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u901a\u8fc7os.environ\u5b57\u5178\u6765\u5b9e\u73b0\uff0cos.environ\u662f\u4e00\u4e2a\u5305\u542b\u73af\u5883\u53d8\u91cf\u7684\u5b57\u5178\uff0c\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>os.environ[&#39;MY_VARIABLE&#39;] = &#39;some_value&#39;<\/p>\n<h2><strong>\u9a8c\u8bc1\u73af\u5883\u53d8\u91cf\u662f\u5426\u8bbe\u7f6e\u6210\u529f<\/strong><\/h2>\n<p>print(os.environ[&#39;MY_VARIABLE&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>os.environ[&#39;MY_VARIABLE&#39;] = &#39;some_value&#39;<\/code>\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u540d\u4e3aMY_VARIABLE\uff0c\u503c\u4e3asome_value\u7684\u73af\u5883\u53d8\u91cf\u3002\u7136\u540e\u4f7f\u7528<code>print(os.environ[&#39;MY_VARIABLE&#39;])<\/code>\u9a8c\u8bc1\u73af\u5883\u53d8\u91cf\u662f\u5426\u8bbe\u7f6e\u6210\u529f\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528os.getenv()\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = os.getenv(&#39;MY_VARIABLE&#39;)<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>os.getenv(&#39;MY_VARIABLE&#39;)<\/code>\u83b7\u53d6\u4e86\u540d\u4e3aMY_VARIABLE\u7684\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cfmy_var\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528dotenv\u6a21\u5757\u52a0\u8f7d\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>dotenv\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u548c\u7ba1\u7406<code>.env<\/code>\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5python-dotenv\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-dotenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1\u3001\u521b\u5efa.env\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>.env<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>MY_VARIABLE=some_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u52a0\u8f7d.env\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\u52a0\u8f7d<code>.env<\/code>\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dotenv import load_dotenv<\/p>\n<p>import os<\/p>\n<h2><strong>\u52a0\u8f7d.env\u6587\u4ef6<\/strong><\/h2>\n<p>load_dotenv()<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = os.getenv(&#39;MY_VARIABLE&#39;)<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>from dotenv import load_dotenv<\/code>\u5bfc\u5165\u4e86load_dotenv\u51fd\u6570\uff0c\u7136\u540e\u901a\u8fc7<code>load_dotenv()<\/code>\u52a0\u8f7d\u4e86<code>.env<\/code>\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u3002\u6700\u540e\u901a\u8fc7<code>os.getenv(&#39;MY_VARIABLE&#39;)<\/code>\u83b7\u53d6\u4e86\u540d\u4e3aMY_VARIABLE\u7684\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7ba1\u7406\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4f7f\u7528dotenv\u6a21\u5757\u52a0\u8f7d<code>.env<\/code>\u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\u6765\u7ba1\u7406\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aconfig.py\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>MY_VARIABLE = &#39;some_value&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165config.py\u6587\u4ef6\uff0c\u5e76\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import config<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = config.MY_VARIABLE<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>import config<\/code>\u5bfc\u5165\u4e86config.py\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>config.MY_VARIABLE<\/code>\u83b7\u53d6\u4e86\u540d\u4e3aMY_VARIABLE\u7684\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u901a\u8fc7\u7cfb\u7edf\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u6240\u4e0d\u540c\u3002\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u5728Windows\u548cLinux\/Mac\u4e0b\u5982\u4f55\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u5728Windows\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5728Windows\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u53f3\u952e\u70b9\u51fb\u201c\u8ba1\u7b97\u673a\u201d\u56fe\u6807\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002<\/li>\n<li>\u5728\u5de6\u4fa7\u70b9\u51fb\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u6216\u201c\u7528\u6237\u53d8\u91cf\u201d\u4e2d\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u6309\u94ae\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\u8f93\u5165\u53d8\u91cf\u540d\u548c\u53d8\u91cf\u503c\uff0c\u7136\u540e\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u6309\u94ae\u3002<\/li>\n<\/ol>\n<p><p>\u4f8b\u5982\uff0c\u8981\u8bbe\u7f6e\u540d\u4e3aMY_VARIABLE\uff0c\u503c\u4e3asome_value\u7684\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u201c\u53d8\u91cf\u540d\u201d\u4e2d\u8f93\u5165MY_VARIABLE\uff0c\u5728\u201c\u53d8\u91cf\u503c\u201d\u4e2d\u8f93\u5165some_value\uff0c\u7136\u540e\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u6309\u94ae\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u5728Linux\/Mac\u4e0b\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5728Linux\/Mac\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<del>\/.bashrc\u6216<\/del>\/.zshrc\u6587\u4ef6\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728<del>\/.bashrc\u6216<\/del>\/.zshrc\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export MY_VARIABLE=some_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u4fee\u6539\u751f\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.zshrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>export MY_VARIABLE=some_value<\/code>\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u540d\u4e3aMY_VARIABLE\uff0c\u503c\u4e3asome_value\u7684\u73af\u5883\u53d8\u91cf\u3002\u7136\u540e\u901a\u8fc7<code>source ~\/.bashrc<\/code>\u6216<code>source ~\/.zshrc<\/code>\u547d\u4ee4\u4f7f\u4fee\u6539\u751f\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u73af\u5883\uff08\u5982\u5f00\u53d1\u73af\u5883\u3001\u6d4b\u8bd5\u73af\u5883\u548c\u751f\u4ea7\u73af\u5883\uff09\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u73af\u5883\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u7ba1\u7406\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528\u4e0d\u540c\u7684.env\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u73af\u5883\u521b\u5efa\u4e0d\u540c\u7684<code>.env<\/code>\u6587\u4ef6\uff0c\u5e76\u5728\u52a0\u8f7d\u65f6\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4ee5\u4e0b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>.env.development<\/p>\n<p>.env.testing<\/p>\n<p>.env.production<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u52a0\u8f7d\u65f6\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dotenv import load_dotenv<\/p>\n<p>import os<\/p>\n<h2><strong>\u52a0\u8f7d\u4e0d\u540c\u73af\u5883\u4e0b\u7684.env\u6587\u4ef6<\/strong><\/h2>\n<p>if os.getenv(&#39;ENV&#39;) == &#39;development&#39;:<\/p>\n<p>    load_dotenv(&#39;.env.development&#39;)<\/p>\n<p>elif os.getenv(&#39;ENV&#39;) == &#39;testing&#39;:<\/p>\n<p>    load_dotenv(&#39;.env.testing&#39;)<\/p>\n<p>elif os.getenv(&#39;ENV&#39;) == &#39;production&#39;:<\/p>\n<p>    load_dotenv(&#39;.env.production&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = os.getenv(&#39;MY_VARIABLE&#39;)<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>os.getenv(&#39;ENV&#39;)<\/code>\u83b7\u53d6\u5f53\u524d\u73af\u5883\uff0c\u7136\u540e\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684<code>.env<\/code>\u6587\u4ef6\u5e76\u52a0\u8f7d\u3002\u6700\u540e\u901a\u8fc7<code>os.getenv(&#39;MY_VARIABLE&#39;)<\/code>\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7684\u73af\u5883\u521b\u5efa\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u5728\u52a0\u8f7d\u65f6\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4ee5\u4e0b\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>config_development.py<\/p>\n<p>config_testing.py<\/p>\n<p>config_production.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u52a0\u8f7d\u65f6\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u6839\u636e\u73af\u5883\u52a0\u8f7d\u4e0d\u540c\u7684\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>if os.getenv(&#39;ENV&#39;) == &#39;development&#39;:<\/p>\n<p>    import config_development as config<\/p>\n<p>elif os.getenv(&#39;ENV&#39;) == &#39;testing&#39;:<\/p>\n<p>    import config_testing as config<\/p>\n<p>elif os.getenv(&#39;ENV&#39;) == &#39;production&#39;:<\/p>\n<p>    import config_production as config<\/p>\n<h2><strong>\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p>my_var = config.MY_VARIABLE<\/p>\n<p>print(my_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>os.getenv(&#39;ENV&#39;)<\/code>\u83b7\u53d6\u5f53\u524d\u73af\u5883\uff0c\u7136\u540e\u6839\u636e\u73af\u5883\u9009\u62e9\u76f8\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\u5e76\u5bfc\u5165\u3002\u6700\u540e\u901a\u8fc7<code>config.MY_VARIABLE<\/code>\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u65f6\u5e94\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h2>1\u3001\u907f\u514d\u5c06\u654f\u611f\u4fe1\u606f\u786c\u7f16\u7801<\/h2>\n<\/p>\n<p><p>\u5e94\u907f\u514d\u5c06\u654f\u611f\u4fe1\u606f\uff08\u5982\u6570\u636e\u5e93\u5bc6\u7801\u3001API\u5bc6\u94a5\u7b49\uff09\u786c\u7f16\u7801\u5728\u4ee3\u7801\u4e2d\u3002\u76f8\u53cd\uff0c\u5e94\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5b58\u50a8\u5728\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u636e\u5e93\u5bc6\u7801<\/strong><\/h2>\n<p>db_password = os.getenv(&#39;DB_PASSWORD&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5e94\u63d0\u4f9b\u4e00\u4e2a\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6\uff0c\u4f9b\u5f00\u53d1\u4eba\u5458\u53c2\u8003\u548c\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>.env.example<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5217\u51fa\u6240\u6709\u9700\u8981\u7684\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code>DB_HOST=localhost<\/p>\n<p>DB_USER=user<\/p>\n<p>DB_PASSWORD=password<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u6839\u636e\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6\u521b\u5efa\u81ea\u5df1\u7684<code>.env<\/code>\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cp .env.example .env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u7248\u672c\u63a7\u5236\u5ffd\u7565\u5b9e\u9645\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5e94\u5c06\u5b9e\u9645\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>.env<\/code>\u6587\u4ef6\uff09\u6dfb\u52a0\u5230\u7248\u672c\u63a7\u5236\u7cfb\u7edf\u7684\u5ffd\u7565\u5217\u8868\u4e2d\uff0c\u907f\u514d\u5c06\u654f\u611f\u4fe1\u606f\u63d0\u4ea4\u5230\u4ee3\u7801\u4ed3\u5e93\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728<code>.gitignore<\/code>\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>.env<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u88c5\u73af\u5883\u53d8\u91cf\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001dotenv\u6a21\u5757\u3001\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u7cfb\u7edf\u8bbe\u7f6e\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7ba1\u7406\u9879\u76ee\u4e2d\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u65f6\u5e94\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u907f\u514d\u5c06\u654f\u611f\u4fe1\u606f\u786c\u7f16\u7801\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u914d\u7f6e\u6587\u4ef6\u4f9b\u5f00\u53d1\u4eba\u5458\u53c2\u8003\u548c\u4f7f\u7528\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Windows\u7cfb\u7edf\u4e2d\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u8bbe\u7f6ePython\u7684\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\uff1a  <\/p>\n<ol>\n<li>\u627e\u5230Python\u5b89\u88c5\u8def\u5f84\uff0c\u901a\u5e38\u662f<code>C:\\PythonXX<\/code>\uff08XX\u8868\u793a\u7248\u672c\u53f7\uff09\u3002  <\/li>\n<li>\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u6216\u201c\u8ba1\u7b97\u673a\u201d\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002  <\/li>\n<li>\u8fdb\u5165\u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002  <\/li>\n<li>\u5728\u7cfb\u7edf\u53d8\u91cf\u4e2d\u627e\u5230\u201cPath\u201d\uff0c\u9009\u62e9\u540e\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002  <\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9\u201c\u65b0\u5efa\u201d\uff0c\u7136\u540e\u8f93\u5165Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u66f4\u6539\u3002  <\/li>\n<li>\u91cd\u65b0\u542f\u52a8\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u8f93\u5165<code>python<\/code>\u547d\u4ee4\u4ee5\u786e\u8ba4\u8bbe\u7f6e\u6210\u529f\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u5728macOS\u6216Linux\u4e0a\u914d\u7f6ePython\u73af\u5883\u53d8\u91cf\uff1f<\/strong><br \/>\u5728macOS\u6216Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u8f91Shell\u914d\u7f6e\u6587\u4ef6\u6765\u8bbe\u7f6ePython\u73af\u5883\u53d8\u91cf\uff1a  <\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\uff0c\u8f93\u5165<code>nano ~\/.bash_profile<\/code>\uff08\u6216<code>~\/.bashrc<\/code>\uff0c\u53d6\u51b3\u4e8e\u4f7f\u7528\u7684Shell\uff09\u3002  <\/li>\n<li>\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5982\u4e0b\u884c\uff1a<code>export PATH=&quot;\/usr\/local\/bin\/python3:$PATH&quot;<\/code>\uff0c\u786e\u4fdd\u8def\u5f84\u6307\u5411Python\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002  <\/li>\n<li>\u4fdd\u5b58\u5e76\u9000\u51fa\u7f16\u8f91\u5668\u3002  <\/li>\n<li>\u8f93\u5165<code>source ~\/.bash_profile<\/code>\uff08\u6216<code>~\/.bashrc<\/code>\uff09\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002  <\/li>\n<li>\u8f93\u5165<code>python3<\/code>\u547d\u4ee4\u4ee5\u786e\u8ba4Python\u662f\u5426\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u3002<\/li>\n<\/ol>\n<p><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1Python\u662f\u5426\u914d\u7f6e\u6210\u529f\uff1f<\/strong><br \/>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u9a8c\u8bc1Python\u662f\u5426\u5df2\u6b63\u786e\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff1a  <\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u67e5\u770b\u8f93\u51fa\u7684Python\u7248\u672c\u4fe1\u606f\u3002  <\/li>\n<li>\u8fd0\u884c<code>which python<\/code>\u6216<code>which python3<\/code>\u547d\u4ee4\uff0c\u786e\u8ba4\u8fd4\u56de\u7684\u8def\u5f84\u4e0e\u5b89\u88c5\u8def\u5f84\u4e00\u81f4\u3002  <\/li>\n<li>\u8bd5\u7740\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u4f8b\u5982<code>print(&quot;Hello, World!&quot;)<\/code>\uff0c\u67e5\u770b\u662f\u5426\u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u3002<br \/>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fddPython\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u6210\u529f\u5e76\u80fd\u591f\u987a\u5229\u4f7f\u7528Python\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Python\u88c5\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528os\u6a21\u5757\u3001dotenv\u6a21\u5757\u3001\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u7cfb\u7edf\u8bbe\u7f6e\u7b49\u3002 \u4f7f\u7528os\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1175081,"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\/1175079"}],"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=1175079"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175079\/revisions"}],"predecessor-version":[{"id":1175084,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175079\/revisions\/1175084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1175081"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}