首页 > 编程 > Python > 正文

Python父目录、子目录的相互调用方法

2019-11-25 13:17:01
字体:
来源:转载
供稿:网友

最近在使用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 showdatafrom test1.a import plusfrom test2.b import showshowdata()show()plus()

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

如何c.py想要调用另一个文件夹的a的话,需要加上sys.path.apend(“..”)

c.py

import syssys.path.append("..")from test1 import a as tt.showdata()

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

以上这篇Python父目录、子目录的相互调用方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表