{"id":1059238,"date":"2024-12-31T15:23:30","date_gmt":"2024-12-31T07:23:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059238.html"},"modified":"2024-12-31T15:23:33","modified_gmt":"2024-12-31T07:23:33","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e5%bd%93%e4%b8%ad%e8%b0%83%e7%94%a8%e5%85%b6%e4%bb%96-py%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059238.html","title":{"rendered":"\u5982\u4f55\u5728python\u5f53\u4e2d\u8c03\u7528\u5176\u4ed6 py\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3fd5242c-e797-4e98-82bd-737f7075f894.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python\u5f53\u4e2d\u8c03\u7528\u5176\u4ed6 py\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528sys\u6a21\u5757\u548c\u6267\u884cpy\u6587\u4ef6\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u8bbf\u95ee\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002<\/strong> <\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528import\u8bed\u53e5<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u5e76\u4e14\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u540d\u8fdb\u884c\u524d\u7f00\u6807\u8bc6\u3002<strong>\u4f7f\u7528from&#8230;import\u8bed\u53e5<\/strong>\u5219\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002<strong>\u4f7f\u7528sys\u6a21\u5757<\/strong>\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\uff0c\u5e76\u5bfc\u5165\u6a21\u5757\u3002\u6700\u540e\uff0c<strong>\u6267\u884cpy\u6587\u4ef6<\/strong>\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528os.system\u6216subprocess\u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u7684\u65b9\u6cd5\u548c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528import\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>Python\u7684import\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u5bfc\u5165\u65b9\u5f0f\u3002\u5b83\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u4ece\u800c\u4f7f\u6211\u4eec\u80fd\u591f\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6587\u4ef6\uff1a<code>file1.py<\/code>\u548c<code>file2.py<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u5728<code>file1.py<\/code>\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>def greet():<\/p>\n<p>    print(&quot;Hello from file1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>file2.py<\/code>\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528import\u8bed\u53e5\u6765\u5bfc\u5165<code>file1.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file2.py<\/p>\n<p>import file1<\/p>\n<p>file1.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>file2.py<\/code>\uff0c\u6211\u4eec\u5c06\u770b\u5230\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello from file1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u76f4\u63a5\u4f7f\u7528\u88ab\u5bfc\u5165\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u800c\u4e0d\u9700\u8981\u6dfb\u52a0\u6a21\u5757\u540d\u4f5c\u4e3a\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><p>\u7ee7\u7eed\u4f7f\u7528\u524d\u9762\u7684\u4f8b\u5b50\uff0c\u5728<code>file2.py<\/code>\u4e2d\uff0c\u6211\u4eec\u6539\u4e3a\u4f7f\u7528from&#8230;import\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># file2.py<\/p>\n<p>from file1 import greet<\/p>\n<p>greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>file2.py<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\u76f8\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello from file1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528sys\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6a21\u5757\u53ef\u80fd\u4e0d\u5728\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u9700\u8981\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u7684\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>project\/<\/p>\n<p>    m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>    subdir\/<\/p>\n<p>        file1.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0<code>subdir<\/code>\u5230sys.path\uff0c\u7136\u540e\u5bfc\u5165<code>file1<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import sys<\/p>\n<p>import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6587\u4ef6\u7684\u76ee\u5f55<\/strong><\/h2>\n<p>current_dir = os.path.dirname(os.path.abspath(__file__))<\/p>\n<h2><strong>\u5c06\u5b50\u76ee\u5f55\u6dfb\u52a0\u5230sys.path<\/strong><\/h2>\n<p>sys.path.append(os.path.join(current_dir, &quot;subdir&quot;))<\/p>\n<p>import file1<\/p>\n<p>file1.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>main.py<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello from file1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6267\u884cpy\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u6267\u884c\u53e6\u4e00\u4e2aPython\u811a\u672c\uff0c\u800c\u4e0d\u662f\u5bfc\u5165\u5176\u4e2d\u7684\u5185\u5bb9\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528os.system\u6216subprocess\u6a21\u5757\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6587\u4ef6\uff1a<code>main.py<\/code>\u548c<code>script.py<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u5728<code>script.py<\/code>\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e9b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># script.py<\/p>\n<p>print(&quot;This is script.py&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528os.system\u6765\u6267\u884c<code>script.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import os<\/p>\n<p>os.system(&quot;python script.py&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>main.py<\/code>\uff0c\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>This is script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528subprocess\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import subprocess<\/p>\n<p>subprocess.run([&quot;python&quot;, &quot;script.py&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c<code>main.py<\/code>\uff0c\u540c\u6837\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>This is script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002\u4f7f\u7528import\u8bed\u53e5\u548cfrom&#8230;import\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7edd\u5927\u591a\u6570\u60c5\u51b5\u3002\u4f7f\u7528sys\u6a21\u5757\u53ef\u4ee5\u52a8\u6001\u5730\u6dfb\u52a0\u6a21\u5757\u8def\u5f84\uff0c\u9002\u7528\u4e8e\u6a21\u5757\u8def\u5f84\u4e0d\u56fa\u5b9a\u7684\u60c5\u51b5\u3002\u6267\u884cpy\u6587\u4ef6\u5219\u9002\u7528\u4e8e\u9700\u8981\u8fd0\u884c\u72ec\u7acb\u811a\u672c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5728Python\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\u3001\u590d\u7528\u4ee3\u7801\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u6216\u5305\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u5176\u4ed6\u7684.py\u6587\u4ef6\u3002\u53ea\u9700\u786e\u4fdd\u9700\u8981\u5bfc\u5165\u7684\u6587\u4ef6\u4e0e\u5f53\u524d\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u5728Python\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\u4e2d\u3002\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_module.py<\/code>\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import my_module<\/code>\u6765\u5bfc\u5165\uff0c\u5e76\u4f7f\u7528<code>my_module.function_name()<\/code>\u6765\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u7684\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\uff1f<\/strong><br \/>\u5f53\u4f60\u5bfc\u5165\u4e00\u4e2a\u6a21\u5757\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\uff08.\uff09\u8fd0\u7b97\u7b26\u8bbf\u95ee\u8be5\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>my_module.py<\/code>\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>my_function<\/code>\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>my_module.my_function()<\/code>\u6765\u8c03\u7528\u5b83\u3002\u5982\u679c\u4f60\u53ea\u60f3\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from my_module import my_function<\/code>\u8bed\u53e5\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>my_function()<\/code>\u800c\u65e0\u9700\u524d\u7f00\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u907f\u514d\u5faa\u73af\u5bfc\u5165\u7684\u95ee\u9898\uff1f<\/strong><br \/>\u5faa\u73af\u5bfc\u5165\u901a\u5e38\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u65f6\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u8003\u8651\u91cd\u6784\u4ee3\u7801\uff0c\u5c06\u5171\u4eab\u7684\u529f\u80fd\u653e\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u6216\u8005\u63a8\u8fdf\u5bfc\u5165\uff08\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\uff09\u4ee5\u907f\u514d\u5bfc\u5165\u65f6\u7684\u4f9d\u8d56\u95ee\u9898\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u7684\u521d\u59cb\u5316\u987a\u5e8f\u6b63\u786e\uff0c\u4ece\u800c\u51cf\u5c11\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8c03\u7528\u5176\u4ed6py\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001\u4f7f\u7528from&#8230;import\u8bed\u53e5\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1059246,"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\/1059238"}],"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=1059238"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059238\/revisions"}],"predecessor-version":[{"id":1059250,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059238\/revisions\/1059250"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059246"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}