{"id":1112915,"date":"2025-01-08T17:42:57","date_gmt":"2025-01-08T09:42:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112915.html"},"modified":"2025-01-08T17:43:00","modified_gmt":"2025-01-08T09:43:00","slug":"%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%aepy%e6%96%87%e4%bb%b6%e7%94%a8python3%e8%bf%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112915.html","title":{"rendered":"\u5982\u4f55\u8bbe\u7f6epy\u6587\u4ef6\u7528python3\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074733\/9dedbecf-fe5f-49b9-bd9d-60b1a62b4e92.webp\" alt=\"\u5982\u4f55\u8bbe\u7f6epy\u6587\u4ef6\u7528python3\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u8981\u8bbe\u7f6epy\u6587\u4ef6\u7528python3\u8fd0\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0shebang\u3001\u4e3apython3\u8bbe\u7f6e\u9ed8\u8ba4\u89e3\u91ca\u5668\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u6307\u5b9a\u89e3\u91ca\u5668\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/strong>\u6700\u4e3a\u7b80\u4fbf\u7684\u65b9\u6cd5\u662f\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0shebang\uff0c\u5373\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0<code>#!\/usr\/bin\/env python3<\/code>\u3002\u8fd9\u6837\uff0c\u5f53\u6267\u884c\u811a\u672c\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u8c03\u7528Python 3\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<p><p>Python 3 \u662f\u73b0\u4ee3Python\u7f16\u7a0b\u7684\u6807\u51c6\u7248\u672c\uff0c\u8bb8\u591a\u65b0\u529f\u80fd\u548c\u6539\u8fdb\u90fd\u5728Python 3\u4e2d\u5b9e\u73b0\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4f60\u7684\u811a\u672c\u4f7f\u7528Python 3\u8fd0\u884c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u6765\u8bbe\u7f6epy\u6587\u4ef6\u4f7f\u7528Python 3\u8fd0\u884c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u9ad8\u7ea7\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0shebang<\/strong><\/h2>\n<p><p>\u5728Python\u811a\u672c\u7684\u7b2c\u4e00\u884c\u6dfb\u52a0shebang\uff0c\u6307\u5b9a\u4f7f\u7528Python 3\u89e3\u91ca\u5668\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u8ba9\u811a\u672c\u5728Unix\u7cfb\u7edf\u4e0a\u76f4\u63a5\u8fd0\u884c\uff0c\u800c\u65e0\u9700\u663e\u5f0f\u8c03\u7528\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">#!\/usr\/bin\/env python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u884c\u6dfb\u52a0\u5230\u4f60\u7684Python\u6587\u4ef6\uff08*.py\uff09\u7684\u7b2c\u4e00\u884c\u3002\u8fd9\u6837\uff0c\u5f53\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u811a\u672c\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4f7f\u7528Python 3\u89e3\u91ca\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ .\/myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4e3aPython 3\u8bbe\u7f6e\u9ed8\u8ba4\u89e3\u91ca\u5668<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e2d\uff0c\u9ed8\u8ba4\u7684Python\u89e3\u91ca\u5668\u53ef\u80fd\u4ecd\u7136\u662fPython 2\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06Python 3\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u67e5\u770b\u7cfb\u7edf\u4e2d\u5b89\u88c5\u7684Python\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ python --version<\/p>\n<p>$ python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cPython 3\u5df2\u5b89\u88c5\uff0c\u4f60\u5c06\u770b\u5230\u7c7b\u4f3c\u4e8e<code>Python 3.x.x<\/code>\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u65b0\u7cfb\u7edf\u7684\u9ed8\u8ba4Python\u89e3\u91ca\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u5728Unix\u7cfb\u7edf\uff08\u5982Linux\u6216macOS\uff09\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u66f4\u65b0\u7b26\u53f7\u94fe\u63a5\u6765\u5c06Python 3\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ sudo ln -sf \/usr\/bin\/python3 \/usr\/bin\/python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u7cfb\u7edf\u4e2d\u5176\u4ed6\u4f9d\u8d56\u4e8ePython 2\u7684\u7a0b\u5e8f\uff0c\u56e0\u6b64\u8bf7\u8c28\u614e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u6307\u5b9a\u89e3\u91ca\u5668<\/strong><\/h2>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u663e\u5f0f\u8c03\u7528Python 3\u89e3\u91ca\u5668\u6765\u8fd0\u884c\u811a\u672c\uff0c\u800c\u65e0\u9700\u4fee\u6539\u811a\u672c\u672c\u8eab\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ python3 myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u63a5\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u5bf9\u811a\u672c\u8fdb\u884c\u4efb\u4f55\u4fee\u6539\u3002\u4f60\u53ea\u9700\u786e\u4fdd\u7cfb\u7edf\u4e2d\u7684<code>python3<\/code>\u547d\u4ee4\u6307\u5411\u6b63\u786e\u7684Python 3\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/h2>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u786e\u4fddPython 3\u89e3\u91ca\u5668\u5728\u7cfb\u7edf\u8def\u5f84\u4e2d\uff0c\u5e76\u4e14\u4f18\u5148\u4e8ePython 2\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5728Unix\u7cfb\u7edf\u4e0a\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u8f91shell\u914d\u7f6e\u6587\u4ef6\uff08\u4f8b\u5982<code>.bashrc<\/code>\u3001<code>.bash_profile<\/code>\u6216<code>.zshrc<\/code>\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">export PATH=&quot;\/path\/to\/python3\/bin:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06<code>\/path\/to\/python3\/bin<\/code>\u66ff\u6362\u4e3aPython 3\u89e3\u91ca\u5668\u6240\u5728\u7684\u5b9e\u9645\u8def\u5f84\u3002\u4fdd\u5b58\u6587\u4ef6\u5e76\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728Windows\u7cfb\u7edf\u4e0a\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u6253\u5f00\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u5bf9\u8bdd\u6846\uff0c\u9009\u62e9\u201c\u9ad8\u7ea7\u201d\u9009\u9879\u5361\uff0c\u70b9\u51fb\u201c\u73af\u5883\u53d8\u91cf\u201d\u6309\u94ae\u3002\u5728\u201c\u7cfb\u7edf\u53d8\u91cf\u201d\u90e8\u5206\uff0c\u627e\u5230<code>Path<\/code>\u53d8\u91cf\u5e76\u7f16\u8f91\uff0c\u6dfb\u52a0Python 3\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code>C:\\Python39<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u70b9\u51fb\u201c\u786e\u5b9a\u201d\u4fdd\u5b58\u66f4\u6539\uff0c\u7136\u540e\u91cd\u65b0\u542f\u52a8\u547d\u4ee4\u884c\u7a97\u53e3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p><p>\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u786e\u4fdd\u6bcf\u4e2a\u9879\u76ee\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684Python\u89e3\u91ca\u5668\u548c\u5305\u3002\u8fd9\u662f\u7ba1\u7406Python\u9879\u76ee\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ python3 -m venv venv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>venv<\/code>\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542bPython 3\u89e3\u91ca\u5668\u548c\u5305\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u4ee5\u4f7f\u7528\u5176\u4e2d\u7684Python 3\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5728Unix\u7cfb\u7edf\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ source venv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728Windows\u7cfb\u7edf\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u6fc0\u6d3b\u540e\uff0c\u4f60\u5c06\u770b\u5230\u547d\u4ee4\u884c\u63d0\u793a\u7b26\u53d1\u751f\u53d8\u5316\uff0c\u663e\u793a\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u8fd9\u8868\u793a\u4f60\u73b0\u5728\u6b63\u5728\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e2d\u7684Python 3\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u4f9d\u8d56\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684\u4f9d\u8d56\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ pip install -r requirements.txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u9879\u76ee\u4f7f\u7528\u7279\u5b9a\u7248\u672c\u7684\u5305\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u7cfb\u7edf\u4e2d\u7684\u5176\u4ed6\u9879\u76ee\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u4ee3\u7801\u7f16\u8f91\u5668\u6216IDE\u914d\u7f6ePython\u89e3\u91ca\u5668<\/strong><\/h2>\n<p><p>\u5927\u591a\u6570\u73b0\u4ee3\u4ee3\u7801\u7f16\u8f91\u5668\u548c\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u90fd\u5141\u8bb8\u4f60\u914d\u7f6ePython\u89e3\u91ca\u5668\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u6216\u8c03\u8bd5\u4ee3\u7801\u65f6\u4f7f\u7528\u6307\u5b9a\u7684\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>VS Code<\/strong><\/p>\n<\/p>\n<p><p>\u5728VS Code\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6ePython\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u9762\u677f\uff08<code>Ctrl+Shift+P<\/code>\u6216<code>Cmd+Shift+P<\/code>\uff09\u3002<\/li>\n<li>\u8f93\u5165\u5e76\u9009\u62e9\u201cPython: Select Interpreter\u201d\u3002<\/li>\n<li>\u9009\u62e9Python 3\u89e3\u91ca\u5668\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>PyCharm<\/strong><\/p>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u914d\u7f6ePython\u89e3\u91ca\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u201cSettings\u201d\u5bf9\u8bdd\u6846\uff08<code>Ctrl+Alt+S<\/code>\uff09\u3002<\/li>\n<li>\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\u4e2d\u9009\u62e9\u201cProject: <project_name>\u201d &gt; \u201cPython Interpreter\u201d\u3002<\/li>\n<li>\u70b9\u51fb\u53f3\u4fa7\u7684\u9f7f\u8f6e\u56fe\u6807\uff0c\u9009\u62e9\u201cAdd&#8230;\u201d\uff0c\u7136\u540e\u9009\u62e9Python 3\u89e3\u91ca\u5668\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5176\u4ed6\u7f16\u8f91\u5668\u548cIDE<\/strong><\/p>\n<\/p>\n<p><p>\u5927\u591a\u6570\u5176\u4ed6\u4ee3\u7801\u7f16\u8f91\u5668\u548cIDE\u4e5f\u63d0\u4f9b\u7c7b\u4f3c\u7684\u914d\u7f6e\u9009\u9879\u3002\u8bf7\u67e5\u9605\u76f8\u5e94\u7684\u6587\u6863\u4ee5\u4e86\u89e3\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u4e03\u3001\u4f7f\u7528Docker\u914d\u7f6ePython\u73af\u5883<\/strong><\/h2>\n<p><p>Docker\u662f\u4e00\u79cd\u5bb9\u5668\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Docker\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542bPython 3\u89e3\u91ca\u5668\u7684\u5bb9\u5668\uff0c\u4ee5\u786e\u4fdd\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5728\u4efb\u4f55\u5e73\u53f0\u4e0a\u90fd\u80fd\u4e00\u81f4\u8fd0\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaDockerfile<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>Dockerfile<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-dockerfile\">FROM python:3.9-slim<\/p>\n<p>WORKDIR \/app<\/p>\n<p>COPY . \/app<\/p>\n<p>RUN pip install --no-cache-dir -r requirements.txt<\/p>\n<p>CMD [&quot;python&quot;, &quot;myscript.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aDockerfile\u5c06\u4f7f\u7528Python 3.9-slim\u57fa\u7840\u955c\u50cf\uff0c\u590d\u5236\u9879\u76ee\u6587\u4ef6\uff0c\u5b89\u88c5\u4f9d\u8d56\u5305\uff0c\u5e76\u8bbe\u7f6e\u9ed8\u8ba4\u547d\u4ee4\u4ee5\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6784\u5efaDocker\u955c\u50cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4ee5\u6784\u5efaDocker\u955c\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ docker build -t my-python-app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my-python-app<\/code>\u7684Docker\u955c\u50cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cDocker\u5bb9\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ docker run --rm my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u542f\u52a8\u4e00\u4e2a\u5305\u542bPython 3\u89e3\u91ca\u5668\u7684\u5bb9\u5668\uff0c\u5e76\u8fd0\u884c\u4f60\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<h2><strong>\u516b\u3001\u4f7f\u7528Makefile\u7b80\u5316\u547d\u4ee4<\/strong><\/h2>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Makefile\u6765\u7b80\u5316\u8fd0\u884cPython\u811a\u672c\u7684\u547d\u4ee4\u3002Makefile\u662f\u4e00\u79cd\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u9879\u76ee\u7684\u6784\u5efa\u548c\u8fd0\u884c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaMakefile<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>Makefile<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-makefile\">.PHONY: run<\/p>\n<p>run:<\/p>\n<p>    python3 myscript.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a<code>run<\/code>\u7684\u76ee\u6807\uff0c\u5e76\u6307\u5b9a\u4f7f\u7528Python 3\u89e3\u91ca\u5668\u8fd0\u884c\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cMakefile\u76ee\u6807<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u4ee5\u6267\u884cMakefile\u76ee\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">$ make run<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f7f\u7528Python 3\u89e3\u91ca\u5668\u8fd0\u884c\u4f60\u7684\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u4f7f\u7528Python 3\u89e3\u91ca\u5668\u8fd0\u884c\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u73af\u5883\uff0c\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u914d\u7f6e\u548c\u7ba1\u7406Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Windows\u4e0a\u8bbe\u7f6e.py\u6587\u4ef6\u4ee5\u4f7f\u7528Python 3\u8fd0\u884c\uff1f<\/strong><br \/>\u5728Windows\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8bbe\u7f6e.py\u6587\u4ef6\u4f7f\u7528Python 3\u8fd0\u884c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5Python 3\uff0c\u5e76\u5728\u73af\u5883\u53d8\u91cf\u4e2d\u6b63\u786e\u914d\u7f6e\u4e86Python\u7684\u8def\u5f84\u3002\u63a5\u7740\uff0c\u53f3\u952e\u5355\u51fb\u8981\u8fd0\u884c\u7684.py\u6587\u4ef6\uff0c\u9009\u62e9\u201c\u6253\u5f00\u65b9\u5f0f\u201d\uff0c\u7136\u540e\u9009\u62e9Python 3\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff08\u901a\u5e38\u4f4d\u4e8ePython\u7684\u5b89\u88c5\u76ee\u5f55\u4e0b\uff09\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u8f93\u5165<code>python file.py<\/code>\u6765\u76f4\u63a5\u8fd0\u884c\u8be5\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Linux\u6216macOS\u4e0a\u8bbe\u7f6e.py\u6587\u4ef6\u4ee5\u4f7f\u7528Python 3\u8fd0\u884c\uff1f<\/strong><br \/>\u5728Linux\u6216macOS\u7cfb\u7edf\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528\u547d\u4ee4\u6765\u8fd0\u884c.py\u6587\u4ef6\u3002\u786e\u4fdd\u60a8\u7684\u6587\u4ef6\u9996\u884c\u5305\u542b<code>#!\/usr\/bin\/env python3<\/code>\uff0c\u8fd9\u4f1a\u544a\u8bc9\u7cfb\u7edf\u4f7f\u7528Python 3\u6765\u6267\u884c\u8be5\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>chmod +x file.py<\/code>\u547d\u4ee4\u4e3a\u6587\u4ef6\u6dfb\u52a0\u6267\u884c\u6743\u9650\u3002\u6700\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>.\/file.py<\/code>\u6765\u8fd0\u884c\u8be5\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u679c.py\u6587\u4ef6\u4e0d\u4f7f\u7528Python 3\u8fd0\u884c\uff0c\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u7684.py\u6587\u4ef6\u65e0\u6cd5\u4f7f\u7528Python 3\u8fd0\u884c\uff0c\u9996\u5148\u68c0\u67e5\u6587\u4ef6\u7684\u9996\u884c\u662f\u5426\u6307\u5411Python 3\u7684\u89e3\u91ca\u5668\u3002\u786e\u4fdd\u8be5\u6587\u4ef6\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\uff0c\u5e76\u4e14\u4f7f\u7528\u7684\u5e93\u548c\u6a21\u5757\u4e0ePython 3\u517c\u5bb9\u3002\u5982\u679c\u4ecd\u7136\u5b58\u5728\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u6dfb\u52a0<code>python3 file.py<\/code>\uff0c\u76f4\u63a5\u6307\u5b9a\u4f7f\u7528Python 3\u6765\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8bbe\u7f6epy\u6587\u4ef6\u7528python3\u8fd0\u884c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u5728\u6587\u4ef6\u5934\u90e8\u6dfb\u52a0shebang\u3001\u4e3apython3\u8bbe\u7f6e\u9ed8 [&hellip;]","protected":false},"author":3,"featured_media":1112924,"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\/1112915"}],"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=1112915"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112915\/revisions"}],"predecessor-version":[{"id":1112925,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112915\/revisions\/1112925"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112924"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}