{"id":1003410,"date":"2024-12-27T10:17:02","date_gmt":"2024-12-27T02:17:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003410.html"},"modified":"2024-12-27T10:17:04","modified_gmt":"2024-12-27T02:17:04","slug":"%e5%a6%82%e4%bd%95%e7%94%a8cmd%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003410.html","title":{"rendered":"\u5982\u4f55\u7528cmd\u8c03\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080801\/020c8cc5-5418-4de3-86af-6c677bc155e9.webp\" alt=\"\u5982\u4f55\u7528cmd\u8c03\u7528python\" \/><\/p>\n<p><p> <strong>\u8981\u7528CMD\u8c03\u7528Python\uff0c\u9996\u5148\u9700\u8981\u786e\u8ba4Python\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f\u6216\u6267\u884cPython\u811a\u672c\u6587\u4ef6\u3001\u4f7f\u7528\u7279\u5b9a\u547d\u4ee4\u8fd0\u884cPython\u4ee3\u7801\u3002<\/strong>\u5176\u4e2d\uff0c\u786e\u4fddPython\u5b89\u88c5\u5e76\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\u3002\u901a\u8fc7\u914d\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\u4e2d\u8c03\u7528Python\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u5176\u5b8c\u6574\u8def\u5f84\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u786e\u8ba4Python\u5b89\u88c5\u53ca\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u547d\u4ee4\u884c\u4e2d\u987a\u5229\u8c03\u7528Python\uff0c\u9996\u5148\u9700\u8981\u786e\u8ba4Python\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u68c0\u67e5Python\u662f\u5426\u5df2\u5b89\u88c5<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u68c0\u67e5Python\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\uff0c\u53ef\u4ee5\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u5e76\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cPython\u5df2\u5b89\u88c5\uff0c\u547d\u4ee4\u884c\u5c06\u8fd4\u56dePython\u7684\u7248\u672c\u53f7\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u76f8\u5e94\u7684\u7248\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u6dfb\u52a0Python\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u201c\u7cfb\u7edf\u5c5e\u6027\u201d\u7a97\u53e3\uff08\u53ef\u4ee5\u901a\u8fc7\u53f3\u952e\u70b9\u51fb\u201c\u6b64\u7535\u8111\u201d\u5e76\u9009\u62e9\u201c\u5c5e\u6027\u201d\u6765\u6253\u5f00\uff09\u3002<\/li>\n<li>\u70b9\u51fb\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\u90e8\u5206\uff0c\u627e\u5230\u5e76\u9009\u62e9\u201cPath\u201d\u53d8\u91cf\uff0c\u70b9\u51fb\u201c\u7f16\u8f91\u201d\u3002<\/li>\n<li>\u5728\u201c\u7f16\u8f91\u73af\u5883\u53d8\u91cf\u201d\u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb\u201c\u65b0\u5efa\u201d\uff0c\u5e76\u8f93\u5165Python\u7684\u5b89\u88c5\u8def\u5f84\uff08\u4f8b\u5982\uff1a<code>C:\\Python39<\/code>\uff09\u3002<\/li>\n<li>\u786e\u8ba4\u6240\u6709\u7a97\u53e3\u5e76\u4fdd\u5b58\u66f4\u6539\u3002<\/li>\n<\/ul>\n<p><p>\u6dfb\u52a0\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u518d\u6b21\u8f93\u5165<code>python --version<\/code>\u6765\u786e\u8ba4\u914d\u7f6e\u662f\u5426\u6210\u529f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f\u6216\u6267\u884cPython\u811a\u672c\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u73af\u5883\u914d\u7f6e\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f\u6216\u76f4\u63a5\u6267\u884cPython\u811a\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fdb\u5165Python\u4ea4\u4e92\u6a21\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python<\/code>\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\u3002\u8fd9\u6837\u5c06\u8fdb\u5165Python\u7684\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165Python\u4ee3\u7801\u8fdb\u884c\u6d4b\u8bd5\u548c\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fdb\u5165\u4ea4\u4e92\u6a21\u5f0f\u540e\uff0c\u53ef\u4ee5\u8f93\u5165\u7b80\u5355\u7684Python\u6307\u4ee4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6309\u4e0b\u56de\u8f66\u952e\u540e\uff0c\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6267\u884cPython\u811a\u672c\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u6267\u884cPython\u811a\u672c\u6587\u4ef6\uff0c\u9700\u8981\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u5bfc\u822a\u5230\u811a\u672c\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u7136\u540e\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python script_name.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d<code>script_name.py<\/code>\u662f\u4f60\u7684Python\u811a\u672c\u6587\u4ef6\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u811a\u672c\u6587\u4ef6\u540d\u4e3a<code>example.py<\/code>\uff0c\u5219\u53ef\u4ee5\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6309\u4e0b\u56de\u8f66\u540e\uff0c\u547d\u4ee4\u884c\u5c06\u6267\u884c\u8be5\u811a\u672c\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7279\u5b9a\u547d\u4ee4\u8fd0\u884cPython\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u8fdb\u5165\u4ea4\u4e92\u6a21\u5f0f\u548c\u6267\u884c\u811a\u672c\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528<code>-c<\/code>\u9009\u9879\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528<code>-c<\/code>\u9009\u9879<\/strong><\/p>\n<\/p>\n<p><p><code>-c<\/code>\u9009\u9879\u5141\u8bb8\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8fd0\u884c\u5355\u884cPython\u4ee3\u7801\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -c &quot;print(&#39;Hello, World!&#39;)&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u51fa<code>Hello, World!<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>-m<\/code>\u9009\u9879<\/strong><\/p>\n<\/p>\n<p><p><code>-m<\/code>\u9009\u9879\u7528\u4e8e\u8fd0\u884cPython\u6a21\u5757\u3002\u5047\u8bbe\u9700\u8981\u8fd0\u884c\u4e00\u4e2a\u6807\u51c6\u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m module_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8981\u4f7f\u7528Python\u7684HTTP\u670d\u52a1\u5668\u6a21\u5757\uff0c\u53ef\u4ee5\u8f93\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m http.server<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u4f1a\u542f\u52a8\u4e00\u4e2a\u7b80\u5355\u7684HTTP\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u8c03\u8bd5\u53ca\u6545\u969c\u6392\u9664<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528CMD\u8c03\u7528Python\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u548c\u6545\u969c\u6392\u9664\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u786e\u4fdd\u8def\u5f84\u6b63\u786e<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>python<\/code>\u540e\u51fa\u73b0\u9519\u8bef\uff0c\u9996\u5148\u8981\u68c0\u67e5Python\u7684\u5b89\u88c5\u8def\u5f84\u662f\u5426\u6b63\u786e\u6dfb\u52a0\u5230\u4e86\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68c0\u67e5Python\u7248\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6709\u591a\u4e2aPython\u7248\u672c\u5b89\u88c5\u5728\u540c\u4e00\u53f0\u8ba1\u7b97\u673a\u4e0a\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u7279\u5b9a\u7248\u672c\u7684Python\u6267\u884c\u5668\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>python3<\/code>\u6765\u660e\u786e\u8c03\u7528Python 3.x\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 script_name.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u811a\u672c\u6587\u4ef6\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u6267\u884cPython\u811a\u672c\u6587\u4ef6\u65f6\uff0c\u5f53\u524d\u76ee\u5f55\u8def\u5f84\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u66f4\u6539\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd path_to_script_directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u9519\u8bef\u4fe1\u606f<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u811a\u672c\u8fd0\u884c\u5931\u8d25\u65f6\uff0c\u547d\u4ee4\u884c\u901a\u5e38\u4f1a\u63d0\u4f9b\u9519\u8bef\u4fe1\u606f\u3002\u4ed4\u7ec6\u9605\u8bfb\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u5efa\u8bae\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u987a\u5229\u8c03\u7528Python\uff0c\u5b9e\u73b0\u811a\u672c\u7684\u8fd0\u884c\u548c\u8c03\u8bd5\u3002\u8fd9\u5bf9\u4e8ePython\u5f00\u53d1\u8005\u548c\u4f7f\u7528Python\u8fdb\u884c\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u4eba\u6765\u8bf4\uff0c\u662f\u4e00\u9879\u91cd\u8981\u7684\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728CMD\u4e2d\u786e\u8ba4Python\u662f\u5426\u5df2\u5b89\u88c5\uff1f<\/strong><br \/>\u5728CMD\u4e2d\uff0c\u8f93\u5165<code>python --version<\/code>\u6216<code>python -V<\/code>\uff0c\u5982\u679cPython\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u7cfb\u7edf\u5c06\u8fd4\u56de\u6240\u5b89\u88c5\u7684Python\u7248\u672c\u53f7\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\uff0c\u5e76\u786e\u4fdd\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9009\u62e9\u5c06Python\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728CMD\u4e2d\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728CMD\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u60a8\u9700\u8981\u5bfc\u822a\u5230\u5305\u542b\u811a\u672c\u7684\u76ee\u5f55\u3002\u4f7f\u7528<code>cd<\/code>\u547d\u4ee4\u8fdb\u5165\u76f8\u5e94\u6587\u4ef6\u5939\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165<code>python your_script.py<\/code>\u6765\u6267\u884c\u811a\u672c\uff0c\u5176\u4e2d<code>your_script.py<\/code>\u662f\u60a8\u8981\u8fd0\u884c\u7684Python\u6587\u4ef6\u540d\u3002<\/p>\n<p><strong>CMD\u4e2d\u5982\u4f55\u4f7f\u7528\u865a\u62df\u73af\u5883\u8fd0\u884cPython\uff1f<\/strong><br \/>\u521b\u5efa\u865a\u62df\u73af\u5883\u53ef\u4ee5\u901a\u8fc7\u5728CMD\u4e2d\u8f93\u5165<code>python -m venv myenv<\/code>\u6765\u5b9e\u73b0\uff0c\u5176\u4e2d<code>myenv<\/code>\u662f\u865a\u62df\u73af\u5883\u7684\u540d\u79f0\u3002\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>myenv\\Scripts\\activate<\/code>\uff08Windows\u7cfb\u7edf\uff09\u6765\u542f\u52a8\u5b83\u3002\u6fc0\u6d3b\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728\u8be5\u73af\u5883\u4e2d\u5b89\u88c5\u5e93\u5e76\u8fd0\u884cPython\u811a\u672c\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528CMD\u8c03\u7528Python\uff0c\u9996\u5148\u9700\u8981\u786e\u8ba4Python\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8fdb\u5165Python\u4ea4\u4e92\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1003412,"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\/1003410"}],"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=1003410"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003410\/revisions"}],"predecessor-version":[{"id":1003413,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003410\/revisions\/1003413"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003412"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}