Python父目录、子目录相互调用

最近在使用Python的过程中经常遇到找不到该模块的问题。其中一个就是父目录子目录之间相互调用的情况。下面简单总结下。
《Python父目录、子目录相互调用》
我们在F:\Code文件夹下面创建一个test文件夹
而test文件夹里面如下
《Python父目录、子目录相互调用》
包含两个子目录
《Python父目录、子目录相互调用》
a.py

def showdata():
    print("this is a")

def plus():
    a=1
    b=2
    print(a+b)

b.py

def show():
    print("this is b")

从父目路test.py调用a和b

from test1.a import showdata
from test1.a import plus
from test2.b import  show

showdata()
show()
plus()

《Python父目录、子目录相互调用》
如何c.py想要调用另一个文件夹的a的话,需要加上sys.path.apend(“..”)
c.py

import sys

sys.path.append("..")
from test1 import a as t

t.showdata()

《Python父目录、子目录相互调用》

    原文作者:分支限界法
    原文地址: https://blog.csdn.net/gzj_1101/article/details/78555402
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞