\u6587\u4ef6 module_b\/file_b.py \u4e2d\u9700\u8981\u5bfc\u5165 module_a\/file_a.py \u4e2d\u7684\u51fd\u6570 func_a<\/p>\n
# \u63a8\u8350 \u7edd\u5bf9\u5bfc\u5165 \u4ece\u9876\u5c42\u76ee\u5f55\u5bfc\u5165\r\nfrom module_a.file_a import func_a\r\n\r\n# \u4e0d\u63a8\u8350 \u76f8\u5bf9\u5bfc\u5165\r\nfrom ..module_a import func_a<\/pre>\n\u6709\u65f6\u5019\u5728PyCharm\u4e2d\u53ef\u4ee5\u8fd0\u884c\uff0c\u800c\u4ece\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u8fd0\u884c\u5374\u8fd0\u884c\u4e0d\u8d77\u6765<\/p>\n
\u5982\u679c\u5728PyCharm\u4e2d\u8fd0\u884c\u6587\u4ef6\uff0c\u6216\u8005\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\u8fd0\u884c\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u8d77\u6765<\/p>\n
$ cd demo_project\r\n$ python3 module_b\/file_b.py \r\nfunc_a<\/pre>\n\u5982\u679c\u8fdb\u5165\u5230module_b\u76ee\u5f55\uff0c\u8fd0\u884cfile_b.py\uff0c\u5c31\u4f1a\u62a5\u5bfc\u5305\u9519\u8bef<\/p>\n
$ cd module_b\r\n$ python3 file_b.py\r\nTraceback (most recent call last):\r\n File \"file_b.py\", line 2, in \r\n from module_a.file_a import func_a\r\nModuleNotFoundError: No module named 'module_a'<\/module><\/pre>\n\u67e5\u770bPYTHONPATH\u53d8\u91cf\uff0c\u53d1\u73b0\u5f53\u524d\u76ee\u5f55\u662f\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u91cc\u7684<\/p>\n
$ echo $PYTHONPATH\r\n.:<\/pre>\n\u5176\u5b9e\uff0c\u4e00\u822c\u6211\u90fd\u4f1a\u5c06\u5f53\u524d\u8fd0\u884c\u7684\u76ee\u5f55\u8bbe\u7f6e\u4e3a\u73af\u5883\u53d8\u91cf<\/p>\n
$ more ~\/.bash_profile\r\n\r\nexport PYTHONPATH=.:$PYTHONPATH<\/pre>\n\u6240\u4ee5\uff0c\u53ea\u9700\u8981\u5c06\u9879\u76ee\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u4e2d\u53ef\u89e3\u51b3\u5b50\u76ee\u5f55\u4e2d\u8fd0\u884c\uff0c\u5bfc\u81f4\u627e\u4e0d\u5230\u5305\u7684\u95ee\u9898<\/p>\n
# -*- coding: utf-8 -*-\r\nimport os\r\nimport sys\r\n\r\n# \u6839\u636e\u5b9e\u9645\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\uff0c\u5c06\u8fd0\u884c\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u4e2d\r\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\r\n# \u6ce8\u610f\uff0c\u6b64\u5904\u4e0d\u8981\u4f7f\u7528append\r\nsys.path.insert(0, BASE_DIR)<\/pre>\n\u52a0\u5165\u73af\u5883\u53d8\u91cf\u5b8c\u6574\u4ee3\u7801<\/p>\n
# -*- coding: utf-8 -*-\r\nimport os\r\nimport sys\r\n\r\n# \u6839\u636e\u5b9e\u9645\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\uff0c\u5c06\u8fd0\u884c\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf\u4e2d\r\nBASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\r\n# \u6ce8\u610f\uff0c\u6b64\u5904\u4e0d\u8981\u4f7f\u7528append\r\nsys.path.insert(0, BASE_DIR)\r\n\r\nfrom module_a.file_a import func_a\r\n\r\n\r\ndef func_b():\r\n func_a()\r\n\r\n\r\nif __name__ == '__main__':\r\n func_b()<\/pre>\n\u518d\u6b21\u8fd0\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u6210\u529f\u8c03\u7528<\/p>\n
$ cd module_b\r\n$ python3 file_b.py\r\nfunc_a<\/pre>\n\u5f53\u7136\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u4ece\u9879\u76ee\u76ee\u5f55\u8fd0\u884c\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u505a\u5355\u72ec\u7684\u8bbe\u7f6e<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6709\u5982\u4e0b\u76ee\u5f55 demo_project\/ module_a\/ file_a.py [func_a] module […]<\/p>\n","protected":false},"author":668,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-233314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233314"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233314\/revisions"}],"predecessor-version":[{"id":233318,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233314\/revisions\/233318"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}