{"id":940646,"date":"2024-12-26T21:05:00","date_gmt":"2024-12-26T13:05:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940646.html"},"modified":"2024-12-26T21:05:01","modified_gmt":"2024-12-26T13:05:01","slug":"python-%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/940646.html","title":{"rendered":"python \u5982\u4f55\u5b9a\u4e49\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075207\/408b2c21-aa31-495f-be2a-fe658c4641ef.webp\" alt=\"python \u5982\u4f55\u5b9a\u4e49\u6a21\u5757\" \/><\/p>\n<p><p> <strong>\u5b9a\u4e49Python\u6a21\u5757\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u521b\u5efa.py\u6587\u4ef6\u3001\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u3001\u7ec4\u7ec7\u4ee3\u7801\u3002<\/strong> Python\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u5b9a\u4e49\u548c\u8bed\u53e5\u7684\u6587\u4ef6\uff0c\u5176\u6587\u4ef6\u540d\u4ee5\u201c.py\u201d\u7ed3\u5c3e\u3002\u6a21\u5757\u5141\u8bb8\u60a8\u5c06\u4ee3\u7801\u7ec4\u7ec7\u6210\u903b\u8f91\u4e0a\u7684\u5206\u7ec4\uff0c\u65b9\u4fbf\u7ba1\u7406\u548c\u91cd\u7528\u3002\u5b9a\u4e49\u6a21\u5757\u7684\u7b2c\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u7136\u540e\u53ef\u4ee5\u5728\u8be5\u6587\u4ef6\u4e2d\u7f16\u5199\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u548c\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u3002<strong>\u6a21\u5757\u4f7f\u4ee3\u7801\u7ec4\u7ec7\u66f4\u6e05\u6670\u3001\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efaPython\u6a21\u5757\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u521b\u5efaPython\u6a21\u5757\u7684\u7b2c\u4e00\u6b65\u662f\u521b\u5efa\u4e00\u4e2a\u4ee5\u201c.py\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>mymodule.py<\/code>\u7684\u6a21\u5757\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u3002<\/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>class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def display(self):<\/p>\n<p>        return f&quot;Name: {self.name}, Age: {self.age}&quot;<\/p>\n<p>PI = 3.14159<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u6a21\u5757\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>greet<\/code>\uff0c\u4e00\u4e2a\u7c7b<code>Person<\/code>\u548c\u4e00\u4e2a\u5e38\u91cf<code>PI<\/code>\u3002\u8fd9\u4e9b\u7ec4\u4ef6\u53ef\u4ee5\u5728\u5176\u4ed6Python\u811a\u672c\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u6a21\u5757\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u5176\u4ed6Python\u6587\u4ef6\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u5b83\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5bfc\u5165\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5bfc\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u7136\u540e\u901a\u8fc7\u6a21\u5757\u540d\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.greet(&quot;Alice&quot;))<\/p>\n<p>person = mymodule.Person(&quot;Bob&quot;, 30)<\/p>\n<p>print(person.display())<\/p>\n<p>print(mymodule.PI)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>mymodule<\/code>\u6a21\u5757\u88ab\u5bfc\u5165\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>mymodule.greet<\/code>\u3001<code>mymodule.Person<\/code>\u548c<code>mymodule.PI<\/code>\u6765\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528from&#8230;import\u8bed\u53e5\u5bfc\u5165\u7279\u5b9a\u7ec4\u4ef6<\/h4>\n<\/p>\n<p><p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>from...import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u7ec4\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7ec4\u4ef6\u540d\u79f0\uff0c\u800c\u65e0\u9700\u6a21\u5757\u540d\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import greet, Person<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p>person = Person(&quot;Bob&quot;, 30)<\/p>\n<p>print(person.display())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53ea\u5bfc\u5165\u4e86<code>greet<\/code>\u51fd\u6570\u548c<code>Person<\/code>\u7c7b\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u4eec\uff0c\u800c\u4e0d\u9700\u8981\u6a21\u5757\u540d\u524d\u7f00\u3002<\/p>\n<\/p>\n<p><h4>3. \u5bfc\u5165\u6240\u6709\u7ec4\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>from...import *<\/code>\u53ef\u4ee5\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u6240\u6709\u7ec4\u4ef6\uff0c\u4f46\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u5e76\u4e14\u4f7f\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import *<\/p>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p>person = Person(&quot;Bob&quot;, 30)<\/p>\n<p>print(person.display())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ec4\u7ec7\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u5c06\u6a21\u5757\u7ec4\u7ec7\u6210\u5305\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u5176\u4e2d\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u6587\u4ef6<code>__init__.py<\/code>\uff0c\u7528\u4e8e\u6807\u8bc6\u8be5\u76ee\u5f55\u662f\u4e00\u4e2a\u5305\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e00\u4e2a\u5305<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5305\u65f6\uff0c\u9700\u8981\u5728\u76ee\u5f55\u4e2d\u6dfb\u52a0\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\u3002\u5373\u4f7f\u6587\u4ef6\u662f\u7a7a\u7684\uff0c\u5b83\u4e5f\u5fc5\u987b\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>mypackage<\/code>\u662f\u4e00\u4e2a\u5305\uff0c\u5305\u542b\u4e24\u4e2a\u6a21\u5757<code>module1<\/code>\u548c<code>module2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u5305\u540d\u548c\u6a21\u5757\u540d\u5bfc\u5165\u5305\u4e2d\u7684\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage import module1<\/p>\n<p>from mypackage.module2 import some_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u6709\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u8fd9\u610f\u5473\u7740\u5728\u6a21\u5757\u5185\u90e8\u5b9a\u4e49\u7684\u540d\u79f0\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u6a21\u5757\u7684\u540d\u79f0\u51b2\u7a81\u3002\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u540d\u79f0\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u540d\u6765\u8bbf\u95ee\uff0c\u8fd9\u6709\u52a9\u4e8e\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>1. \u6a21\u5757\u7684\u4f5c\u7528\u57df<\/h4>\n<\/p>\n<p><p>\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5185\u5bb9\u5728\u6a21\u5757\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u6a21\u5757\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>print(&quot;Module name:&quot;, __name__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f53\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\uff0c<code>__name__<\/code>\u5c5e\u6027\u7684\u503c\u662f\u6a21\u5757\u7684\u540d\u79f0\uff1b\u5982\u679c\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\uff0c<code>__name__<\/code>\u7684\u503c\u662f<code>&quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528if <strong>name<\/strong> == &quot;<strong>main<\/strong>&quot;<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u5728\u6a21\u5757\u4e2d\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;:<\/code>\u5757\u6765\u7f16\u5199\u6d4b\u8bd5\u4ee3\u7801\u3002\u8fd9\u90e8\u5206\u4ee3\u7801\u4ec5\u5728\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\u6267\u884c\uff0c\u800c\u5728\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u6267\u884c\u3002<\/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>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6a21\u5757\u7684\u8def\u5f84\u548c\u641c\u7d22<\/h3>\n<\/p>\n<p><p>\u5f53\u60a8\u5bfc\u5165\u6a21\u5757\u65f6\uff0cPython\u4f1a\u5728\u4e00\u7cfb\u5217\u76ee\u5f55\u4e2d\u641c\u7d22\u6a21\u5757\u3002\u8fd9\u4e9b\u76ee\u5f55\u7531<code>sys.path<\/code>\u5217\u8868\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h4>1. \u67e5\u770b\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7684\u6a21\u5757\u641c\u7d22\u8def\u5f84\uff1a<\/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<p><h4>2. \u4fee\u6539\u6a21\u5757\u641c\u7d22\u8def\u5f84<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>sys.path<\/code>\u5217\u8868\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u641c\u7d22\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>sys.path.append(&#39;\/path\/to\/custom\/modules&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u5757\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>Python\u4f7f\u7528\u6a21\u5757\u7f13\u5b58\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u5f53\u6a21\u5757\u9996\u6b21\u88ab\u5bfc\u5165\u65f6\uff0cPython\u4f1a\u5728<code>sys.modules<\/code>\u4e2d\u7f13\u5b58\u8be5\u6a21\u5757\u3002\u540e\u7eed\u5bfc\u5165\u5c06\u4f7f\u7528\u7f13\u5b58\uff0c\u800c\u4e0d\u662f\u91cd\u65b0\u52a0\u8f7d\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>sys.modules<\/code>\u8bbf\u95ee\u6a21\u5757\u7f13\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import mymodule<\/p>\n<p>print(sys.modules[&#39;mymodule&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5e38\u7528\u7684\u6807\u51c6\u5e93\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff0c\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u4efb\u52a1\uff0c\u5982\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u7f16\u7a0b\u3001\u6570\u636e\u89e3\u6790\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u6807\u51c6\u5e93\u6a21\u5757\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>os<\/code>: \u63d0\u4f9b\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u529f\u80fd\u3002<\/li>\n<li><code>sys<\/code>: \u63d0\u4f9b\u4e0ePython\u89e3\u91ca\u5668\u76f8\u5173\u7684\u529f\u80fd\u3002<\/li>\n<li><code>math<\/code>: \u63d0\u4f9b\u6570\u5b66\u51fd\u6570\u3002<\/li>\n<li><code>datetime<\/code>: \u63d0\u4f9b\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u529f\u80fd\u3002<\/li>\n<li><code>json<\/code>: \u63d0\u4f9bJSON\u7f16\u7801\u548c\u89e3\u7801\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u7f16\u5199\u53ef\u91cd\u7528\u7684\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u6a21\u5757\u65f6\uff0c\u8003\u8651\u6a21\u5757\u7684\u901a\u7528\u6027\u548c\u53ef\u91cd\u7528\u6027\u662f\u5f88\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7f16\u5199\u53ef\u91cd\u7528\u6a21\u5757\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6e05\u6670\u7684\u547d\u540d<\/h4>\n<\/p>\n<p><p>\u4e3a\u6a21\u5757\u3001\u51fd\u6570\u548c\u53d8\u91cf\u9009\u62e9\u6e05\u6670\u4e14\u63cf\u8ff0\u6027\u7684\u540d\u79f0\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u7406\u89e3\u6a21\u5757\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2. \u63d0\u4f9b\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u548c\u51fd\u6570\u4e2d\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u8bf4\u660e\u5176\u7528\u9014\u548c\u4f7f\u7528\u65b9\u6cd5\u3002\u8fd9\u6709\u52a9\u4e8e\u5176\u4ed6\u5f00\u53d1\u8005\u5feb\u901f\u4e86\u89e3\u6a21\u5757\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;Return a greeting message for the given name.&quot;&quot;&quot;<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u907f\u514d\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4ee5\u51cf\u5c11\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u6027\u548c\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h4>4. \u63d0\u4f9b\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u4e3a\u6a21\u5757\u63d0\u4f9b\u6d4b\u8bd5\u4ee3\u7801\uff0c\u786e\u4fdd\u6a21\u5757\u7684\u529f\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>from mymodule import greet<\/p>\n<p>class TestMyModule(unittest.TestCase):<\/p>\n<p>    def test_greet(self):<\/p>\n<p>        self.assertEqual(greet(&quot;Alice&quot;), &quot;Hello, Alice!&quot;)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u5bf9\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528Python\u6a21\u5757\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002\u6a21\u5757\u662fPython\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u901a\u8fc7\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u81ea\u5b9a\u4e49\u6a21\u5757\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u5c06\u4f60\u7684Python\u4ee3\u7801\u4fdd\u5b58\u5728\u4e00\u4e2a\u4ee5\u201c.py\u201d\u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982\u201cmymodule.py\u201d\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u8981\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\uff0c\u53ea\u9700\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\u901a\u8fc7\u201cimport mymodule\u201d\u6765\u5bfc\u5165\u5b83\u3002\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u8bbf\u95ee\u6a21\u5757\u4e2d\u5b9a\u4e49\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u6a21\u5757\u4e0e\u5305\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u6a21\u5757\u662f\u4e00\u4e2a\u5355\u72ec\u7684Python\u6587\u4ef6\uff0c\u800c\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u6587\u4ef6\u5939\u3002\u5305\u4e2d\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u540d\u4e3a\u201c<strong>init<\/strong>.py\u201d\u7684\u6587\u4ef6\uff0c\u4ee5\u4fbfPython\u5c06\u5176\u89c6\u4e3a\u4e00\u4e2a\u5305\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5305\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u7ef4\u62a4\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\u800c\u4e0d\u662f\u6574\u4e2a\u6a21\u5757\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u53ea\u60f3\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u51fd\u6570\u6216\u7c7b\uff0c\u53ef\u4ee5\u4f7f\u7528\u201cfrom &#8230; import &#8230;\u201d\u8bed\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728\u201cmymodule.py\u201d\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a\u201cmyfunction\u201d\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u201cfrom mymodule import myfunction\u201d\u6765\u76f4\u63a5\u5bfc\u5165\u8fd9\u4e2a\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u907f\u514d\u4e86\u6bcf\u6b21\u8c03\u7528\u65f6\u90fd\u8981\u4f7f\u7528\u6a21\u5757\u540d\u7684\u9ebb\u70e6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5b9a\u4e49Python\u6a21\u5757\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u521b\u5efa.py\u6587\u4ef6\u3001\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u3001\u7ec4\u7ec7\u4ee3\u7801\u3002 Python\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPytho [&hellip;]","protected":false},"author":3,"featured_media":940649,"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\/940646"}],"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=940646"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940646\/revisions"}],"predecessor-version":[{"id":940650,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/940646\/revisions\/940650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/940649"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=940646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=940646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=940646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}