{"id":1027137,"date":"2024-12-31T10:48:33","date_gmt":"2024-12-31T02:48:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1027137.html"},"modified":"2024-12-31T10:48:36","modified_gmt":"2024-12-31T02:48:36","slug":"python%e8%84%9a%e6%9c%ac%e5%a6%82%e4%bd%95%e4%b8%80%e7%82%b9%e5%b0%b1%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1027137.html","title":{"rendered":"python\u811a\u672c\u5982\u4f55\u4e00\u70b9\u5c31\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/696cfcfa-4f3b-4dbb-b38e-b7283d46d8c4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u811a\u672c\u5982\u4f55\u4e00\u70b9\u5c31\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>Python\u811a\u672c\u5982\u4f55\u4e00\u70b9\u5c31\u8fd0\u884c\uff1a\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u3001\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5728Windows\u4e0a\u901a\u8fc7\u53cc\u51fb\u6765\u8fd0\u884cPython\u811a\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u3001\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177\u7b49\u65b9\u6cd5\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c<strong>\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06.py\u6587\u4ef6\u4e0ePython\u89e3\u91ca\u5668\u5173\u8054\uff0c\u53cc\u51fb.py\u6587\u4ef6\u5373\u53ef\u8fd0\u884cPython\u811a\u672c\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054<\/h3>\n<\/p>\n<p><p>\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u8bc6\u522b\u5e76\u81ea\u52a8\u4f7f\u7528Python\u89e3\u91ca\u5668\u6765\u8fd0\u884c.py\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python<\/strong>\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5\u4e86Python\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002\u5b89\u88c5\u65f6\uff0c\u52a1\u5fc5\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u6253\u5f00\u201c\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u5668\u201d\u3002<\/li>\n<li>\u627e\u5230\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\uff08\u4f8b\u5982 script.py\uff09\uff0c\u53f3\u952e\u70b9\u51fb\u8be5\u6587\u4ef6\u5e76\u9009\u62e9\u201c\u6253\u5f00\u65b9\u5f0f\u201d\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684\u7a97\u53e3\u4e2d\uff0c\u9009\u62e9\u201c\u9009\u62e9\u5176\u4ed6\u5e94\u7528\u201d\u3002<\/li>\n<li>\u627e\u5230\u5e76\u9009\u62e9\u201cPython\u201d\uff0c\u52fe\u9009\u201c\u59cb\u7ec8\u4f7f\u7528\u6b64\u5e94\u7528\u6253\u5f00 .py \u6587\u4ef6\u201d\uff0c\u7136\u540e\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8bbe\u7f6e\uff0c\u53cc\u51fb\u4efb\u4f55.py\u6587\u4ef6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u8c03\u7528Python\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u901a\u8fc7\u53cc\u51fb\u684c\u9762\u4e0a\u7684\u5feb\u6377\u65b9\u5f0f\u6765\u8fd0\u884cPython\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u627e\u5230Python\u811a\u672c<\/strong>\uff1a\u5728\u6587\u4ef6\u8d44\u6e90\u7ba1\u7406\u5668\u4e2d\u627e\u5230\u9700\u8981\u8fd0\u884c\u7684Python\u811a\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5feb\u6377\u65b9\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53f3\u952e\u70b9\u51fb\u8be5\u811a\u672c\u6587\u4ef6\uff0c\u9009\u62e9\u201c\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u201d\u3002<\/li>\n<li>\u5c06\u521b\u5efa\u7684\u5feb\u6377\u65b9\u5f0f\u62d6\u52a8\u5230\u684c\u9762\u4e0a\u6216\u4f60\u559c\u6b22\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f16\u8f91\u5feb\u6377\u65b9\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u53f3\u952e\u70b9\u51fb\u5feb\u6377\u65b9\u5f0f\u56fe\u6807\uff0c\u9009\u62e9\u201c\u5c5e\u6027\u201d\u3002<\/li>\n<li>\u5728\u201c\u5feb\u6377\u65b9\u5f0f\u201d\u9009\u9879\u5361\u4e2d\u7684\u201c\u76ee\u6807\u201d\u680f\uff0c\u8f93\u5165\u547d\u4ee4\uff1a<code>python script.py<\/code>\uff0c\u5176\u4e2d <code>script.py<\/code> \u662f\u4f60\u7684\u811a\u672c\u6587\u4ef6\u540d\uff08\u9700\u8981\u586b\u5199\u811a\u672c\u7684\u5b8c\u6574\u8def\u5f84\uff09\u3002<\/li>\n<li>\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u53cc\u51fb\u5feb\u6377\u65b9\u5f0f\u56fe\u6807\uff0c\u5373\u53ef\u8fd0\u884c\u5bf9\u5e94\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5c06Python\u811a\u672c\u6253\u5305\u6210\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08.exe\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177\uff0c\u5982PyInstaller\u3001cx_Freeze\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyInstaller\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5PyInstaller<\/strong>\uff1a\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PyInstaller\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5305Python\u811a\u672c<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\uff0c\u5bfc\u822a\u5230Python\u811a\u672c\u6240\u5728\u7684\u76ee\u5f55\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5c06\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a\n<pre><code class=\"language-shell\">pyinstaller --onefile script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8be5\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2adist\u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5305\u542b\u6253\u5305\u597d\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08script.exe\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6<\/strong>\uff1a\u53cc\u51fb\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5373\u53ef\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u786e\u4fdd\u811a\u672c\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u6216\u6253\u5305\u811a\u672c\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u517c\u5bb9\u6027\u68c0\u67e5\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6d4b\u8bd5\u811a\u672c<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8fd0\u884c\u811a\u672c\uff0c\u786e\u4fdd\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u6216\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5\u4f9d\u8d56\u9879<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u811a\u672c\u4f9d\u8d56\u4e8e\u7b2c\u4e09\u65b9\u5e93\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5b89\u88c5\u5728\u76ee\u6807\u73af\u5883\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7requirements.txt\u6587\u4ef6\u7ba1\u7406\u4f9d\u8d56\u9879\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8de8\u5e73\u53f0\u6d4b\u8bd5<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u811a\u672c\u9700\u8981\u5728\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff08\u4f8b\u5982Windows\u3001macOS\u3001Linux\uff09\uff0c\u786e\u4fdd\u5728\u6bcf\u4e2a\u7cfb\u7edf\u4e0a\u90fd\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u786e\u4fdd\u6ca1\u6709\u5e73\u53f0\u7279\u5b9a\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f9d\u8d56\u9879\u548c\u9694\u79bb\u9879\u76ee\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u548c\u4f7f\u7528\u865a\u62df\u73af\u5883\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-shell\">python -m venv venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8be5\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3avenv\u7684\u865a\u62df\u73af\u5883\u6587\u4ef6\u5939\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-shell\">venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5728macOS\u6216Linux\u7cfb\u7edf\u4e0a\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a\n<pre><code class=\"language-shell\">source venv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u4f9d\u8d56\u9879<\/strong>\uff1a\u5728\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u4f7f\u7528pip\u5b89\u88c5\u811a\u672c\u6240\u9700\u7684\u4f9d\u8d56\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u811a\u672c<\/strong>\uff1a\u5728\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u72b6\u6001\u4e0b\uff0c\u8fd0\u884cPython\u811a\u672c\uff0c\u786e\u4fdd\u4f9d\u8d56\u9879\u7684\u6b63\u786e\u6027\u548c\u73af\u5883\u7684\u9694\u79bb\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u5de5\u5177\uff08Task Scheduler\uff09\u6765\u5b9a\u65f6\u6216\u81ea\u52a8\u8fd0\u884cPython\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u4efb\u52a1\u8ba1\u5212\u5de5\u5177\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u5f00\u4efb\u52a1\u8ba1\u5212\u5de5\u5177<\/strong>\uff1a\u5728Windows\u5f00\u59cb\u83dc\u5355\u4e2d\uff0c\u641c\u7d22\u5e76\u6253\u5f00\u201c\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u65b0\u4efb\u52a1<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u4efb\u52a1\u8ba1\u5212\u5de5\u5177\u4e2d\uff0c\u70b9\u51fb\u201c\u521b\u5efa\u4efb\u52a1\u201d\u3002<\/li>\n<li>\u5728\u201c\u5e38\u89c4\u201d\u9009\u9879\u5361\u4e2d\uff0c\u8f93\u5165\u4efb\u52a1\u540d\u79f0\u548c\u63cf\u8ff0\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u89e6\u53d1\u5668<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u201c\u89e6\u53d1\u5668\u201d\u9009\u9879\u5361\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u3002<\/li>\n<li>\u9009\u62e9\u4efb\u52a1\u7684\u89e6\u53d1\u65f6\u95f4\u548c\u9891\u7387\uff0c\u4f8b\u5982\u6bcf\u5929\u8fd0\u884c\u3001\u6bcf\u5468\u8fd0\u884c\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728\u201c\u64cd\u4f5c\u201d\u9009\u9879\u5361\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u3002<\/li>\n<li>\u5728\u201c\u64cd\u4f5c\u201d\u4e0b\u62c9\u83dc\u5355\u4e2d\u9009\u62e9\u201c\u542f\u52a8\u7a0b\u5e8f\u201d\u3002<\/li>\n<li>\u5728\u201c\u7a0b\u5e8f\/\u811a\u672c\u201d\u680f\u4e2d\uff0c\u8f93\u5165Python\u89e3\u91ca\u5668\u7684\u8def\u5f84\uff0c\u4f8b\u5982 <code>C:\\Python39\\python.exe<\/code>\u3002<\/li>\n<li>\u5728\u201c\u6dfb\u52a0\u53c2\u6570\u201d\u680f\u4e2d\uff0c\u8f93\u5165Python\u811a\u672c\u7684\u8def\u5f84\uff0c\u4f8b\u5982 <code>C:\\path\\to\\script.py<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u4efb\u52a1<\/strong>\uff1a\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u4efb\u52a1\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4efb\u52a1\u8ba1\u5212\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u65f6\u95f4\u81ea\u52a8\u8fd0\u884cPython\u811a\u672c\uff0c\u65e0\u9700\u624b\u52a8\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u811a\u672c\u4e2d\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u914d\u7f6e\u8fd0\u884c\u65f6\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u6253\u5f00\u201c\u63a7\u5236\u9762\u677f\u201d\uff0c\u9009\u62e9\u201c\u7cfb\u7edf\u548c\u5b89\u5168\u201d &gt; \u201c\u7cfb\u7edf\u201d &gt; \u201c\u9ad8\u7ea7\u7cfb\u7edf\u8bbe\u7f6e\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/li>\n<li>\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u6216\u201c\u7528\u6237\u53d8\u91cf\u201d\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\u3002<\/li>\n<li>\u8f93\u5165\u53d8\u91cf\u540d\u548c\u53d8\u91cf\u503c\uff0c\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5728Python\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os.environ<\/code> \u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-python\">import os<\/p>\n<p>my_var = os.environ.get(&#39;MY_VAR&#39;)<\/p>\n<p>print(f&#39;MY_VAR: {my_var}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u548c\u4f7f\u7528\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7075\u6d3b\u914d\u7f6e\u811a\u672c\u53c2\u6570\uff0c\u907f\u514d\u786c\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u3001\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0Python\u811a\u672c\u7684\u4e00\u70b9\u5373\u8fd0\u884c\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u8fd8\u9700\u8981\u8003\u8651\u811a\u672c\u7684\u517c\u5bb9\u6027\u3001\u4f9d\u8d56\u9879\u7ba1\u7406\u548c\u73af\u5883\u914d\u7f6e\uff0c\u786e\u4fdd\u811a\u672c\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u901a\u8fc7\u4f7f\u7528\u865a\u62df\u73af\u5883\u3001\u4efb\u52a1\u8ba1\u5212\u5de5\u5177\u548c\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u811a\u672c\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u7075\u6d3b\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u8bbe\u7f6e\u4e3a\u53cc\u51fb\u5373\u53ef\u8fd0\u884c\uff1f<\/strong><br \/>\u8981\u8ba9Python\u811a\u672c\u80fd\u591f\u901a\u8fc7\u53cc\u51fb\u8fd0\u884c\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u6587\u4ef6\u540e\u7f00\u540d\u66f4\u6539\u4e3a <code>.py<\/code>\uff0c\u786e\u4fdd\u5728\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86Python\u89e3\u91ca\u5668\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\uff08Windows\u73af\u5883\uff09\u6216\u4f7f\u7528Shell\u811a\u672c\uff08Linux\/Mac\u73af\u5883\uff09\u6765\u8c03\u7528Python\u811a\u672c\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u53cc\u51fb\u8be5\u6279\u5904\u7406\u6216Shell\u811a\u672c\u6587\u4ef6\u5373\u53ef\u6267\u884c\u60a8\u7684Python\u4ee3\u7801\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 <code>PyInstaller<\/code> \u6216 <code>cx_Freeze<\/code> \u5c06Python\u811a\u672c\u6253\u5305\u4e3a\u72ec\u7acb\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u8fd9\u4e9b\u5de5\u5177\u4f1a\u5c06Python\u89e3\u91ca\u5668\u548c\u6240\u6709\u4f9d\u8d56\u9879\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u4f7f\u5f97\u7528\u6237\u65e0\u9700\u5b89\u88c5Python\u73af\u5883\u5373\u53ef\u8fd0\u884c\u7a0b\u5e8f\u3002\u6253\u5305\u540e\uff0c\u7528\u6237\u53ea\u9700\u53cc\u51fb\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5373\u53ef\u542f\u52a8\u811a\u672c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Windows\u4e0a\u8bbe\u7f6ePython\u73af\u5883\u53d8\u91cf\u4ee5\u4fbf\u4e8e\u8fd0\u884c\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Windows\u4e0a\u8bbe\u7f6ePython\u73af\u5883\u53d8\u91cf\uff0c\u60a8\u9700\u8981\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u5728\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u4e2d\uff0c\u9009\u62e9\u201c\u73af\u5883\u53d8\u91cf\u201d\uff0c\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\u627e\u5230\u201cPath\u201d\uff0c\u5e76\u7f16\u8f91\u5b83\u4ee5\u6dfb\u52a0Python\u7684\u5b89\u88c5\u76ee\u5f55\u548cScripts\u76ee\u5f55\u3002\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u4efb\u610f\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\u4e2d\u76f4\u63a5\u8fd0\u884cPython\u811a\u672c\uff0c\u800c\u65e0\u9700\u6bcf\u6b21\u90fd\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u811a\u672c\u5982\u4f55\u4e00\u70b9\u5c31\u8fd0\u884c\uff1a\u8bbe\u7f6e\u6587\u4ef6\u5173\u8054\u3001\u521b\u5efa\u5feb\u6377\u65b9\u5f0f\u3001\u4f7f\u7528\u811a\u672c\u6253\u5305\u5de5\u5177 \u5982\u679c\u4f60\u5e0c\u671b\u5728Windows\u4e0a\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1027144,"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\/1027137"}],"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=1027137"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027137\/revisions"}],"predecessor-version":[{"id":1027146,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1027137\/revisions\/1027146"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1027144"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1027137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1027137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1027137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}