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

最近在使用Python的过程中经常遇到找不到该模块的问题。其中一个就是父目录子目录之间相互调用的情况。下面简单总结下。

我们在F:\Code文件夹下面创建一个test文件夹

而test文件夹里面如下

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

包含两个子目录

a.py

'''
想要学习Python?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!
'''
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()

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

c.py

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

猜你喜欢

转载自blog.csdn.net/fei347795790/article/details/89712005