python 模块中的 __init__.py __main__.py
python中文件夹想作为一个模块被引用,则在文件夹内必须要包含 __init__.py 文件,即使此文件为空。
如果此模块想要运行则必须要包含 __main__.py 文件。接下来说下两个文件起到的作用。
拿 robotframework 模块下的文件举例:
__init__.py里面一般包含了需要引用的模块
1 from robot.rebot import rebot, rebot_cli
2 from robot.run import run, run_cli
3 from robot.version import get_version
__all__ 参数意为导出包内模块,以下连接可以参考,不包含在__all__ 列表的模块不可被其他程序引用
此处 __version__ 应为一个系统定义的名字, 可在系统内引用
from robot.rebot import rebot, rebot_cli
from robot.run import run, run_cli
from robot.version import get_version __all__ = ['run', 'run_cli', 'rebot', 'rebot_cli']
__version__ = get_version()
对于 __main__.py 我的理解是一个模块的入口函数执行模块
import sys # Allows running as a script. __name__ check needed with multiprocessing:
# https://github.com/robotframework/robotframework/issues/1137
if 'robot' not in sys.modules and __name__ == '__main__':
import pythonpathsetter from robot import run_cli run_cli(sys.argv[1:])
当我们执行模块代码时首先会加载__init__.py 定义的引入模块,然后进入__mian__.py 文件运行
一下是运行模块的结果,调到了run_cli 的函数进行解析运行
E:\Software\Software\Python2.7.11\Lib\site-packages>python -m robot --help
Robot Framework -- A generic test automation framework
Version: 3.0 (Python 2.7.11 on win32)
Usage: robot [options] data_sources
or: python -m robot [options] data_sources
or: python path/to/robot [options] data_sources
or: java -jar robotframework.jar [options] data_sources
。。。 。。。 。。。 。。。
Options
=======
-N --name name Set the name of the top level test suite. Underscores
in the name are converted to spaces. Default name is
created from the name of the executed data source.
-D --doc documentation Set the documentation of the top level test suite.
Underscores in the documentation are converted to
spaces and it may also contain simple HTML formatting
(e.g. *bold* and http://url/).
-M --metadata name:value * Set metadata of the top level suite. Underscores
in the name and value are converted to spaces. Value
can contain same HTML formatting as --doc.
参考以下作者博客,敬谢:
https://www.cnblogs.com/alamZ/p/6943869.html
https://blog.zengrong.net/post/2192.html
python 模块中的 __init__.py __main__.py的更多相关文章
- Python - 模块中的"if __name__ == '__main__':"
1.1 如果导入的模块除了定义函数之外还中有可以执行代码,那么Python解释器在导入这个模块时就会执行这些代码. module1.py: def foo(): print('module 1') f ...
- python模块中的__all__属性
转自:http://blog.csdn.net/sxingming/article/details/52903377 python模块中的__all__属性,可用于模块导入时限制,如:from mod ...
- python模块中__init__.py的作用
基本概念先上结论举例解释实验一:不包含__init__.py实验二:A中包含__init__.py实验三:A.A_A中也包含__init__.py进阶基本概念概念 解释import 即导入,方式就是在 ...
- python模块中的特殊变量
37.模块的特殊变量: 显示模块中的变量 import s1 print(vars(s1)) 1.__doc__:打印注释信息. #!/usr/bin/env python # _ ...
- Python开发【第一篇】Python模块中特殊变量
模块中特殊变量 生产环境中,常用的就是__name__和__file__ __doc__ __package__ __cached__ __name__ __file__ 一. __doc__ #获 ...
- 嵌入Python系列 | 调用Python模块中无参数函数
开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...
- 嵌入Python | 调用Python模块中无参数的函数
开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...
- 简述 Python 类中的 __init__、__new__、__call__ 方法
任何事物都有一个从创建,被使用,再到消亡的过程,在程序语言面向对象编程模型中,对象也有相似的命运:创建.初始化.使用.垃圾回收,不同的阶段由不同的方法(角色)负责执行. 定义一个类时,大家用得最多的就 ...
- RobotFramework中加载自定义python包中的library(一个py文件中有多个类)
结构如下: appsdk\ appsdk.py(这里面有多个类,包括appsdk,appsdksync等类) __init__.py ... ① 有个appsdk的文件夹(符合python包的定义) ...
随机推荐
- 阅读MDN文档之层叠与继承(二)
目录 The cascade Importance Specificity Source order A note on rule mixing Inheritance Controlling inh ...
- git环境搭建以及第一个PHP程序
使用mac下的sublime等编辑器帮助代码编写,然后到linux下运行网页代码.可以通过/vagrant共享目录完成,但是默认apache默认目录为/var/www/html,不想改变该目录,同时为 ...
- Ruby学习之深入类
在讨论对象模型时,对类做了初步了解,关于类本身,还有许多知识需要学习. 类定义 Ruby中,可以用class关键字或者Class.new方法来定义一个类,在Ruby中,类定义的同时就是在运行代码,类和 ...
- robotframework的学习笔记(十四)------学习Robot Framework必须掌握的库—-BuiltIn库
作为一门表格语言,为了保持简单的结构,RF没有像别的高级语言那样提供类似if else while等内置关键字来实现各种逻辑功能,而是提供给了用户BuiltIn库.如果用户想在测试用例中实现比较复杂的 ...
- Validation of viewstate MAC failed 解决办法
大部分人都说是在页里或web.config里加EnableEventValidation="false" EnableViewStateMac="false" ...
- 计算机基础理论知识梳理篇(三):VLAN与VLAN网卡相关概念
VLAN(Virtual Local Area Network) 虚拟局域网(VLAN,802.1Q)是一组逻辑上的设备和用户,这些设备和用户并不受物理位置的限制,可以根据功能.部门及应用等因素将它们 ...
- 基于div表单模拟右对齐
基于div表单模拟右对齐 --------------------------------------------------------- ----------------------------- ...
- [已解决]This dependency was not found: * common/stylus/index.styl in ./src/main.js To install it, you can run: npm install --save common/stylus/index.styl
出现 This dependency was not found: * common/stylus/index.styl in ./src/main.js To install it, you can ...
- J2EE 项目本地发布路径及修改
J2EE的项目Run on Server后,在tomcat安装目录下的webapps没有出现所建立的工程名字. 很明显项目并没有自动部署到tomcat的webapps中而是部署在了别的容器中. 在内置 ...
- Ubuntu 搭建简单的git server
Git 可以使用四种主要的协议来传输资料:本地协议(Local),HTTP 协议,SSH(Secure Shell)协议及 Git 协议. 在此,我们将会讨论那些协议及哪些情形应该使用(或避免使用)他 ...