Introducing embodiment Python package (per)



   Description:
Code execution environment python3 under PyCharm2019.1 IDE platform is based on



using a desktop browser to browse normal

Code directory structure of FIG.

(This directory is also a link in the form of explanatory text picture, you can browse click the jump; hereinafter the same)



# -*- coding:utf-8 -*-
def aPrint() -> object:
    print("This is a print test from A.py")

def sayHello() -> object:
    print("Helllo my friend long time no see - saying from A.py")
    
A.py file

# -*- coding:utf-8 -*-
def bPrint():
    print("This is a print test from B.py")

def sayHello() -> object:
    print("Helllo my friend long time no see - saying from B.py")
B.py file

# -*- coding:utf-8 -*-
def cPrint():
    print("This is a print test from C.py")

def sayHello() -> object:
    print("Helllo my friend long time no see - saying from C.py")
C.py file
# __init__.py文件,里面可以写import C 时要执行的代码,当然也可以留空
# 不过往往可以用于进行预处理操作
print("information output from __init__.py in dirPagC.")
# 值得注意的是:import C 若是通过绝对路径添加、在静态编译时无法识别时的话
# __init__.py中的代码不会被执行
# 另外多次 import 也只会是默认执行且只执行一次 __init__.py中的代码
In contrast with the __init__.py file directory

# -*- coding:utf-8 -*-
import A

A.aPrint()
callA1.py file

# -*- coding:utf-8 -*-
from A import aPrint

aPrint()
callA2.py file

# -*- coding:utf-8 -*-

# 调用文件与被调用文件所在的目录并列时,调用格式需要添加路径
# 由于是并列目录,故共同的“根”路径可以不写,即:
# dirName 其本质是 需要导入的python包的绝对路径名而非仅仅是所处的文件夹名
# 只不过此处是使用了基于当前文件夹目录下的相对路径来替代表示而已
######### 1 ##########
# # import dirName.pyFileName
# import dirB.B
# dirB.B.bPrint()
########## 2 ##########
# # form dirName import pyFileName
# from dirB import B
# B.bPrint()
########## 3 ##########
# # from dirName.pyFileName import funtionName
# from dirB.B import bPrint
# bPrint()
########## 4 ##########
import sys
sys.path.append('E:\Code\Python\MyMoudle\dirB')
import B
B.bPrint()
# 绝对路径是通用的包引入办法
# 引入的包在静态编译时是无法识别的
# 动态编译后才会识别,能够正常运行

########## 5 ##########
# # import sys
# # sys.path.append('E:\Code\Python\MyMoudle')
# import dirB.B
# dirB.B.bPrint()
# 这第5种方式与第一种其实是一个样子
# 实在要灵活运用,可以看一下第6种方式
########## 6 ##########
import sys
sys.path.append('E:\Code')
import Python.MyMoudle.dirB.B
Python.MyMoudle.dirB.B.bPrint()
# 需要说明的是,以下方式目前的编译器暂时不支持
# sys.path.append('E:')
# import Code.Python.MyMoudle.dirB.B
# Code.Python.MyMoudle.dirB.B.bPrint()
# 即:sys.path.append添加的路径最终必须是落在某个文件夹里
# 而绝非可以是整个磁盘的添加
callB.py file
Code View to add

# -*- coding:utf-8 -*-

# 调用文件与被调用文件所在的目录并列时,调用格式需要添加路径
# 由于是并列目录,故共同的“根”路径可以不写,即:
# dirName 其本质是 需要导入的python包的绝对路径名而非仅仅是所处的文件夹名
# 只不过此处是使用了基于当前文件夹目录下的相对路径来替代表示而已

# ######## 1 ##########
# # import dirName.pyFileName
import dirPagC.C
dirPagC.C.cPrint()
# ######### 2 ##########
# # form dirName import pyFileName
from dirPagC import C
C.cPrint()
# ######### 3 ##########
# # from dirName.pyFileName import funtionName
from dirPagC.C import cPrint
cPrint()
# # ######### 4 #########
import sys
sys.path.append('E:\Code\Python\MyMoudle\dirPagC')
import C
C.cPrint()
# 绝对路径是通用的包引入办法
# 引入的包在静态编译时是无法识别的
# 动态编译后才会识别,能够正常运行

callC.py file
Code View to add

# -*- coding:utf-8 -*-

# dirName 其本质是 需要导入的python包的绝对路径名而非仅仅是所处的文件夹名
# 只不过此处是使用了基于当前文件夹目录下的相对路径来替代表示而已
# 说到底就两种类别
# 1. import (dir.)pyFileName
# 使用py文件下的多个方法
# 2. from (dir.)pyFileName import funtionName
# 使用py文件下的指定方法

# 同级目录或者是子目录往父目录直接调用
# 两者调用格式
########## 1 ##########
# import pyFileName
# import A
# A.aPrint()
########## 2 ##########
# from pyFileName import funtionName
from A import aPrint
aPrint()

# 并列目录的调用,调用格式需要添加路径
# 由于是并列目录,故共同的“根”路径可以不写
########## 1 ##########
# import dirName.pyFileName
# import dirB.B
# dirB.B.bPrint()
########## 2 ##########
# form dirName import pyFileName
# from dirB import B
# B.bPrint()
########## 3 ##########
# from dirName.pyFileName import funtionName
from dirB.B import bPrint
bPrint()

# C.py处于包文件夹dirPagCall中,有别于B.py处于普通文件夹dirCall中
########## 1 ##########
# import packageDirName.pyFileName
# import dirPagC.C
# dirPagC.C.cPrint()
########## 2 ##########
# form packageDirName import pyFileName
# from dirPagC import C
# C.cPrint()
# dirPagC.C.cPrint() # 只能怪识别 import后的内容,故此行代码将会报无法识别dirPagC的错误
########## 3 ##########
# from packageDirName.pyFileName import funtionName
from dirPagC.C import cPrint
cPrint()
# dirPagC.C.cPrint()
# C.cPrint() # 同理,此二行会报无法识别的错误
toCallABC.py file

# -*- coding:utf-8 -*-

# import A
# A.sayHello()
from dirB.B import sayHello
from A import sayHello
sayHello()
sayHello()

# import dirB.B
# dirB.B.sayHello()
# from dirB import B
# B.sayHello()
from dirB.B import sayHello
sayHello()

# import dirPagC.C
# dirPagC.C.sayHello()
# from dirPagC import C
# C.sayHello()
from dirPagC.C import sayHello
sayHello()

# import dirPagC
# dirPagC.C.sayHello()
# 查到的方法说是 这第四种也可以
callABC.py file
The same method on how to use the "namespace"

Analogy can exist in other languages ​​namespace concept (C, C ++, C #, ......) by the method names package to achieve distinction; For the nearest matching principle, the details can be spotted map link

Last added: For __init__.py file

  __init__.py preprocessed code, when executed import, multiple import is executed only once

2019/11/08 00:33



# 补充一种包的引入方式,可能存在争议

import dirB
dirB.B.sayHello()

import  dirPagC
dirPagC.C.sayHello()
Currently there is no difference whether the __init__.py

Direct import folder, the previous statement is: When there is general __init__.py file folder, the folder will become a package, can then be introduced as an ordinary bag, like, import the folder (currently tested, with or without __init__ .py so can be introduced)

above average knowledge through experience the test, if error correction are welcome in the comments section, all subject to the official developer documentation.

2019/11/08 00:52

Published 89 original articles · won praise 159 · views 50000 +

Guess you like

Origin blog.csdn.net/I_love_you_dandan/article/details/102965131