{"id":486,"date":"2015-08-18T23:08:52","date_gmt":"2015-08-18T15:08:52","guid":{"rendered":"http:\/\/julyclyde.org\/?p=486"},"modified":"2015-08-18T23:08:52","modified_gmt":"2015-08-18T15:08:52","slug":"python-import%e8%af%ad%e5%8f%a5%e4%b8%80%e5%a4%84%e6%96%87%e6%a1%a3%e5%92%8c%e8%a1%8c%e4%b8%ba%e4%b8%8d%e4%b8%80%e8%87%b4%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/julyclyde.org\/?p=486","title":{"rendered":"Python import\u8bed\u53e5\u4e00\u5904\u6587\u6863\u548c\u884c\u4e3a\u4e0d\u4e00\u81f4\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u8bdd\u8bf4\uff0c\u6211\u56e2\u8fd0\u7ef4\u7ec4\u66fe\u4e00\u5ea6\u6781\u4e3a\u660c\u76db\uff0c\u53ef\u60dc\u540e\u6765\u516c\u53f8\u91cc\u6709\u4eba\u641e\u653f\u6cbb\uff0c\u597d\u597d\u7684\u4e00\u4e2a\u7ec4\u88ab\u62c6\u6210\u4e86\u597d\u51e0\u4e2a\uff0c\u540e\u6765\u7adf\u5206\u5c5e\u4e0d\u540c\u5927\u90e8\u95e8\uff0c\u5de5\u4f5c\u5173\u7cfb\u4e5f\u5f00\u59cb\u5f62\u540c\u964c\u8def\u3002\u8003\u8651\u5230\u4e30\u5bcc\u7ecf\u9a8c\uff0c\u6362\u6362\u65b9\u5411\uff0c\u4eca\u5e747\u6708\uff0c\u6211\u8f6c\u5c97\u5230\u4e86\u8fd0\u7ef4\u5f00\u53d1\u7ec4\uff0c\u5f00\u59cb\u771f\u6b63\u628aPython\u4f5c\u4e3a\u804c\u4e1a\u3002<\/p>\n<p>\u65e0\u5948\uff0c\u57fa\u7840\u5dee\uff0c\u8fd8\u5f97\u5b66\uff0c8\u670818\u65e5\uff0c\u8fd9\u4e48\u5409\u5229\u7684\u4e00\u5929\uff0c\u6211\u7b2cn\u6b21\u5728\u770bPython\u7684\u6587\u6863\uff0c\u770b\u7740\u770b\u7740\uff0c\u6ce8\u610f\u5230\u4e00\u4e9b\u6807\u51c6\u5e93\u7684\u540d\u5b57\u662f\u5e26\u70b9\u7684\uff0c\u6bd4\u5982os.path\uff0c\u518d\u6bd4\u5982logging.config\u7b49\u3002\u7136\u540e\u5c31\u5f00\u59cbhelp\u5404\u4e2a\u6a21\u5757\uff0c\u53d1\u73b0os.path\u7684\u540d\u5b57\u5c45\u7136\u4e0d\u53ebpath\u800c\u662fposixpath\uff01\uff01\u518d\u770b\uff0clogging\u662f\u4e2apackage\uff0c\u4f46os\u5c45\u7136\u662f\u4e2amodule\uff01\uff01<\/p>\n<p>\u4e8e\u662f\uff0c\u95ee\u9898\u5c31\u6765\u4e86\uff1a<\/p>\n<p>Python\u8bed\u8a00\u91cc\u5bf9 import A.B.C \u8fd9\u79cd\u5f62\u5f0f\u662f\u8fd9\u4e48\u89c4\u5b9a\u7684\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/docs.python.org\/2.7\/tutorial\/modules.html\">when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can\u2019t be a class or function or variable defined in the previous item.<\/a><\/p><\/blockquote>\n<p>\u4e8e\u662f\u90a3\u5c31\u5947\u602a\u4e86\uff0c\u8fd9\u4e2aos\u65e2\u7136\u4e0d\u662fpackage\uff0c\u600e\u4e48\u80fd import os.path \u5462\uff1f<\/p>\n<p>\u672c\u7740\u6280\u672f\u4e8b\u95ee\u5495\u679c\u7684\u539f\u5219\uff0c\u641c\u5230<a href=\"http:\/\/stackoverflow.com\/questions\/2724348\/should-i-use-import-os-path-or-import-os\">\u8fd9\u4e48\u4e00\u7bc7<\/a>\uff0c\u5176\u4e2d\u63d0\u5230\uff0cpython\u5728\u521d\u59cb\u5316\u65f6\u4f1a\u52a0\u8f7d\u4e00\u5806\u6a21\u5757\uff0c\u5176\u4e2d\u5c31\u5305\u62ecos\uff0c\u800cos\u6a21\u5757\u5224\u65ad\u7cfb\u7edf\u7c7b\u578b\u4e4b\u540e\uff0c\u9009\u5b9a\u4e86posixpath\u6a21\u5757\u6765\u914d\u5408\u5de5\u4f5c\uff0c\u8ba9\u5b83\u5728\u672c\u547d\u540d\u7a7a\u95f4\u91cc\u53ebpath\uff0c\u8fd8\u53c8\u628a\u5b83\u4ee5os.path\u7684\u540d\u5b57\u52a0\u5165\u4e86sys.modules\u4e4b\u4e2d\uff1b\u5f85\u6211\u6267\u884cimport os.path\u65f6\uff0cimport\u8bed\u53e5\u53d1\u73b0sys.modules\u91cc\u5df2\u7ecf\u6709\u4e86\uff0c\u4e8e\u662f\u76f4\u63a5\u505a\u4e2asymbol\u8fc7\u6765\u5c31\u4e86\u4e8b\u3002<\/p>\n<p>\u672c\u6765\u4e8b\u60c5\u5230\u8fd9\u91cc\u5df2\u7ecf\u7ed3\u675f\u4e86\u3002<strong>\u4f46\u662f<\/strong>\u6c34\u6728\u4e0a\u6709\u670b\u53cb\u5728<a href=\"http:\/\/www.newsmth.net\/nForum\/#!article\/Python\/124356?p=1#a1\">\u53c2\u4e0e\u8ba8\u8bba<\/a>\u7684\u65f6\u5019\uff0c\u4e3a\u4e86\u8bf4\u660e\u4e0a\u8ff0\u5176\u5b9e\u662f\u8bed\u8a00\u5f15\u64ce\u7684\u884c\u4e3a\uff0c\u800c\u4e0d\u662fos\u7684\u7279\u6b8a\u6027\u5bfc\u81f4\u7684\uff0c\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<p>a.py\u6587\u4ef6\uff1a<\/p>\n<blockquote><p>import sys<\/p>\n<p>import json as shit<\/p>\n<p>sys.modules[&#8216;a.shit&#8217;] = shit<\/p><\/blockquote>\n<p>\u7136\u540e\u4ece\u53e6\u4e00\u5904import a.shit \u5c45\u7136\u4e5f\u6210\u529f\uff01<\/p>\n<p>\u5982\u679c\u4ecea.py\u91cc\u53bb\u6389\u7b2c\u4e09\u884c\uff0c\u867d\u7136import a.shit\u65f6\u4f1a\u62a5\u544aNameError: name &#8216;a&#8217; is not defined\uff0c\u4f46sys.modules\u91cc\u5df2\u7ecf\u51fa\u73b0\u4e86a\uff0c\u8fd9\u8bf4\u660ea\u6a21\u5757\u5df2\u7ecf\u88abimport\u8fdb\u6765\u4e86\uff01<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u8bf4os.path\u542b\u7740\u91d1\u94a5\u5319\u662f\u56e0\u4e3a\u5b83\u7238\u53ebos\uff0c\u90a3a.shit\u53c8\u7b97\u8001\u51e0\uff0c\u51ed\u4ec0\u4e48\u548cos.path\u4eab\u53d7\u4e00\u6837\u7684\u5f85\u9047\uff1f<\/p>\n<p>\u7ecf\u8fc7\u6211\u7cbe\u5fc3\u6784\u9020\u7684\u5b9e\u9a8c\uff0c\u4e0d\u7528a.py\u6587\u4ef6\uff0c\u800c\u7528\u4e00\u4e2a\u7a7a\u76ee\u5f55a\u4ee3\u66ff\uff0c\u5728python\u91ccimport a.shit\u65f6\uff0c\u7528strace\u5077\u5077\u7684\u5f55\u4e0b\u5b83\u7684\u53ef\u803b\u884c\u5f84\uff0c\u53d1\u73b0python\u5c45\u7136\u5148\u5c1d\u8bd5\u627e\u540d\u4e3aa\u7684package\uff08\u6253\u5f00a\u76ee\u5f55\u627e__init__.py\uff09\u672a\u679c\u4e4b\u540e\u53c8\u5c1d\u8bd5\u627e\u5404\u79cd\u5f62\u5f0f\u6587\u4ef6\u540d\u7684\u540d\u4e3aa\u7684module\u3002<\/p>\n<p>\u6700\u540e\u7684\u7ed3\u8bba\u662f\uff1a\u867d\u7136os.path\u5b83\u7238\u53ebos\uff0c\u4f46\u5176\u5b9e\u5373\u4f7f\u5b83\u7238\u4e0d\u53ebos\uff0c\u4e5f\u7167\u6837\u80fd\u6210\u529fimport\u8fdb\u6765\uff0c\u6839\u6e90\u5728\u4e8eimport\u547d\u4ee4\u5e76\u4e0d\u662f\u50cf\u6587\u6863\u6240\u8bf4\u7684\uff0c\u5bf9 import A.B.C\u8fd9\u79cd\u5f62\u5f0f\uff0c\u575a\u6301\u4e86A.B\u5fc5\u987b\u4e3apackage\u7684\u539f\u5219\uff0c\u800c\u662f\u5bbd\u8fdb\uff08\u8bed\u8a00\u884c\u4e3a\uff09\u4e25\u51fa\uff08\u6587\u6863\u53d9\u8ff0\uff09\uff0c\u8a00\u884c\u4e0d\u4e00\u3002<\/p>\n<p>\u8fd9\u6837\u4e0d\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bdd\u8bf4\uff0c\u6211\u56e2\u8fd0\u7ef4\u7ec4\u66fe\u4e00\u5ea6\u6781\u4e3a\u660c\u76db\uff0c\u53ef\u60dc\u540e\u6765\u516c\u53f8\u91cc\u6709\u4eba\u641e\u653f\u6cbb\uff0c\u597d\u597d\u7684\u4e00\u4e2a\u7ec4\u88ab\u62c6\u6210\u4e86\u597d\u51e0\u4e2a\uff0c\u540e\u6765\u7adf\u5206\u5c5e\u4e0d\u540c\u5927\u90e8\u95e8\uff0c\u5de5\u4f5c\u5173\u7cfb\u4e5f\u5f00\u59cb\u5f62\u540c\u964c\u8def\u3002\u8003\u8651\u5230\u4e30\u5bcc\u7ecf\u9a8c\uff0c\u6362\u6362\u65b9\u5411\uff0c\u4eca\u5e747\u6708\uff0c\u6211\u8f6c\u5c97\u5230\u4e86\u8fd0\u7ef4\u5f00\u53d1\u7ec4\uff0c\u5f00\u59cb\u771f\u6b63\u628aPython\u4f5c\u4e3a\u804c\u4e1a\u3002 \u65e0\u5948\uff0c\u57fa\u7840\u5dee\uff0c\u8fd8\u5f97\u5b66\uff0c8\u670818\u65e5\uff0c\u8fd9\u4e48\u5409\u5229\u7684\u4e00\u5929\uff0c\u6211\u7b2cn\u6b21\u5728\u770bPython\u7684\u6587\u6863\uff0c\u770b\u7740\u770b\u7740\uff0c\u6ce8\u610f\u5230\u4e00\u4e9b\u6807\u51c6\u5e93\u7684\u540d\u5b57\u662f\u5e26\u70b9\u7684\uff0c\u6bd4\u5982os.path\uff0c\u518d\u6bd4\u5982logging.config\u7b49\u3002\u7136\u540e\u5c31\u5f00\u59cbhelp\u5404\u4e2a\u6a21\u5757\uff0c\u53d1\u73b0os.path\u7684\u540d\u5b57\u5c45\u7136\u4e0d\u53ebpath\u800c\u662fposixpath\uff01\uff01\u518d\u770b\uff0clogging\u662f\u4e2apackage\uff0c\u4f46os\u5c45\u7136\u662f\u4e2amodule\uff01\uff01 \u4e8e\u662f\uff0c\u95ee\u9898\u5c31\u6765\u4e86\uff1a Python\u8bed\u8a00\u91cc\u5bf9 import A.B.C \u8fd9\u79cd\u5f62\u5f0f\u662f\u8fd9\u4e48\u89c4\u5b9a\u7684\uff1a when using syntax like import item.subitem.subsubitem, each item except for the last must be a package; the last item can be a module or a package but can\u2019t be a class or function &hellip; <a href=\"https:\/\/julyclyde.org\/?p=486\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[31,27,12],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-default","tag-bugs","tag-python","tag-standardization"],"_links":{"self":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=486"}],"version-history":[{"count":0,"href":"https:\/\/julyclyde.org\/index.php?rest_route=\/wp\/v2\/posts\/486\/revisions"}],"wp:attachment":[{"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/julyclyde.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}