Python中的模块和脚本有什么区别? [英] What is the difference between a module and a script in Python?

查看:646
本文介绍了Python中的模块和脚本有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

认为标题概括了这个问题:-)

Think the title summarizes the question :-)

推荐答案

脚本通常是直接运行的一段代码,由其本身运行.模块通常是由其他代码段导入的库.

A script is generally a directly executable piece of code, run by itself. A module is generally a library, imported by other pieces of code.

请注意,没有内部区别-两者都是可执行的和可导入的,尽管在直接执行和导入旨在执行以下操作的代码时,库代码通常不执行任何操作(或仅运行其单元测试)是脚本会导致其执行,因此是常见的if __name__ == "__main__"测试.

Note that there's no internal distinction -- both are executable and importable, although library code often won't do anything (or will just run its unit tests) when executed directly and importing code designed to be a script will cause it to execute, hence the common if __name__ == "__main__" test.

这篇关于Python中的模块和脚本有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆