{"id":4468,"date":"2019-06-15T08:46:46","date_gmt":"2019-06-15T00:46:46","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4468"},"modified":"2019-06-15T08:46:46","modified_gmt":"2019-06-15T00:46:46","slug":"python%e4%b8%ad%e7%9a%84%e6%a8%a1%e5%9d%97%e5%bc%95%e5%85%a5","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4468.html","title":{"rendered":"Python\u4e2d\u7684\u6a21\u5757\u5f15\u5165"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6574\u7406\u4e00\u4e0bPython\u6a21\u5757\u5f15\u5165\u76f8\u5173\u7684\u5c0f\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p><strong>\u5728\u5f15\u5165\u6a21\u5757\u65f6\uff0c\u6700\u597d\u6309\u7167\u4ee5\u4e0b\u987a\u5e8f\u8fdb\u884cimport\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u6807\u51c6\u5e93\u6a21\u5757\uff08\u6bd4\u5982\uff1asys\/os\/re\u7b49\uff09\uff1b<\/li><li>\u7b2c\u4e09\u65b9\u5e93\u6a21\u5757\uff08\u6bd4\u5982\uff1aMySQLdb\/requests\u7b49\uff09\uff1b<\/li><li>\u672c\u5730\u81ea\u5f00\u53d1\u6a21\u5757\uff1b<\/li><\/ol>\n\n\n\n<p><strong>\u5177\u4f53\u5230\u67d0\u4e00\u6a21\u5757\u7684import\u65b9\u5f0f\u4e00\u822c\u6709\u4ee5\u4e0b3\u79cd\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pandas<br>from pandas import DataFrame<br>from pandas import *<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u5728\u8fd9\u91cc\u63a8\u8350\u7528\u7b2c\u4e00\u79cd<\/strong>\uff0c\u867d\u7136\u5728\u4f7f\u7528\u7684\u65f6\u5019\u8981\u591a\u6572\u4e00\u4e9b\u5b57\u7b26\uff0c\u6bd4\u5982\uff1apandas.foo\uff0c\u4f46\u662f\u8fd9\u6837<strong>\u6709\u72ec\u7acb\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e0d\u6613\u5f15\u8d77\u6b67\u4e49<\/strong>\uff1b<\/li><li>\u5176\u6b21\u63a8\u8350\u7b2c\u4e8c\u79cd\uff0c\u4f46\u5728\u672c\u5730\u4ee3\u7801\u4e2d\u8981\u6ce8\u610f\u907f\u514d\u91cd\u540d\uff0c\u5426\u5219\u53ef\u80fd\u51fa\u73b0\u975e\u9884\u671f\u7684\u60c5\u51b5\u3002<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\"><strong>Python\u4e2d\u7684 sys.path.append() \u548c sys.path.insert()<\/strong><\/h6>\n\n\n\n<p>Python\u7a0b\u5e8f\u4e2d\u4f7f\u7528 import xxx \u65f6\uff0cPython\u89e3\u6790\u5668\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u3001Python\u6807\u51c6\u5e93\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u6a21\u5757\u76ee\u5f55\u4e2d\u641c\u7d22 xxx\uff0c\u5982\u679c\u90fd\u641c\u7d22\u4e0d\u5230\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528sys.path.append()\u65b9\u6cd5\u53ef\u4ee5\u4e34\u65f6\u6dfb\u52a0\u641c\u7d22\u8def\u5f84\uff0c\u65b9\u4fbf\u66f4\u7b80\u6d01\u7684import\u5176\u4ed6\u5305\u548c\u6a21\u5757\u3002\u8fd9\u79cd\u65b9\u6cd5\u5bfc\u5165\u7684\u8def\u5f84\u4f1a\u5728Python\u7a0b\u5e8f\u9000\u51fa\u540e\u5931\u6548\u3002<\/p>\n\n\n\n<p><strong># 1. \u5c06\u6307\u5b9a\u7684\u300c\u76f8\u5bf9\u76ee\u5f55\u300d\u548c\u300c\u7edd\u5bf9\u76ee\u5f55\u300d\u52a0\u5165\u641c\u7d22\u8def\u5f84\u4e2d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import sys<br>sys.path.append('..') #\u8868\u793a\u5bfc\u5165\u5f53\u524d\u6587\u4ef6\u7684\u4e0a\u5c42\u76ee\u5f55\u5230\u641c\u7d22\u8def\u5f84\u4e2d<br>sys.path.append('\/home\/model') # \u7edd\u5bf9\u8def\u5f84<br>from folderA.folderB.fileA import functionA<\/pre>\n\n\n\n<p><strong># 2. \u5c06\u300c\u5f53\u524d\u76ee\u5f55\u300d\u52a0\u5165\u641c\u7d22\u8def\u5f84\u4e2d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os,sys<br>sys.path.append(os.getcwd())<\/pre>\n\n\n\n<p><strong># 3. \u5b9a\u4e49\u641c\u7d22\u4f18\u5148\u987a\u5e8f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import sys<br>sys.path.insert(1, \".\/model\")<\/pre>\n\n\n\n<p><code>sys.path.insert(1, \".\/model\")<\/code>\u5b9a\u4e49\u4e86\u6dfb\u52a0\u7684\u641c\u7d22\u8def\u5f84\u7684\u4f18\u5148\u987a\u5e8f\uff0c\u5e8f\u53f7\u4ece0\u5f00\u59cb\uff0c\u8868\u793a\u6700\u5927\u4f18\u5148\u7ea7\uff0csys.path.insert()\u52a0\u5165\u7684\u4e5f\u662f\u4e34\u65f6\u641c\u7d22\u8def\u5f84\uff0c\u7a0b\u5e8f\u9000\u51fa\u540e\u5931\u6548\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><a href=\"http:\/\/effbot.org\/pyfaq\/what-are-the-best-practices-for-using-import-in-a-module.htm\">What are the &#8220;best practices&#8221; for using import in a module?<\/a><\/strong><\/li><li><strong><a href=\"https:\/\/stackoverflow.com\/questions\/9916878\/importing-modules-in-python-best-practice\">Importing modules in Python &#8211; best practice<\/a><\/strong><\/li><li><a href=\"https:\/\/realpython.com\/absolute-vs-relative-python-imports\/\">Absolute vs Relative Imports in Python<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/dcrmg\/article\/details\/79546962\"><strong>Python\u4e2d\u7684 sys.path.append() \u548c sys.path.insert()<\/strong><\/a><br><a href=\"https:\/\/www.cnblogs.com\/mtcnn\/p\/9411730.html\">https:\/\/www.cnblogs.com\/mtcnn\/p\/9411730.html<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/qq_27923041\/article\/details\/72878635\">sys.path.insert()\u7528\u6cd5<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/31291608\/effect-of-using-sys-path-insert0-path-and-sys-pathappend-when-loading-modul\">https:\/\/stackoverflow.com\/questions\/31291608\/effect-of-using-sys-path-insert0-path-and-sys-pathappend-when-loading-modul<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/10095037\/why-use-sys-path-appendpath-instead-of-sys-path-insert1-path\">https:\/\/stackoverflow.com\/questions\/10095037\/why-use-sys-path-appendpath-instead-of-sys-path-insert1-path<\/a><\/li><li><a href=\"https:\/\/askubuntu.com\/questions\/470982\/how-to-add-a-python-module-to-syspath\">https:\/\/askubuntu.com\/questions\/470982\/how-to-add-a-python-module-to-syspath<\/a><\/li><li><a href=\"https:\/\/pymotw.com\/2\/sys\/imports.html\">Modules and Imports<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u4e00\u4e0bPython\u6a21\u5757\u5f15\u5165\u76f8\u5173\u7684\u5c0f\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u548c\u4f7f\u7528\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1420,8,1385,1421,1422],"class_list":["post-4468","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-import","tag-python","tag-sys","tag-sys-path-append","tag-sys-path-insert"],"views":3886,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4468"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4468\/revisions"}],"predecessor-version":[{"id":4469,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4468\/revisions\/4469"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}