导语:
本文主要介绍了关于python Package如何设置文件入口的相关知识,包括python添加到环境变量,以及python自定义包的导入这些编程知识,希望对大家有参考作用。
1、说明
(1)Python中的包是一个模块的文件夹,文件夹通常被__init__.py指定为包;
(2)Package可以为某个目录下所有的文件设置统一入口。
2、实例
someDir/
main.py
subModules/
__init__.py
subA.py
subSubModules/
__init__.py
subSubA.py
# subA.py
def subAFun():
print('Hello from subAFun')
def subAFunTwo():
print('Hello from subAFunTwo')
# subSubA.py
def subSubAFun():
print('Hello from subSubAFun')
def subSubAFunTwo():
print('Hello from subSubAFunTwo')
# __init__.py from subDir
# Adds 'subAFun()' and 'subAFunTwo()' to the 'subDir' namespace
from .subA import *
# The following two import statement do the same thing, they add 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace. The first one assumes '__init__.py' is empty in 'subSubDir', and the second one, assumes '__init__.py' in 'subSubDir' contains 'from .subSubA import *'.
# Assumes '__init__.py' is empty in 'subSubDir'
# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
from .subSubDir.subSubA import *
# Assumes '__init__.py' in 'subSubDir' has 'from .subSubA import *'
# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespace
from .subSubDir import *
# __init__.py from subSubDir
# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subSubDir' namespace
from .subSubA import *
# main.py
import subDir
subDir.subAFun() # Hello from subAFun
subDir.subAFunTwo() # Hello from subAFunTwo
subDir.subSubAFun() # Hello from subSubAFun
subDir.subSubAFunTwo() # Hello from subSubAFunTwo
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
本文为原创文章,版权归知行编程网所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ python用什么编辑器09/14
- ♥ 什么是 python 对象方法12/20
- ♥ python3库安装后无法使用怎么解决?11/21
- ♥ 熟练使用 Python 字典处理索引统计10/31
- ♥ 如何在 Python 中编写计时器12/25
- ♥ python运行图标消失怎么办09/03
内容反馈