Executing modules as scripts
2024-08-24 21:09:06
When you run a Python module with
python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the __name__
set to "__main__"
. That means that by adding this code at the end of your module:
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
$ python fibo.py 50
1 1 2 3 5 8 13 21 34
If the module is imported, the code is not run:
>>>
>>> import fibo
>>>
This is often used either to provide a convenient user interface to a module, or for testing purposes (running the module as a script executes a test suite).
最新文章
- Service 广播 到Fragment
- groupspecWidhoutAuthorizations与groupspecWidthAuthorizations的区别
- MFC ADO连接Sql Server数据库报无效指针的问题
- [转] 利用SET STATISTICS IO和SET STATISTICS TIME 优化SQL Server查询性能
- java抓取快递信息
- oracle的用户
- 在Iframe框架下如何跳转到登录界面
- C#调用WebService实例和开发
- Win7 Cygwin环境试验Nutch tutorial遇到的异常解决方法
- (11.28)Java小知识!
- Nginx作为HTTP服务器--Nginx配置图片服务器
- linux入门--Linux和UNIX的关系及区别
- 主引导扇区MBR的解析
- [ZJOI2012]旅游
- (转载)windows下安装配置Xampp
- RecyclerView拖拽排序;
- adb push和adb install区别
- Revit API单位转换类
- Postgres快速创建大量测试数据
- phpstorm中FTP自动同步功能
热门文章
- iframe-摘自网友
- (巨坑)改了tpl文件之后,前端效果没反应
- K2工作流的使用
- BZOJ1230 [Usaco2008 Nov]lites 开关灯
- [转载]Windows 7笔记本创建wifi热点供手机上网教程
- CentOS查看软件源提供的软件版本命令
- [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想
- 类似UC天气下拉和微信下拉眼睛头部弹入淡出UI交互效果(开源项目)。
- bzoj 1934: [Shoi2007]Vote 善意的投票
- hdu 4607 Park Visit