Module calls in python

1 Introduction

Often we need to call each other each module in python, will appear the following situations:

Call (1) of files in the same directory

(2) call the parent directory files in subdirectories

Call (3) of files in the same directory

2. Example

The conventional directory structure: AB have A, B are two subdirectories, AB has the file itself AB1.py

__ init__ .py: empty file

B1.py: there are two functions getb1 () and getb11 ()

Here Insert Picture Description

(1) file calls in the same directory, we will not repeat them.

(2) call the parent directory subdirectories

For example AB in AB1.py call B1.py file B

AB1.py:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# -*- coding=utf-8 -*-
# 父目录调用子目录,可以使用以下两种方式

# 方式一
from B import B1
B1.getb1()

# 方式二
from B.B1 import getb1
getb1()

from B.B1 import *
getb1()
getb11()

Call (3) of files in the same directory

For example A1.py file A file B calls B1.py

A1.py:

# -×- coding=utf-8 -*-

import sys
from B.B1 import *
# 写入相对路径或绝对路径
sys.path.append('../B')
# sys.path.append('C:\\Study\\python\\AB\\B')

getb1()
getb11()
Published 706 original articles · won praise 731 · Views 1.01 million +

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/103962772