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包的定义) ...
随机推荐
- 4、树莓派的中文:安装ftp,安装gcc,安装qt,编译qt程序,运行qt界面程序
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.安装FTP:sudo apt-get install vsftpd 2.配置FTP,修改,/etc/vsftpd.conf # ...
- android的ADK下载地址
把下面所有的包下载到temp目录下进行安装. 用代理http://ppdaili.com/https://dl-ssl.google.com/android/repository/repository ...
- windows 安装Mysql压缩包
1.将mysql压缩包解压至选定目录下 2.修改my-default.ini文件的以下内容 basedir = E:\develope_software\MySQL\mysql-advanced-5. ...
- 程序猿的日常——Java中的集合列表
列表对于日常开发来说实在是太常见了,以至于很多开发者习惯性的用到数组,就来一个ArrayList,根本不做过多的思考.其实列表里面还是有很多玩法的,有时候玩不好,搞出来bug还得定位半天.所以这里就再 ...
- jQuery知识盲点
一.属性操作 元素的尺寸 width() height() innerWidth() innerHeight() outerWidth() outerHeight() $('div').width() ...
- MySQL字符串相关函数学习二
① LOWER(str):将字符串转为小写:与此函数具有相同作用的函数有LCASE() 如果参数是小写.数字或其他特殊字符,则返回原数据 ② LEFT(str, len):返回字符串str左边的len ...
- 用grant命令为用户赋权限以后,登录时,出现:ERROR 1045 (28000)
ERROR 1045(28000)信息是因为权限的问题.这个ERROR分为两种情况: 第一种: ERROR 1045 (28000): Access denied for user 'root'@'l ...
- Mac下安装ant(利用brew)
安装ant最简单的方法就是通过brew.步骤如下:1. 安装brew(如果已经安装可以跳过这步). ruby -e "$(curl -fsSL https://raw.github.com/ ...
- python3之OS与sys模块
一.Python os模块 包含普遍的操作系统功能,如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 1.os.access() 查看文件是否有指定权限,有则返回True否则返回flase ...
- [linux] C语言Linux系统编程-TCP通信的11种状态
三次握手由client主动发出SYN请求, 此时client处于SYN_SENT状态(第一次握手)当server收到之后会由LISTEN转变为SYN_REVD状态, 并回复client, client ...