Executing modules as scripts

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).

时间: 2024-07-30 06:44:48

Executing modules as scripts的相关文章

Python Tutorial 学习(六)--Modules

6. Modules 当你退出Python的shell模式然后又重新进入的时候,之前定义的变量,函数等都会没有了. 因此, 推荐的做法是将这些东西写入文件,并在适当的时候调用获取他们. 这就是为人所知的脚本文件. 随着编程的深入,代码的增多,你可能又会将代码存到不同的文件中方便管理. 你会想到去使用之前的编程中已经写好了的一个函数的定义. Python有自己的方式去实现这些.它会将这些保存了定义的函数,类等的文件(文件夹)称作module; 一个module中的定义的函数 类等可以被导入到另一个

Python 2.7.8 学习笔记(001)python manuals/the python tutorial

从今天开始学python, python有点意思,第一感觉界面和matlab有点像. 手头没有什么资料,就从安装好了的一个python 2.7.8,里面有个英文版的manual,那就只好从这里开始吧,为什么不是中文版的呢??那就边看边翻译吧. python漫游指南:python是一种简单易学功能强大的编程语言.它有高效的数据结构和简单但有效的面向对象编程方法.python优雅的语法和动态拼写以及解释特性,使得它在许多平台上成为一种理想的脚本语言和快速程序开发工具. python的解释器和扩展标准

Python手册

如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使用文本编辑器来准备解释器的输入会更好,使用文件作为替代的输入.这也被称作创建脚本.当程序越来越长时,出于易于维护的原因,你可能会将程序分割为几个文件.你也可能想要在多个程序中使用很好用的一个函数,而不用将其定义拷贝到每一个程序中. 为了支持这些需求,Python提供了将定义放入一个文件的方式,并且在脚本或者解释器交互式实例中使用它们.这样的文件称为模块:模块中的定义可以导入到其他模

第一章 Codec Engine概要

本文翻译自TI的手册,该手册是学习GPP+DSP开发的金典文档,希望对各位入门有所帮助,有理解不当之处望请赐教. Codec Engine Application Developer User's Guide Literature Number: SPRUE67D Codec Engine 应用开发使用手册原文参见: http://blog.csdn.net/dyzok88/article/details/42154487 后期会更新<第二章 Codec Engine安装和设置>. // 正文

u-boot-2016.09顶层makefile分析

## SPDX-License-Identifier: GPL-2.0+# VERSION = 2016PATCHLEVEL = 09SUBLEVEL =EXTRAVERSION =NAME = # *DOCUMENTATION*# To see a list of typical targets execute "make help"# More info can be located in ./README# Comments in this file are targeted o

debugging tools

https://blogs.msdn.microsoft.com/debugdiag/ https://blogs.msdn.microsoft.com/debuggingtoolbox/2012/10/04/tools-for-your-debugging-toolbox/ TOOLS –        Performance Monitor – PAL –        Process Monitor –        Process Explorer –        MPSReport

如何搭建apache+svn+trac平台?

一.trac 概念Trac 是用Python写的一个基于Web的事件跟踪系统,它使用WiKi作为文档的格式,Subversion作为版本控制系统.可帮助开发人员进行源码版本管理.Bug跟踪和讨论.Debian 如果你的系统是Debian则用apt-get install trac命令安装即可,Trac依赖的所有软件包系统会自动安装.安装完成后,就可进行配置了.Trac可以设置成Standalone服务器,只供本机使用.也可以与Apache等Web服务器集成,如果与Apache集成,则可配置成CG

mysql编译错误记录

最近换了一家公司了,很多基础环境重新做,遇见一个问题,记录一下之前自动编译的mysql脚本发现无法使用了原先环境是cento6.7环境可以正常使用,现在使用centos7.4环境发现报错 /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql FATAL ERROR: please install the fo

Biopython - basics

Introduction From the biopython website their goal is to “make it as easy as possible to use Python for bioinformatics by creating high-quality, reusable modules and scripts.” These modules use the biopython tutorial as a template for what you will l