本篇文章给大家分享的是有关怎么在python中利用Package设置文件入口,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
Python主要用来做什么
Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。
1、说明
(1)Python 中的包(Package)则是模块的文件夹,往往由 __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' namespacefrom .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' namespacefrom .subSubDir.subSubA import * # Assumes '__init__.py' in 'subSubDir' has 'from .subSubA import *'# Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subDir' namespacefrom .subSubDir import *# __init__.py from subSubDir # Adds 'subSubAFun()' and 'subSubAFunTwo()' to the 'subSubDir' namespacefrom .subSubA import * # main.py import subDir subDir.subAFun() # Hello from subAFunsubDir.subAFunTwo() # Hello from subAFunTwosubDir.subSubAFun() # Hello from subSubAFunsubDir.subSubAFunTwo() # Hello from subSubAFunTwo
以上就是怎么在python中利用Package设置文件入口,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。