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

查看:34
本文介绍了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天全站免登陆