最近在使用Python的过程中经常遇到找不到该模块的问题。其中一个就是父目录子目录之间相互调用的情况。下面简单总结下。
我们在F:/Code文件夹下面创建一个test文件夹
而test文件夹里面如下
包含两个子目录
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 showdatafrom test1.a import plusfrom test2.b import showshowdata()show()plus()
如何c.py想要调用另一个文件夹的a的话,需要加上sys.path.apend(“..”)
c.py
import syssys.path.append("..")from test1 import a as tt.showdata()
以上这篇Python父目录、子目录的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。
新闻热点
疑难解答
图片精选