{"id":970998,"date":"2024-12-27T05:30:56","date_gmt":"2024-12-26T21:30:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970998.html"},"modified":"2024-12-27T05:30:58","modified_gmt":"2024-12-26T21:30:58","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5-py","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/970998.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5bfc\u5165.py"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194037\/f19a67f6-9d81-48b3-9f9f-c8d3a980bf25.webp\" alt=\"python\u4e2d\u5982\u4f55\u5bfc\u5165.py\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165.py\u6587\u4ef6\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528importlib\u6a21\u5757\u7b49<\/strong>\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u53e6\u4e00\u4e2a.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u7528\u8be5\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002<strong>\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import mymodule<\/code>\u6765\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/strong>\u3002\u5982\u679c\u53ea\u9700\u8981\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from mymodule import my_function<\/code>\u7684\u8bed\u6cd5\u6765\u5bfc\u5165\u7279\u5b9a\u7684\u529f\u80fd\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>importlib<\/code>\u6a21\u5757\u6765\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u5728\u9700\u8981\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u5bfc\u5165\u54ea\u4e2a\u6a21\u5757\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528IMPORT\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u662f\u5bfc\u5165.py\u6587\u4ef6\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u53e6\u4e00\u4e2a.py\u6587\u4ef6\u5f53\u4f5c\u6a21\u5757\u5bfc\u5165\uff0c\u4ece\u800c\u4f7f\u7528\u5176\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u548c\u7c7b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import module_name<\/code>\u53ef\u4ee5\u5c06\u4e00\u4e2a.py\u6587\u4ef6\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u3002\u6587\u4ef6\u540d\u662f<code>module_name.py<\/code>\u65f6\uff0c\u5bfc\u5165\u65f6\u53ea\u9700\u4f7f\u7528\u6a21\u5757\u540d\uff0c\u65e0\u9700\u52a0\u540e\u7f00\u201c.py\u201d\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e00\u4e2a\u6587\u4ef6<code>mymodule.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u5bfc\u5165\u5e76\u4f7f\u7528\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528AS\u91cd\u547d\u540d\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5bfc\u5165\u7684\u6a21\u5757\u540d\u592a\u957f\uff0c\u6216\u8005\u53ef\u80fd\u4e0e\u5176\u4ed6\u6a21\u5757\u51b2\u7a81\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u6765\u91cd\u547d\u540d\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule as mm<\/p>\n<p>print(mm.greet(&quot;Bob&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5206\u6a21\u5757\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u90e8\u5206\uff0c\u5982\u67d0\u4e2a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import greet<\/p>\n<p>print(greet(&quot;Charlie&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ea\u5bfc\u5165\u9700\u8981\u7684\u90e8\u5206\uff0c\u51cf\u5c11\u4e86\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u591a\u4e2a\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e00\u6b21\u5bfc\u5165\u591a\u4e2a\u5bf9\u8c61\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import greet, another_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u6240\u6709\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>*<\/code>\u6765\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5bf9\u8c61\uff0c\u4f46\u8fd9\u79cd\u505a\u6cd5\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import *<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528IMPORTLIB\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u52a8\u6001\u5bfc\u5165\u6a21\u5757\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>importlib<\/code>\u6a21\u5757\u3002\u5b83\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u5bfc\u5165\u6a21\u5757\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u52a8\u6001\u51b3\u5b9a\u5bfc\u5165\u54ea\u4e2a\u6a21\u5757\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165importlib<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5bfc\u5165<code>importlib<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528import_module\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>importlib.import_module()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u6a21\u5757\u540d\uff08\u5b57\u7b26\u4e32\uff09\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mymodule = importlib.import_module(&#39;mymodule&#39;)<\/p>\n<p>print(mymodule.greet(&quot;David&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u91cd\u8f7d\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u5df2\u7ecf\u88ab\u5bfc\u5165\u5e76\u4e14\u9700\u8981\u91cd\u8f7d\uff08\u4f8b\u5982\uff0c\u6a21\u5757\u7684\u5185\u5bb9\u53d1\u751f\u4e86\u53d8\u5316\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>importlib.reload()<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">importlib.reload(mymodule)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7edd\u5bf9\u5bfc\u5165\u548c\u76f8\u5bf9\u5bfc\u5165<\/p>\n<\/p>\n<p><p>\u5728\u8f83\u5927\u7684\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u7ec4\u7ec7\u591a\u4e2a\u6a21\u5757\uff0c\u8fd9\u65f6\u7edd\u5bf9\u5bfc\u5165\u548c\u76f8\u5bf9\u5bfc\u5165\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7edd\u5bf9\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u7edd\u5bf9\u5bfc\u5165\u662f\u6307\u4ece\u9879\u76ee\u7684\u6839\u76ee\u5f55\u51fa\u53d1\uff0c\u6307\u5b9a\u5bfc\u5165\u7684\u6a21\u5757\u8def\u5f84\u3002\u8fd9\u79cd\u65b9\u5f0f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u5f88\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># project\/<\/p>\n<h2><strong>\u251c\u2500\u2500 package\/<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u2502   \u251c\u2500\u2500 module_a.py<\/strong><\/h2>\n<h2><strong>\u2502   \u2514\u2500\u2500 module_b.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<h2><strong>module_a.py<\/strong><\/h2>\n<p>def foo():<\/p>\n<p>    return &quot;foo from module_a&quot;<\/p>\n<h2><strong>module_b.py<\/strong><\/h2>\n<p>from package.module_a import foo<\/p>\n<p>print(foo())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u76f8\u5bf9\u5bfc\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u76f8\u5bf9\u5bfc\u5165\u57fa\u4e8e\u5f53\u524d\u6a21\u5757\u7684\u4f4d\u7f6e\uff0c\u4f7f\u7528<code>.<\/code>\u8868\u793a\u5f53\u524d\u76ee\u5f55\uff0c<code>..<\/code>\u8868\u793a\u4e0a\u4e00\u7ea7\u76ee\u5f55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_b.py<\/p>\n<p>from .module_a import foo  # \u5f53\u524d\u76ee\u5f55<\/p>\n<h2><strong>or<\/strong><\/h2>\n<p>from ..package.module_a import foo  # \u4e0a\u4e00\u7ea7\u76ee\u5f55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6ce8\u610f\uff1a\u76f8\u5bf9\u5bfc\u5165\u53ea\u80fd\u5728\u5305\u5185\u4f7f\u7528\uff0c\u4e0d\u80fd\u5728\u9876\u5c42\u6a21\u5757\uff08\u5373\u76f4\u63a5\u8fd0\u884c\u7684\u811a\u672c\uff09\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5904\u7406\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/p>\n<\/p>\n<p><p>Python\u5728\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u4f1a\u6839\u636e<code>sys.path<\/code>\u4e2d\u7684\u8def\u5f84\u67e5\u627e\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u5f53\u524d\u641c\u7d22\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>print(sys.path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8def\u5f84<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6dfb\u52a0\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/your\/module&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\uff0cPython\u4f1a\u5728<code>\/path\/to\/your\/module<\/code>\u76ee\u5f55\u4e2d\u67e5\u627e\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e<code>PYTHONPATH<\/code>\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbfPython\u5728\u542f\u52a8\u65f6\u81ea\u52a8\u5305\u542b\u8fd9\u4e9b\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u5728Linux\u6216macOS\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u8bbe\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PYTHONPATH=$PYTHONPATH:\/path\/to\/your\/module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u8bbe\u7f6e\u6dfb\u52a0\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u7ba1\u7406\u6a21\u5757\u7684\u5e38\u89c1\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u5bfc\u5165\u6a21\u5757\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u5757\u672a\u627e\u5230\u9519\u8bef<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679cPython\u63d0\u793a\u627e\u4e0d\u5230\u6a21\u5757\uff0c\u901a\u5e38\u662f\u56e0\u4e3a\u6a21\u5757\u4e0d\u5728<code>sys.path<\/code>\u4e2d\u3002\u786e\u4fdd\u6a21\u5757\u5728\u6b63\u786e\u7684\u8def\u5f84\u4e0a\uff0c\u6216\u8005\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5faa\u73af\u5bfc\u5165\u9519\u8bef<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u5bfc\u5165\u662f\u6307\u6a21\u5757A\u5bfc\u5165\u6a21\u5757B\uff0c\u540c\u65f6\u6a21\u5757B\u5bfc\u5165\u6a21\u5757A\u3002\u8fd9\u79cd\u60c5\u51b5\u4f1a\u5bfc\u81f4\u5bfc\u5165\u5931\u8d25\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u91cd\u6784\u4ee3\u7801\uff0c\u907f\u514d\u5faa\u73af\u5bfc\u5165\uff0c\u6216\u8005\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u547d\u540d\u51b2\u7a81<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u540c\u6a21\u5757\u4e2d\u6709\u76f8\u540c\u7684\u540d\u79f0\uff0c\u53ef\u80fd\u5bfc\u81f4\u51b2\u7a81\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>as<\/code>\u91cd\u547d\u540d\u5bfc\u5165\u7684\u6a21\u5757\u6216\u5bf9\u8c61\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u5757\u91cd\u8f7d\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u6a21\u5757\u5df2\u7ecf\u88ab\u5bfc\u5165\uff0c\u4fee\u6539\u540e\u9700\u8981\u4f7f\u7528<code>importlib.reload()<\/code>\u6765\u91cd\u8f7d\uff0c\u800c\u4e0d\u662f\u518d\u6b21\u4f7f\u7528<code>import<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6a21\u5757\u7684\u7ec4\u7ec7\u4e0e\u6253\u5305<\/p>\n<\/p>\n<p><p>\u5728\u8f83\u5927\u7684\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u7ec4\u7ec7\u548c\u6253\u5305\u6a21\u5757\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u5c06\u76f8\u5173\u7684\u6a21\u5757\u7ec4\u7ec7\u5230\u5305\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5305\u662f\u4e00\u4e2a\u5305\u542b<code>__init__.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\u3002\u53ef\u4ee5\u5c06\u591a\u4e2a\u6a21\u5757\u653e\u5728\u5305\u4e2d\uff0c\u5e76\u5728<code>__init__.py<\/code>\u4e2d\u5b9a\u4e49\u5305\u7684\u521d\u59cb\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># package\/<\/p>\n<h2><strong>\u251c\u2500\u2500 __init__.py<\/strong><\/h2>\n<h2><strong>\u251c\u2500\u2500 module_a.py<\/strong><\/h2>\n<h2><strong>\u2514\u2500\u2500 module_b.py<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528setup.py\u6253\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u5c06\u6a21\u5757\u53d1\u5e03\u5230Python Package Index (PyPI)\uff0c\u9700\u8981\u4f7f\u7528<code>setup.py<\/code>\u6765\u5b9a\u4e49\u5305\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;your_package_name&#39;,<\/p>\n<p>    version=&#39;0.1.0&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # list your dependencies here<\/p>\n<p>    ],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53d1\u5e03\u5230PyPI<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982<code>twine<\/code>\u5c06\u5305\u53d1\u5e03\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist bdist_wheel<\/p>\n<p>twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u5bf9\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165.py\u6587\u4ef6\u6709\u4e86\u6df1\u5165\u7684\u4e86\u89e3\u3002\u65e0\u8bba\u662f\u901a\u8fc7<code>import<\/code>\u3001<code>importlib<\/code>\u8fd8\u662f\u7ba1\u7406\u6a21\u5757\u8def\u5f84\u548c\u7ec4\u7ec7\u5305\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684Python\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\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\u811a\u672c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u5176\u4ed6\u6a21\u5757\u6216\u811a\u672c\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u8981\u5bfc\u5165\u7684\u6a21\u5757\u540d\u3002\u5982\u679c\u6a21\u5757\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\uff0c\u76f4\u63a5\u4f7f\u7528\u6a21\u5757\u540d\u5373\u53ef\uff1b\u5982\u679c\u6a21\u5757\u5728\u4e0d\u540c\u7684\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sys.path.append()<\/code>\u6765\u6dfb\u52a0\u8be5\u76ee\u5f55\u81f3\u8def\u5f84\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>import mymodule<\/code>\u6765\u5bfc\u5165\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5bfc\u5165\u9519\u8bef\uff1f<\/strong><br \/>\u5f53\u5bfc\u5165\u6a21\u5757\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230<code>ModuleNotFoundError<\/code>\u6216\u5176\u4ed6\u9519\u8bef\u3002\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u68c0\u67e5\u6a21\u5757\u6587\u4ef6\u662f\u5426\u5b58\u5728\u4e8e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u786e\u4fdd\u6a21\u5757\u8def\u5f84\u88ab\u6b63\u786e\u8bbe\u7f6e\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6587\u4ef6\u540d\u6ca1\u6709\u62fc\u5199\u9519\u8bef\uff0c\u5e76\u4e14\u4f7f\u7528\u7684Python\u7248\u672c\u4e0e\u6a21\u5757\u517c\u5bb9\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5bfc\u5165\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7<code>from ... import ...<\/code>\u8bed\u6cd5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5728<code>mymodule.py<\/code>\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_function<\/code>\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from mymodule import my_function<\/code>\u6765\u76f4\u63a5\u5bfc\u5165\u8be5\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u4ee3\u7801\u6e05\u6670\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165.py\u6587\u4ef6\u7684\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528import\u8bed\u53e5\u3001from&#8230;import\u8bed\u53e5\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":971004,"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\/970998"}],"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=970998"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970998\/revisions"}],"predecessor-version":[{"id":971005,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/970998\/revisions\/971005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971004"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=970998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=970998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=970998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}