SoFunction
Updated on 2024-11-19

Python parent directory, subdirectory call each other method

Recently in using Python I have often encountered problems with not being able to find the module. One of them is the case where parent directory subdirectories call each other. Here is a brief summary.

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

We create a test folder under the F:\Code folder

And inside the test folder is the following

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

Contains two subdirectories

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

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

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

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

Calls a and b from the parent

from  import showdata
from  import plus
from  import show

showdata()
show()
plus()

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

You need to add ("...") if you want to call the a of another folder.

import sys

("..")
from test1 import a as t

()

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

Above this Python parent directory, subdirectory call each other method is all that I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.