{"id":124718,"date":"2020-06-26T19:43:48","date_gmt":"2020-06-26T11:43:48","guid":{"rendered":"http:\/\/4563.org\/?p=124718"},"modified":"2020-06-26T19:43:48","modified_gmt":"2020-06-26T11:43:48","slug":"%e9%97%ae%e4%b8%aa%e5%a5%bd%e5%9f%ba%e7%a1%80%e7%9a%84-import-%e8%b7%af%e5%be%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=124718","title":{"rendered":"\u95ee\u4e2a\u597d\u57fa\u7840\u7684 import \u8def\u5f84\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u95ee\u4e2a\u597d\u57fa\u7840\u7684 import \u8def\u5f84\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u76ee\u5f55\u7ed3\u6784<br \/>folder:<br \/>&#8211;common.py<br \/>&#8211;A<br \/>&#8212;-__init__.py 0 \u5b57\u8282\u7a7a\u6587\u4ef6<br \/>&#8212;-a.py<br \/>&#8211;B<br \/>&#8212;-__init__.py 0 \u5b57\u8282\u7a7a\u6587\u4ef6<br \/>&#8212;-b.py<br \/>\u5c31\u662f folder \u4e3a\u6839\u76ee\u5f55\uff0cA \u548c B \u662f folder \u7684\u5b50\u76ee\u5f55<\/p>\n<p>common.py \u91cc\u9762<br \/>from A import &#8230;<br \/>&#8230;<br \/>\u89e3\u91ca\u4e00\u4e0b\uff0ccommon \u4f1a\u88ab\u591a\u4e2a\u6a21\u5757\u8c03\u7528\uff0c\u7b97\u662f\u524d\u7f6e\uff0c\u5b83\u90e8\u5206\u5185\u5bb9\u4ece A.a \u83b7\u53d6\u5e76\u8ba1\u7b97<\/p>\n<p>b.py \u91cc\u9762<br \/>import sys<br \/>sys.path.append(&#8216;..&#8217;)<br \/>from common import &#8230;<br \/>&#8230;<br \/>if __name__ == &#8216;__main__&#8217;:<br \/>&#8230;<\/p>\n<p>\u73b0\u5728\u60c5\u51b5\uff1a<br \/>folder&gt; py .\/B\/b.py <br \/>\u201c\u627e\u4e0d\u5230 commn \u6a21\u5757\uff0cb.py \u7b2c n \u884c\u201d<br \/>B&gt; py .\/b.py<br \/>\u201c\u627e\u4e0d\u5230 A \u6a21\u5757\uff0ccommon.py \u7b2c n \u884c\u201d<\/p>\n<p>\u6682\u65f6\u7528\u5176\u4ed6\u65b9\u6848\u662f\u89e3\u51b3\u4e86\uff0c\u4f46\u60f3\u77e5\u9053\uff1a<br \/>1.b.py \u80fd\u5426\u76f4\u63a5\u8fd0\u884c\uff1f<br \/>2.\u8fd9\u79cd\u60c5\u51b5\uff0cb.py \u8be5\u600e\u4e48\u5199 import \uff1f      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2127639\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaolinjia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             b.py \u91cc\u8981\u5199<\/p>\n<p>sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))<\/p>\n<p>\u76f8\u5f53\u4e8e\u5c06 folder \u52a0\u5165\u5230 Python \u8def\u5f84\u4e0b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2127640\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u505a\u6cd5\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u4e60\u60ef\uff0c\u4f46\u662f\u5199\u4ee3\u7801\u96be\u514d\u4f1a\u9047\u5230\u3002<br \/>1 \u7684\u4ee3\u7801\u65f6\u6b63\u786e\u7684\uff0c\u4e0d\u8fc7\u6211\u66f4\u559c\u6b22\u7528\u8fd9\u79cd\u5199\u6cd5<\/p>\n<p>from pathlib import Path<br \/>import sys<br \/>sys.path.append(Path(__file__).parent.parent.absolute().__str__())<\/p>\n<p>from common import *<\/p>\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\u975e\u5e38\u7684 dirty\uff0c\u6392\u5e8f\u5f15\u7528\u65f6\u4f1a\u7834\u574f\u4ee3\u7801\u7ed3\u6784\uff0c\u770b\u8d77\u6765\u4e5f\u5f88\u4e71\u3002<\/p>\n<p>\u8bb0\u4f4f\u4e00\u4e2a\u6838\u5fc3\uff0c\u628a\u9879\u76ee\u7684\u6839\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84\u52a0\u5165\u5230 path \u4e2d\u5c31\u884c\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2127641\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Vegetable <br \/>@xiaolinjia <br \/>\u5c31\u662f\u8bf4\u5bfc\u5165\u7edd\u5bf9\u8def\u5f84\u4e48\uff1f\u8fd9\u4e48\u8bf4\u6211\u61c2\u4e86\uff0c\u8c22\u8c22<\/p>\n<p>\u6211\u53e6\u5916\u4e00\u4e9b\u6a21\u5757\u662f\u7528\u7edd\u5bf9\u8def\u5f84\u5bfc\u5165\u7684\uff0c\u53ea\u662f\u90a3\u4e9b\u6a21\u5757\u662f\u591a\u4e2a\u9879\u76ee\u53ef\u7528\u7684<br \/>\u5355\u4e2a\u9879\u76ee\u5185\u4e5f\u7528\u7edd\u5bf9\u8def\u5f84\uff0c\u8fd9\u4e2a\u662f\u6ca1\u8f6c\u8fc7\u5f2f\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2127642\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JCZ2MkKb5S8ZX9pq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u6211\u4e00\u5f00\u59cb\u4e5f\u7ed5\u8fc7\u3002<\/p>\n<p>\u73b0\u5728\u662f\u9879\u76ee\u76f8\u5173\u7684\u8d70\u6839\u76ee\u5f55\uff0c\u6bd4\u5982 from config import *<\/p>\n<p>\u81ea\u6709\u901a\u7528\u5de5\u5177\u90fd\u653e\u4e00\u4e2a\u76ee\u5f55\uff0c\u6bd4\u5982 util\uff0c\u52a0\u5230 path \u3002<br \/>\u7136\u540e from util.atool import atool                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2127643\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JCZ2MkKb5S8ZX9pq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53e6\u5916\u6a21\u5757\u5316\u8c03\u7528<br \/>folder&gt; py .\/B\/b.py <br \/>\u53d8\u6210<br \/>folder&gt; python -m b.b<\/p>\n<p>\u7f3a\u70b9\u662f\u6709\u65f6\u5fd8\u4e86\u4f1a\u8c03\u7528\u9519\u8bef\uff0c\u4f46\u76f4\u63a5\u96c6\u6210\u5230 sublime \u7684 build \u4e86\uff0c\u5c31\u95ee\u9898\u4e0d\u5927\u3002\u800c\u4e14\u81ea\u90a3\u4ee5\u540e\uff0c\u9879\u76ee\u7ed3\u6784\u6709\u6539\u597d\u4e00\u70b9\uff0c\u4e00\u5f00\u59cb\u90fd\u5e73\u7ea7\u76ee\u5f55\u4e00\u5806 py\u2026\u2026                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u4e2a\u597d\u57fa\u7840\u7684 import \u8def\u5f84\u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124718"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124718"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124718\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124718"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}